Skip to content
Snippets Groups Projects
Commit d3949137 authored by David Sehnal's avatar David Sehnal
Browse files

mol-plugin: wip

parent 5e60a5b2
No related branches found
No related tags found
No related merge requests found
/**
* Copyright (c) 2018 mol* contributors, licensed under MIT, See LICENSE file for more info.
*
* @author David Sehnal <david.sehnal@gmail.com>
*/
import { State } from 'mol-state';
import Viewer from 'mol-canvas3d/viewer';
export class PluginContext {
state = {
data: State,
behaviour: State,
plugin: State
};
viewer: Viewer;
// logger = ;
// settings = ;
}
\ No newline at end of file
/**
* Copyright (c) 2018 mol* contributors, licensed under MIT, See LICENSE file for more info.
*
* @author David Sehnal <david.sehnal@gmail.com>
*/
interface PluginSpec {
}
export { PluginSpec }
\ No newline at end of file
/**
* Copyright (c) 2018 mol* contributors, licensed under MIT, See LICENSE file for more info.
*
* @author David Sehnal <david.sehnal@gmail.com>
*/
import { StateObject, Transformer } from 'mol-state';
export type TypeClass = 'root' | 'data' | 'prop'
export namespace PluginStateObject {
export type TypeClass = 'Root' | 'Group' | 'Data' | 'Object' | 'Representation' | 'Behaviour'
export interface TypeInfo { name: string, shortName: string, description: string, typeClass: TypeClass }
export interface PluginStateObjectProps { label: string }
export const Create = StateObject.factory<TypeInfo, PluginStateObjectProps>();
}
export namespace PluginStateTransform {
export const Create = Transformer.factory('ms-plugin');
}
/**
* Copyright (c) 2018 mol* contributors, licensed under MIT, See LICENSE file for more info.
*
* @author David Sehnal <david.sehnal@gmail.com>
*/
import { PluginStateObject } from './base';
import { CifFile } from 'mol-io/reader/cif';
import { Model as _Model, Structure as _Structure } from 'mol-model/structure'
const _create = PluginStateObject.Create
namespace PluginStateObjects {
export class Root extends _create({ name: 'Root', shortName: 'R', typeClass: 'Root', description: 'Where everything begins.' }) { }
export class Group extends _create({ name: 'Group', shortName: 'G', typeClass: 'Group', description: 'A group on entities.' }) { }
export namespace Data {
export class String extends _create<string>({ name: 'String Data', typeClass: 'Data', shortName: 'S_D', description: 'A string.' }) { }
export class Binary extends _create<Uint8Array>({ name: 'Binary Data', typeClass: 'Data', shortName: 'B_D', description: 'A binary blob.' }) { }
export class Json extends _create<any>({ name: 'JSON Data', typeClass: 'Data', shortName: 'JS_D', description: 'Represents JSON data.' }) { }
export class Cif extends _create<CifFile>({ name: 'Cif File', typeClass: 'Data', shortName: 'CF', description: 'Represents parsed CIF data.' }) { }
// TODO
// export class MultipleRaw extends _create<{
// [key: string]: { type: 'String' | 'Binary', data: string | Uint8Array }
// }>({ name: 'Data', typeClass: 'Data', shortName: 'MD', description: 'Multiple Keyed Data.' }) { }
}
export class Model extends _create<_Model>({ name: 'Molecule Model', typeClass: 'Object', shortName: 'M_M', description: 'A model of a molecule.' }) { }
export class Structure extends _create<_Structure>({ name: 'Molecule Structure', typeClass: 'Object', shortName: 'M_S', description: 'A structure of a molecule.' }) { }
export class StructureRepresentation extends _create<{
// TODO
}>({ name: 'Molecule Structure Representation', typeClass: 'Representation', shortName: 'S_R', description: 'A representation of a molecular structure.' }) { }
}
export { PluginStateObjects }
\ No newline at end of file
......@@ -13,10 +13,11 @@ interface StateContext {
object: {
stateChanged: Subject<{ ref: Transform.Ref }>,
propsChanged: Subject<{ ref: Transform.Ref, newProps: unknown }>,
updated: Subject<{ ref: Transform.Ref }>,
replaced: Subject<{ ref: Transform.Ref, old?: StateObject }>,
created: Subject<{ ref: Transform.Ref }>,
removed: Subject<{ ref: Transform.Ref }>,
updated: Subject<{ ref: Transform.Ref, obj?: StateObject }>,
replaced: Subject<{ ref: Transform.Ref, oldObj?: StateObject, newObj?: StateObject }>,
created: Subject<{ ref: Transform.Ref, obj: StateObject }>,
removed: Subject<{ ref: Transform.Ref, obj?: StateObject }>,
},
warn: Subject<string>
},
......
......@@ -6,10 +6,11 @@
*/
import { Transform } from './transform';
import { UUID } from 'mol-util';
/** A mutable state object */
export interface StateObject<P = unknown, D = unknown> {
ref: Transform.Ref,
readonly id: UUID,
readonly type: StateObject.Type,
readonly props: P,
readonly data: D
......@@ -28,18 +29,18 @@ export namespace StateObject {
}
export interface Type<Info = any> {
kind: string,
info: Info
}
export function factory<TypeInfo, CommonProps>() {
return <D = { }, P = {}>(kind: string, info: TypeInfo) => create<P & CommonProps, D, TypeInfo>(kind, info);
return <D = { }, P = {}>(typeInfo: TypeInfo) => create<P & CommonProps, D, TypeInfo>(typeInfo);
}
export function create<Props, Data, TypeInfo>(kind: string, typeInfo: TypeInfo) {
const dataType: Type<TypeInfo> = { kind, info: typeInfo };
export function create<Props, Data, TypeInfo>(typeInfo: TypeInfo) {
const dataType: Type<TypeInfo> = { info: typeInfo };
return class implements StateObject<Props, Data> {
static type = dataType;
id = UUID.create();
type = dataType;
ref = 'not set' as Transform.Ref;
constructor(public props: Props, public data: Data) { }
......
......@@ -29,7 +29,6 @@ export namespace State {
const root = tree.getValue(tree.rootRef)!;
const defaultObjectProps = (params && params.defaultObjectProps) || { }
rootObject.ref = tree.rootRef;
objects.set(tree.rootRef, {
ref: tree.rootRef,
obj: rootObject,
......@@ -185,11 +184,11 @@ export namespace State {
const update = await updateNode(ctx, root);
setObjectState(ctx, root, StateObject.StateType.Ok);
if (update.action === 'created') {
ctx.stateCtx.events.object.created.next({ ref: root });
ctx.stateCtx.events.object.created.next({ ref: root, obj: update.obj! });
} else if (update.action === 'updated') {
ctx.stateCtx.events.object.updated.next({ ref: root });
ctx.stateCtx.events.object.updated.next({ ref: root, obj: update.obj });
} else if (update.action === 'replaced') {
ctx.stateCtx.events.object.replaced.next({ ref: root, old: update.old });
ctx.stateCtx.events.object.replaced.next({ ref: root, oldObj: update.oldObj, newObj: update.newObj });
}
} catch (e) {
doError(ctx, root, '' + e);
......@@ -212,7 +211,6 @@ export namespace State {
if (!oldTree.nodes.has(currentRef) || !objects.has(currentRef)) {
// console.log('creating...', transform.transformer.id, oldTree.nodes.has(currentRef), objects.has(currentRef));
const obj = await createObject(ctx, transform.transformer, parent, transform.params);
obj.ref = currentRef;
objects.set(currentRef, {
ref: currentRef,
obj,
......@@ -220,7 +218,7 @@ export namespace State {
version: transform.version,
props: { ...ctx.stateCtx.defaultObjectProps, ...transform.defaultProps }
});
return { action: 'created' };
return { action: 'created', obj };
} else {
// console.log('updating...', transform.transformer.id);
const current = objects.get(currentRef)!;
......@@ -228,7 +226,6 @@ export namespace State {
switch (await updateObject(ctx, transform.transformer, parent, current.obj!, oldParams, transform.params)) {
case Transformer.UpdateResult.Recreate: {
const obj = await createObject(ctx, transform.transformer, parent, transform.params);
obj.ref = currentRef;
objects.set(currentRef, {
ref: currentRef,
obj,
......@@ -236,12 +233,12 @@ export namespace State {
version: transform.version,
props: { ...ctx.stateCtx.defaultObjectProps, ...current.props, ...transform.defaultProps }
});
return { action: 'replaced', old: current.obj! };
return { action: 'replaced', oldObj: current.obj!, newObj: obj };
}
case Transformer.UpdateResult.Updated:
current.version = transform.version;
current.props = { ...ctx.stateCtx.defaultObjectProps, ...current.props, ...transform.defaultProps };
return { action: 'updated' };
return { action: 'updated', obj: current.obj };
default:
// TODO check if props need to be updated
return { action: 'none' };
......
......@@ -9,10 +9,10 @@ export interface TypeInfo { name: string, class: TypeClass }
const _obj = StateObject.factory<TypeInfo, ObjProps>()
const _transform = Transformer.factory('test');
export class Root extends _obj('root', { name: 'Root', class: 'root' }) { }
export class Square extends _obj<{ a: number }>('square', { name: 'Square', class: 'shape' }) { }
export class Circle extends _obj<{ r: number }>('circle', { name: 'Circle', class: 'shape' }) { }
export class Area extends _obj<{ volume: number }>('volume', { name: 'Volume', class: 'prop' }) { }
export class Root extends _obj({ name: 'Root', class: 'root' }) { }
export class Square extends _obj<{ a: number }>({ name: 'Square', class: 'shape' }) { }
export class Circle extends _obj<{ r: number }>({ name: 'Circle', class: 'shape' }) { }
export class Area extends _obj<{ volume: number }>({ name: 'Area', class: 'prop' }) { }
export const CreateSquare = _transform<Root, Square, { a: number }>({
name: 'create-square',
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment