Browse Source

wip, state & plugin

David Sehnal 6 years ago
parent
commit
f259499374

+ 11 - 0
src/mol-plugin/command.ts

@@ -6,6 +6,7 @@
 
 import { PluginContext } from './context';
 import { LinkedList } from 'mol-data/generic';
+import { RxEventHelper } from 'mol-util/rx-event-helper';
 
 export { PluginCommand }
 
@@ -42,6 +43,16 @@ namespace PluginCommand {
         private queue = LinkedList<Instance>();
         private disposing = false;
 
+        private ev = RxEventHelper.create();
+
+        readonly behaviour = {
+            locked: this.ev.behavior<boolean>(false)
+        };
+
+        lock(locked: boolean = true) {
+            this.behaviour.locked.next(locked);
+        }
+
         subscribe<T>(cmd: Descriptor<T>, action: Action<T>) {
             let actions = this.subs.get(cmd.id);
             if (!actions) {

+ 5 - 1
src/mol-plugin/context.ts

@@ -18,14 +18,18 @@ export class PluginContext {
     private ev = RxEventHelper.create();
 
     readonly state = new PluginState(this);
+    readonly commands = new PluginCommand.Manager();
 
     readonly events = {
         data: this.state.data.context.events
     };
 
+    readonly behaviors = {
+        command: this.commands.behaviour
+    };
+
     readonly canvas3d: Canvas3D;
 
-    readonly commands = new PluginCommand.Manager();
 
     initViewer(canvas: HTMLCanvasElement, container: HTMLDivElement) {
         try {

+ 7 - 2
src/mol-state/context.ts

@@ -13,7 +13,7 @@ export { StateContext }
 class StateContext {
     private ev = RxEventHelper.create();
 
-    events = {
+    readonly events = {
         object: {
             stateChanged: this.ev<{ ref: Transform.Ref }>(),
             propsChanged: this.ev<{ ref: Transform.Ref, newProps: unknown }>(),
@@ -29,6 +29,10 @@ class StateContext {
         updated: this.ev<void>()
     };
 
+    readonly behaviours = {
+        currentObject: this.ev.behavior<{ ref: Transform.Ref }>(void 0 as any)
+    };
+
     readonly globalContext: unknown;
     readonly defaultObjectProps: unknown;
 
@@ -36,8 +40,9 @@ class StateContext {
         this.ev.dispose();
     }
 
-    constructor(params: { globalContext: unknown, defaultObjectProps: unknown }) {
+    constructor(params: { globalContext: unknown, defaultObjectProps: unknown, rootRef: Transform.Ref }) {
         this.globalContext = params.globalContext;
         this.defaultObjectProps = params.defaultObjectProps;
+        this.behaviours.currentObject.next({ ref: params.rootRef });
     }
 }

+ 3 - 1
src/mol-state/state.ts

@@ -89,7 +89,8 @@ class State {
 
         this.context = new StateContext({
             globalContext: params && params.globalContext,
-            defaultObjectProps
+            defaultObjectProps,
+            rootRef: tree.rootRef
         });
     }
 }
@@ -125,6 +126,7 @@ namespace State {
             ctx.objects.delete(d);
             ctx.transformCache.delete(d);
             ctx.stateCtx.events.object.removed.next({ ref: d });
+            // TODO: handle current object change
         }
 
         initObjectState(ctx, roots);

+ 9 - 1
src/mol-util/rx-event-helper.ts

@@ -4,12 +4,13 @@
  * @author David Sehnal <david.sehnal@gmail.com>
  */
 
-import { Subject } from 'rxjs';
+import { Subject, BehaviorSubject } from 'rxjs';
 
 export { RxEventHelper }
 
 interface RxEventHelper {
     <T>(): Subject<T>,
+    behavior<T>(v: T): BehaviorSubject<T>,
     dispose(): void
 }
 
@@ -31,6 +32,13 @@ class _RxEventHelper {
         this._eventList.push(s);
         return s;
     }
+
+    behavior<T>(v: T) {
+        const s = new BehaviorSubject<T>(v);
+        this._eventList.push(s);
+        return s;
+    }
+
     dispose() {
         if (this._disposed) return;
         for (const e of this._eventList) e.complete();