Browse Source

mol-plugin: TaskManager.requestAbortAll
+ modified requestAbort to accept taskId

David Sehnal 4 years ago
parent
commit
fff9719e48
3 changed files with 27 additions and 21 deletions
  1. 1 1
      src/mol-plugin-ui/task.tsx
  2. 14 18
      src/mol-plugin/context.ts
  3. 12 2
      src/mol-plugin/util/task-manager.ts

+ 1 - 1
src/mol-plugin-ui/task.tsx

@@ -34,7 +34,7 @@ export class BackgroundTaskProgress extends PluginUIComponent<{ }, { tracked: Or
 
 class ProgressEntry extends PluginUIComponent<{ event: TaskManager.ProgressEvent }> {
     abort = () => {
-        this.plugin.requestTaskAbort(this.props.event.progress, 'User Request');
+        this.plugin.managers.task.requestAbort(this.props.event.progress.root.progress.taskId, 'User Request');
     }
 
     render() {

+ 14 - 18
src/mol-plugin/context.ts

@@ -30,7 +30,7 @@ import { Representation } from '../mol-repr/representation';
 import { StructureRepresentationRegistry } from '../mol-repr/structure/registry';
 import { VolumeRepresentationRegistry } from '../mol-repr/volume/registry';
 import { StateTransform } from '../mol-state';
-import { Progress, Task, RuntimeContext } from '../mol-task';
+import { Task, RuntimeContext } from '../mol-task';
 import { ColorTheme } from '../mol-theme/color';
 import { SizeTheme } from '../mol-theme/size';
 import { ThemeRegistryContext } from '../mol-theme/theme';
@@ -62,7 +62,7 @@ import { filter, take } from 'rxjs/operators';
 import { Vec2 } from '../mol-math/linear-algebra';
 
 export class PluginContext {
-    runTask = <T>(task: Task<T>) => this.tasks.run(task);
+    runTask = <T>(task: Task<T>) => this.managers.task.run(task);
     resolveTask = <T>(object: Task<T> | T | undefined) => {
         if (!object) return void 0;
         if (Task.is(object)) return this.runTask(object);
@@ -71,19 +71,10 @@ export class PluginContext {
 
     private disposed = false;
     private ev = RxEventHelper.create();
-    private tasks = new TaskManager();
 
     readonly state = new PluginState(this);
     readonly commands = new PluginCommandManager();
 
-    readonly events = {
-        log: this.ev<LogEntry>(),
-        task: this.tasks.events,
-        canvas3d: {
-            settingsUpdated: this.ev(),
-        }
-    } as const
-
     readonly config = new PluginConfigManager(this.spec.config);
 
     private canvas3dInit = this.ev.behavior<boolean>(false);
@@ -166,8 +157,17 @@ export class PluginContext {
         snapshot: new PluginStateSnapshotManager(this),
         lociLabels: void 0 as any as LociLabelManager,
         toast: new PluginToastManager(this),
-        asset: new AssetManager()
-    } as const
+        asset: new AssetManager(),
+        task: new TaskManager()
+    } as const;
+
+    readonly events = {
+        log: this.ev<LogEntry>(),
+        task: this.managers.task.events,
+        canvas3d: {
+            settingsUpdated: this.ev(),
+        }
+    } as const;
 
     readonly customModelProperties = new CustomProperty.Registry<Model>();
     readonly customStructureProperties = new CustomProperty.Registry<Structure>();
@@ -232,10 +232,6 @@ export class PluginContext {
         return this.runTask(this.state.data.transaction(f, options));
     }
 
-    requestTaskAbort(progress: Progress, reason?: string) {
-        this.tasks.requestAbort(progress, reason);
-    }
-
     clear(resetViewportSettings = false) {
         if (resetViewportSettings) this.canvas3d?.setProps(DefaultCanvas3DParams);
         return PluginCommands.State.RemoveObject(this, { state: this.state.data, ref: StateTransform.RootRef });
@@ -247,7 +243,7 @@ export class PluginContext {
         this.canvas3d?.dispose();
         this.ev.dispose();
         this.state.dispose();
-        this.tasks.dispose();
+        this.managers.task.dispose();
         this.layout.dispose();
         this.helpers.substructureParent.dispose();
 

+ 12 - 2
src/mol-plugin/util/task-manager.ts

@@ -15,6 +15,7 @@ export { TaskManager };
 class TaskManager {
     private ev = RxEventHelper.create();
     private id = 0;
+    private runningTasks = new Set<number>();
     private abortRequests = new Map<number, string | undefined>();
     private currentContext: { ctx: RuntimeContext, refCount: number }[] = [];
 
@@ -50,9 +51,11 @@ class TaskManager {
         }
 
         try {
+            this.runningTasks.add(task.id);
             const ret = await ExecuteInContext(ctx.ctx, task);
             return ret;
         } finally {
+            this.runningTasks.delete(task.id);
             this.events.finished.next({ id });
             this.abortRequests.delete(task.id);
             ctx.refCount--;
@@ -60,8 +63,15 @@ class TaskManager {
         }
     }
 
-    requestAbort(progress: Progress, reason?: string) {
-        this.abortRequests.set(progress.root.progress.taskId, reason);
+    requestAbortAll(reason?: string) {
+        this.runningTasks.forEach(id => this.abortRequests.set(id, reason));
+    }
+
+    requestAbort(taskIdOrProgress: number | Progress, reason?: string) {
+        const id = typeof taskIdOrProgress === 'number'
+            ? taskIdOrProgress
+            : taskIdOrProgress.root.progress.taskId;
+        this.abortRequests.set(id, reason);
     }
 
     dispose() {