From b2a72301c260151664c35a952acc9171772b49ae Mon Sep 17 00:00:00 2001
From: David Sehnal <david.sehnal@gmail.com>
Date: Fri, 2 Nov 2018 13:14:14 +0100
Subject: [PATCH] mol-plugin: event helper

---
 src/mol-plugin/context.ts       | 14 ++++++++++++--
 src/mol-util/rx-event-helper.ts | 19 +++++++++++++++++++
 2 files changed, 31 insertions(+), 2 deletions(-)
 create mode 100644 src/mol-util/rx-event-helper.ts

diff --git a/src/mol-plugin/context.ts b/src/mol-plugin/context.ts
index 4aa5439e7..5355bfc35 100644
--- a/src/mol-plugin/context.ts
+++ b/src/mol-plugin/context.ts
@@ -7,10 +7,13 @@
 import { State, StateTree, StateSelection, Transformer } from 'mol-state';
 import Canvas3D from 'mol-canvas3d/canvas3d';
 import { StateTransforms } from './state/transforms';
-import { Subject } from 'rxjs';
 import { PluginStateObjects as SO } from './state/objects';
+import { RxEventHelper } from 'mol-util/rx-event-helper';
 
 export class PluginContext {
+    private disposed = false;
+    private _events = new RxEventHelper();
+
     state = {
         data: State.create(new SO.Root({ label: 'Root' }, { })),
         // behaviour: State,
@@ -19,7 +22,7 @@ export class PluginContext {
 
     // TODO: better events
     events = {
-        stateUpdated: new Subject<undefined>()
+        stateUpdated: this._events.create<undefined>()
     };
 
     canvas3d: Canvas3D;
@@ -36,6 +39,13 @@ export class PluginContext {
         }
     }
 
+    dispose() {
+        if (this.disposed) return;
+        this.canvas3d.dispose();
+        this._events.dispose();
+        this.disposed = true;
+    }
+
     _test_createState(url: string) {
         const b = StateTree.build(this.state.data.tree);
         const newTree = b.toRoot()
diff --git a/src/mol-util/rx-event-helper.ts b/src/mol-util/rx-event-helper.ts
new file mode 100644
index 000000000..4014f03a9
--- /dev/null
+++ b/src/mol-util/rx-event-helper.ts
@@ -0,0 +1,19 @@
+/**
+ * Copyright (c) 2018 mol* contributors, licensed under MIT, See LICENSE file for more info.
+ *
+ * @author David Sehnal <david.sehnal@gmail.com>
+ */
+
+import { Subject } from 'rxjs';
+
+export class RxEventHelper {
+    private _eventList: Subject<any>[] = [];
+    create<T>() {
+        const s = new Subject<T>();
+        this._eventList.push(s);
+        return s;
+    }
+    dispose() {
+        for (const e of this._eventList) e.complete();
+    }
+}
\ No newline at end of file
-- 
GitLab