From 3a22246e6b507b6e247d504d3c4aa77dde73d217 Mon Sep 17 00:00:00 2001 From: David Sehnal <david.sehnal@gmail.com> Date: Tue, 16 Oct 2018 15:46:05 +0200 Subject: [PATCH] mol-state: wip data model --- package.json | 1 + src/mol-state/context/context.ts | 9 ++++ src/mol-state/context/event.ts | 9 ++++ src/mol-state/index.ts | 5 ++ src/mol-state/model/node.ts | 17 +++++++ src/mol-state/model/reconcile.ts | 12 +++++ src/mol-state/model/tree.ts | 9 ++++ src/mol-state/transform/transform.ts | 27 ++++++++++ src/mol-state/transform/transformer.ts | 69 ++++++++++++++++++++++++++ src/mol-state/transform/tree.ts | 17 +++++++ tsconfig.json | 1 + 11 files changed, 176 insertions(+) create mode 100644 src/mol-state/context/context.ts create mode 100644 src/mol-state/context/event.ts create mode 100644 src/mol-state/index.ts create mode 100644 src/mol-state/model/node.ts create mode 100644 src/mol-state/model/reconcile.ts create mode 100644 src/mol-state/model/tree.ts create mode 100644 src/mol-state/transform/transform.ts create mode 100644 src/mol-state/transform/transformer.ts create mode 100644 src/mol-state/transform/tree.ts diff --git a/package.json b/package.json index 33864e84f..560a89853 100644 --- a/package.json +++ b/package.json @@ -60,6 +60,7 @@ "mol-ql($|/.*)": "<rootDir>/src/mol-ql$1", "mol-script($|/.*)": "<rootDir>/src/mol-script$1", "mol-task($|/.*)": "<rootDir>/src/mol-task$1", + "mol-state($|/.*)": "<rootDir>/src/mol-state$1", "mol-util($|/.*)": "<rootDir>/src/mol-util$1", "mol-view($|/.*)": "<rootDir>/src/mol-view$1" }, diff --git a/src/mol-state/context/context.ts b/src/mol-state/context/context.ts new file mode 100644 index 000000000..b7fd34c4c --- /dev/null +++ b/src/mol-state/context/context.ts @@ -0,0 +1,9 @@ +/** + * Copyright (c) 2018 mol* contributors, licensed under MIT, See LICENSE file for more info. + * + * @author David Sehnal <david.sehnal@gmail.com> + */ + +export interface StateContext { + +} \ No newline at end of file diff --git a/src/mol-state/context/event.ts b/src/mol-state/context/event.ts new file mode 100644 index 000000000..32dcb9a05 --- /dev/null +++ b/src/mol-state/context/event.ts @@ -0,0 +1,9 @@ +/** + * Copyright (c) 2018 mol* contributors, licensed under MIT, See LICENSE file for more info. + * + * @author David Sehnal <david.sehnal@gmail.com> + */ + +export interface EventDispatcher { + // TODO +} \ No newline at end of file diff --git a/src/mol-state/index.ts b/src/mol-state/index.ts new file mode 100644 index 000000000..9dd0621de --- /dev/null +++ b/src/mol-state/index.ts @@ -0,0 +1,5 @@ +/** + * Copyright (c) 2018 mol* contributors, licensed under MIT, See LICENSE file for more info. + * + * @author David Sehnal <david.sehnal@gmail.com> + */ \ No newline at end of file diff --git a/src/mol-state/model/node.ts b/src/mol-state/model/node.ts new file mode 100644 index 000000000..cd4807c60 --- /dev/null +++ b/src/mol-state/model/node.ts @@ -0,0 +1,17 @@ +/** + * Copyright (c) 2018 mol* contributors, licensed under MIT, See LICENSE file for more info. + * + * @author David Sehnal <david.sehnal@gmail.com> + */ + +export interface ModelNode<T = any> { + '@type': T +} + +export namespace ModelNode { + export type TypeOf<T> + = T extends ModelNode<infer X> ? [X] + : T extends [ModelNode<infer X>] ? [X] + : T extends [ModelNode<infer X>, ModelNode<infer Y>] ? [X, Y] + : unknown[]; +} \ No newline at end of file diff --git a/src/mol-state/model/reconcile.ts b/src/mol-state/model/reconcile.ts new file mode 100644 index 000000000..95997d7ce --- /dev/null +++ b/src/mol-state/model/reconcile.ts @@ -0,0 +1,12 @@ +/** + * Copyright (c) 2018 mol* contributors, licensed under MIT, See LICENSE file for more info. + * + * @author David Sehnal <david.sehnal@gmail.com> + */ + +import { TransformTree } from '../transform/tree'; +import { ModelTree } from './tree'; + +export function reconcileTree(transform: TransformTree, model: ModelTree, root?: number) { + // TODO +} \ No newline at end of file diff --git a/src/mol-state/model/tree.ts b/src/mol-state/model/tree.ts new file mode 100644 index 000000000..601f3be43 --- /dev/null +++ b/src/mol-state/model/tree.ts @@ -0,0 +1,9 @@ +/** + * Copyright (c) 2018 mol* contributors, licensed under MIT, See LICENSE file for more info. + * + * @author David Sehnal <david.sehnal@gmail.com> + */ + +export interface ModelTree { + +} \ No newline at end of file diff --git a/src/mol-state/transform/transform.ts b/src/mol-state/transform/transform.ts new file mode 100644 index 000000000..7234fd6fc --- /dev/null +++ b/src/mol-state/transform/transform.ts @@ -0,0 +1,27 @@ +/** + * Copyright (c) 2018 mol* contributors, licensed under MIT, See LICENSE file for more info. + * + * @author David Sehnal <david.sehnal@gmail.com> + */ + +import { Transform } from './transform'; +import { ModelNode } from '../model/node'; +import { Transformer } from './transformer'; + +export interface Transform<A extends ModelNode, B extends ModelNode, P = any> { + readonly instanceId: number, + + readonly transformer: Transformer<A, B, P>, + readonly props: Transform.Props, + + readonly transformerId: string, + readonly params: P, + readonly ref: string, + readonly version: number, +} + +export namespace Transform { + export interface Props { + + } +} \ No newline at end of file diff --git a/src/mol-state/transform/transformer.ts b/src/mol-state/transform/transformer.ts new file mode 100644 index 000000000..271710542 --- /dev/null +++ b/src/mol-state/transform/transformer.ts @@ -0,0 +1,69 @@ +/** + * Copyright (c) 2018 mol* contributors, licensed under MIT, See LICENSE file for more info. + * + * @author David Sehnal <david.sehnal@gmail.com> + */ + +import { Task } from 'mol-task'; +import { EventDispatcher } from '../context/event'; +import { ModelNode } from '../model/node'; +import { ModelTree } from '../model/tree'; + +export interface Transformer<A extends ModelNode, B extends ModelNode, P = any> { + readonly id: Transformer.Id, + readonly definition: Transformer.Definition<A, B, P> +} + +export namespace Transformer { + export type Id = string & { '@type': 'transformer-id' } + export type Params<T extends Transformer<any, any, any>> = T extends Transformer<any, any, infer P> ? P : unknown; + + export interface Definition<A extends ModelNode, B extends ModelNode, P> { + readonly name: string, + readonly namespace: string, + readonly description?: string, + readonly from: ModelNode.TypeOf<A>[], + readonly to: ModelNode.TypeOf<B>[] + + /** + * Apply the actual transformation. It must be pure (i.e. with no side effects). + * Returns a task that produces the result of the result directly. + */ + apply(a: A, params: P, context: TransformContext): Task<B> | B, + + /** + * Attempts to update the entity in a non-destructive way. + * For example changing a color scheme of a visual does not require computing new geometry. + * Return/resolve to undefined if the update is not possible. + * + * The ability to resolve the task to undefined is present for "async updates" (i.e. containing an ajax call). + */ + update?(a: A, b: B, newParams: P, context: TransformContext): Task<B | undefined> | B | undefined, + + /** Check the parameters and return a list of errors if the are not valid. */ + defaultParams?(a: A, context: TransformContext): P, + + /** */ + defaultControls?(a: A, context: TransformContext): ControlsFor<P>, + + /** Check the parameters and return a list of errors if the are not valid. */ + validateParams?(a: A, params: P, context: TransformContext): string[] | undefined, + + /** Test if the transform can be applied to a given node */ + isApplicable?(a: A, context: TransformContext): boolean, + + /** By default, returns true */ + isSerializable?(params: P): { isSerializable: true } | { isSerializable: false; reason: string }, + } + + export type ControlsFor<Props> = { [P in keyof Props]: any } + + /** A tree context constructed dynamically duing application of transforms. */ + export interface TransformContext { + /** An event dispatcher for executing child tasks. */ + dispatcher: EventDispatcher, + + globalContext: any, + tree: ModelTree + } +} \ No newline at end of file diff --git a/src/mol-state/transform/tree.ts b/src/mol-state/transform/tree.ts new file mode 100644 index 000000000..ee9863182 --- /dev/null +++ b/src/mol-state/transform/tree.ts @@ -0,0 +1,17 @@ +/** + * Copyright (c) 2018 mol* contributors, licensed under MIT, See LICENSE file for more info. + * + * @author David Sehnal <david.sehnal@gmail.com> + */ + +export interface TransformTree { + // TODO +} + +export namespace TransformTree { + export interface Update { + readonly tree: TransformTree, + readonly rootId: number, + readonly params: unknown + } +} \ No newline at end of file diff --git a/tsconfig.json b/tsconfig.json index 413bfe585..aa0ec9bcc 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -24,6 +24,7 @@ "mol-model-props": ["./mol-model-props", "./mol-model-props/index.ts"], "mol-ql": ["./mol-ql"], "mol-script": ["./mol-script"], + "mol-state": ["./mol-state", "./mol-state/index.ts"], "mol-task": ["./mol-task", "./mol-task/index.ts"], "mol-util": ["./mol-util", "./mol-util/index.ts"], "mol-view": ["./mol-view"] -- GitLab