From 111a33ccd46d05f1908d346272521d0e51537822 Mon Sep 17 00:00:00 2001 From: David Sehnal <david.sehnal@gmail.com> Date: Mon, 4 Mar 2019 22:02:49 +0100 Subject: [PATCH] mol-state: isUpdating event; plugin state commad tweaks --- src/mol-plugin/behavior/static/state.ts | 14 +++++++++----- src/mol-plugin/command.ts | 2 ++ src/mol-plugin/context.ts | 1 + src/mol-plugin/ui/controls.tsx | 1 + src/mol-state/state.ts | 5 ++++- 5 files changed, 17 insertions(+), 6 deletions(-) diff --git a/src/mol-plugin/behavior/static/state.ts b/src/mol-plugin/behavior/static/state.ts index 6d28606ed..e41e7e564 100644 --- a/src/mol-plugin/behavior/static/state.ts +++ b/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}`); } diff --git a/src/mol-plugin/command.ts b/src/mol-plugin/command.ts index 29e8929f1..b373682d4 100644 --- a/src/mol-plugin/command.ts +++ b/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 }>(), diff --git a/src/mol-plugin/context.ts b/src/mol-plugin/context.ts index 7fd0a9a0c..9f25b9e95 100644 --- a/src/mol-plugin/context.ts +++ b/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, }, diff --git a/src/mol-plugin/ui/controls.tsx b/src/mol-plugin/ui/controls.tsx index 847b7b215..00517be8e 100644 --- a/src/mol-plugin/ui/controls.tsx +++ b/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) { diff --git a/src/mol-state/state.ts b/src/mol-state/state.ts index 129969ad0..4da90c905 100644 --- a/src/mol-state/state.ts +++ b/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) }); -- GitLab