Explorar el Código

mol-state: isUpdating event; plugin state commad tweaks

David Sehnal hace 6 años
padre
commit
111a33ccd4

+ 9 - 5
src/mol-plugin/behavior/static/state.ts

@@ -135,6 +135,10 @@ export function Snapshots(ctx: PluginContext) {
     PluginCommands.State.Snapshots.Apply.subscribe(ctx, ({ id }) => {
         const snapshot = ctx.state.snapshots.setCurrent(id);
         if (!snapshot) return;
+        return PluginCommands.State.Snapshots.Set.dispatch(ctx, { snapshot });
+    });
+
+    PluginCommands.State.Snapshots.Set.subscribe(ctx, ({ snapshot }) => {
         return ctx.state.setSnapshot(snapshot);
     });
 
@@ -150,9 +154,9 @@ export function Snapshots(ctx: PluginContext) {
 
     PluginCommands.State.Snapshots.Fetch.subscribe(ctx, async ({ url }) => {
         const json = await ctx.runTask(ctx.fetch({ url, type: 'json' })); //  fetch(url, { referrer: 'no-referrer' });
-        const current = ctx.state.snapshots.setRemoteSnapshot(json.data);
-        if (!current) return;
-        return ctx.state.setSnapshot(current);
+        const snapshot = ctx.state.snapshots.setRemoteSnapshot(json.data);
+        if (!snapshot) return;
+        return PluginCommands.State.Snapshots.Set.dispatch(ctx, { snapshot });
     });
 
     PluginCommands.State.Snapshots.DownloadToFile.subscribe(ctx, ({ name }) => {
@@ -169,8 +173,8 @@ export function Snapshots(ctx: PluginContext) {
     PluginCommands.State.Snapshots.OpenFile.subscribe(ctx, async ({ file }) => {
         try {
             const data = await readFromFile(file, 'string').run();
-            const json = JSON.parse(data as string);
-            await ctx.state.setSnapshot(json);
+            const snapshot = JSON.parse(data as string);
+            return PluginCommands.State.Snapshots.Set.dispatch(ctx, { snapshot });
         } catch (e) {
             ctx.log.error(`Reading JSON state: ${e}`);
         }

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

@@ -33,6 +33,8 @@ export const PluginCommands = {
             Apply: PluginCommand<{ id: string }>({ isImmediate: true }),
             Clear: PluginCommand<{}>({ isImmediate: true }),
 
+            Set: PluginCommand<{ snapshot: PluginState.Snapshot }>(),
+
             Upload: PluginCommand<{ name?: string, description?: string, serverUrl: string }>({ isImmediate: true }),
             Fetch: PluginCommand<{ url: string }>(),
 

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

@@ -55,6 +55,7 @@ export class PluginContext {
                 removed: merge(this.state.dataState.events.object.removed, this.state.behaviorState.events.object.removed),
                 updated: merge(this.state.dataState.events.object.updated, this.state.behaviorState.events.object.updated)
             },
+            isUpdating: merge(this.state.dataState.events.isUpdating, this.state.behaviorState.events.isUpdating),
             cameraSnapshots: this.state.cameraSnapshots.events,
             snapshots: this.state.snapshots.events,
         },

+ 1 - 0
src/mol-plugin/ui/controls.tsx

@@ -91,6 +91,7 @@ export class StateSnapshotViewportControls extends PluginUIComponent<{}, { isBus
     componentDidMount() {
         // TODO: this needs to be diabled when the state is updating!
         this.subscribe(this.plugin.state.snapshots.events.changed, () => this.forceUpdate());
+        this.subscribe(this.plugin.events.state.isUpdating, (isBusy) => this.setState({ isBusy }));
     }
 
     async update(id: string) {

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

@@ -42,7 +42,8 @@ class State {
             removed: this.ev<State.ObjectEvent & { obj?: StateObject }>()
         },
         log: this.ev<LogEntry>(),
-        changed: this.ev<void>()
+        changed: this.ev<void>(),
+        isUpdating: this.ev<boolean>()
     };
 
     readonly behaviors = {
@@ -130,6 +131,7 @@ class State {
     updateTree(tree: StateTree | StateBuilder, options?: Partial<State.UpdateOptions>): Task<void>
     updateTree(tree: StateTree | StateBuilder, options?: Partial<State.UpdateOptions>): Task<any> {
         return Task.create('Update Tree', async taskCtx => {
+            this.events.isUpdating.next(true);
             let updated = false;
             const ctx = this.updateTreeAndCreateCtx(tree, taskCtx, options);
             try {
@@ -140,6 +142,7 @@ class State {
                 }
             } finally {
                 if (updated) this.events.changed.next();
+                this.events.isUpdating.next(false);
 
                 for (const ref of ctx.stateChanges) {
                     this.events.cell.stateUpdated.next({ state: this, ref, cellState: this.tree.cellStates.get(ref) });