123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384 |
- /**
- * Copyright (c) 2018 mol* contributors, licensed under MIT, See LICENSE file for more info.
- *
- * @author David Sehnal <david.sehnal@gmail.com>
- */
- import { StateObject } from '../object';
- import { Transformer } from '../transformer';
- import { UUID } from 'mol-util';
- export interface Transform<A extends StateObject = StateObject, B extends StateObject = StateObject, P = unknown> {
- readonly transformer: Transformer<A, B, P>,
- readonly params: P,
- readonly ref: Transform.Ref,
- readonly version: string,
- readonly defaultProps?: unknown
- }
- export namespace Transform {
- export type Ref = string
- export interface Props {
- ref: Ref
- }
- export enum Flags {
- // Indicates that the transform was generated by a behaviour and should not be automatically updated
- Generated
- }
- export function create<A extends StateObject, B extends StateObject, P>(transformer: Transformer<A, B, P>, params?: P, props?: Partial<Props>): Transform<A, B, P> {
- const ref = props && props.ref ? props.ref : UUID.create() as string as Ref;
- return {
- transformer,
- params: params || { } as any,
- ref,
- version: UUID.create()
- }
- }
- export function updateParams<T>(t: Transform, params: any): Transform {
- return { ...t, params, version: UUID.create() };
- }
- export function createRoot(ref: Ref): Transform {
- return create(Transformer.ROOT, {}, { ref });
- }
- export interface Serialized {
- transformer: string,
- params: any,
- ref: string,
- version: string,
- defaultProps?: unknown
- }
- function _id(x: any) { return x; }
- export function toJSON(t: Transform): Serialized {
- const pToJson = t.transformer.definition.customSerialization
- ? t.transformer.definition.customSerialization.toJSON
- : _id;
- return {
- transformer: t.transformer.id,
- params: pToJson(t.params),
- ref: t.ref,
- version: t.version,
- defaultProps: t.defaultProps
- };
- }
- export function fromJSON(t: Serialized): Transform {
- const transformer = Transformer.get(t.transformer);
- const pFromJson = transformer.definition.customSerialization
- ? transformer.definition.customSerialization.toJSON
- : _id;
- return {
- transformer,
- params: pFromJson(t.params),
- ref: t.ref,
- version: t.version,
- defaultProps: t.defaultProps
- };
- }
- }
|