From 602ca31eb2480c6b86334e4d8608b0f74f95a39a Mon Sep 17 00:00:00 2001
From: David Sehnal <david.sehnal@gmail.com>
Date: Wed, 20 Feb 2019 17:32:52 +0100
Subject: [PATCH] mol-plugin: PluginComponent refactoring

---
 src/mol-plugin/component.ts               |  3 +--
 src/mol-plugin/layout.ts                  |  5 ++---
 src/mol-plugin/state.ts                   |  4 +---
 src/mol-plugin/state/animation/manager.ts |  4 ++--
 src/mol-plugin/state/camera.ts            |  5 ++---
 src/mol-plugin/state/snapshots.ts         | 25 +++++++++--------------
 src/mol-plugin/ui/state.tsx               |  2 +-
 7 files changed, 19 insertions(+), 29 deletions(-)

diff --git a/src/mol-plugin/component.ts b/src/mol-plugin/component.ts
index 3d15fcb18..b49f80c1b 100644
--- a/src/mol-plugin/component.ts
+++ b/src/mol-plugin/component.ts
@@ -4,7 +4,6 @@
  * @author David Sehnal <david.sehnal@gmail.com>
  */
 
-import { PluginContext } from './context';
 import { shallowMergeArray } from 'mol-util/object';
 import { RxEventHelper } from 'mol-util/rx-event-helper';
 
@@ -35,7 +34,7 @@ export class PluginComponent<State> {
         if (this._ev) this._ev.dispose();
     }
 
-    constructor(public context: PluginContext, initialState: State) {
+    constructor(initialState: State) {
         this._state = initialState;
     }
 }
\ No newline at end of file
diff --git a/src/mol-plugin/layout.ts b/src/mol-plugin/layout.ts
index f41fd2c3a..63b8f19be 100644
--- a/src/mol-plugin/layout.ts
+++ b/src/mol-plugin/layout.ts
@@ -42,7 +42,6 @@ interface RootState {
 }
 
 export class PluginLayout extends PluginComponent<PluginLayoutStateProps> {
-
     readonly events = {
         updated: this.ev()
     }
@@ -176,8 +175,8 @@ export class PluginLayout extends PluginComponent<PluginLayoutStateProps> {
         }
     }
 
-    constructor(context: PluginContext) {
-        super(context, { ...PD.getDefaultValues(PluginLayoutStateParams), ...context.spec.initialLayout });
+    constructor(private context: PluginContext) {
+        super({ ...PD.getDefaultValues(PluginLayoutStateParams), ...context.spec.initialLayout });
 
         PluginCommands.Layout.Update.subscribe(context, e => this.updateProps(e.state));
 
diff --git a/src/mol-plugin/state.ts b/src/mol-plugin/state.ts
index 60bc4a272..86d08ec73 100644
--- a/src/mol-plugin/state.ts
+++ b/src/mol-plugin/state.ts
@@ -22,8 +22,7 @@ class PluginState {
     readonly dataState: State;
     readonly behaviorState: State;
     readonly animation: PluginAnimationManager;
-    readonly cameraSnapshots: CameraSnapshotManager;
-
+    readonly cameraSnapshots = new CameraSnapshotManager();
     readonly snapshots = new PluginStateSnapshotManager();
 
     readonly behavior = {
@@ -89,7 +88,6 @@ class PluginState {
 
         this.behavior.currentObject.next(this.dataState.behaviors.currentObject.value);
 
-        this.cameraSnapshots = new CameraSnapshotManager(plugin);
         this.animation = new PluginAnimationManager(plugin);
     }
 }
diff --git a/src/mol-plugin/state/animation/manager.ts b/src/mol-plugin/state/animation/manager.ts
index 734ddf49a..fc83a7f44 100644
--- a/src/mol-plugin/state/animation/manager.ts
+++ b/src/mol-plugin/state/animation/manager.ts
@@ -162,8 +162,8 @@ class PluginAnimationManager extends PluginComponent<PluginAnimationManager.Stat
         requestAnimationFrame(this.animate);
     }
 
-    constructor(ctx: PluginContext) {
-        super(ctx, { params: { current: '' }, animationState: 'stopped' });
+    constructor(private context: PluginContext) {
+        super({ params: { current: '' }, animationState: 'stopped' });
     }
 }
 
diff --git a/src/mol-plugin/state/camera.ts b/src/mol-plugin/state/camera.ts
index 86709a7a1..830dbaf8d 100644
--- a/src/mol-plugin/state/camera.ts
+++ b/src/mol-plugin/state/camera.ts
@@ -8,7 +8,6 @@ import { Camera } from 'mol-canvas3d/camera';
 import { OrderedMap } from 'immutable';
 import { UUID } from 'mol-util';
 import { PluginComponent } from 'mol-plugin/component';
-import { PluginContext } from 'mol-plugin/context';
 
 export { CameraSnapshotManager }
 
@@ -53,8 +52,8 @@ class CameraSnapshotManager extends PluginComponent<{ entries: OrderedMap<string
         this.events.changed.next();
     }
 
-    constructor(ctx: PluginContext) {
-        super(ctx, { entries: OrderedMap<string, CameraSnapshotManager.Entry>() });
+    constructor() {
+        super({ entries: OrderedMap<string, CameraSnapshotManager.Entry>() });
     }
 }
 
diff --git a/src/mol-plugin/state/snapshots.ts b/src/mol-plugin/state/snapshots.ts
index 8d03ed5ae..d9f4152d2 100644
--- a/src/mol-plugin/state/snapshots.ts
+++ b/src/mol-plugin/state/snapshots.ts
@@ -6,44 +6,39 @@
 
 import { OrderedMap } from 'immutable';
 import { UUID } from 'mol-util';
-import { RxEventHelper } from 'mol-util/rx-event-helper';
 import { PluginState } from '../state';
+import { PluginComponent } from 'mol-plugin/component';
 
 export { PluginStateSnapshotManager }
 
-class PluginStateSnapshotManager {
-    private ev = RxEventHelper.create();
-    private _entries = OrderedMap<string, PluginStateSnapshotManager.Entry>().asMutable();
-
+class PluginStateSnapshotManager extends PluginComponent<{ entries: OrderedMap<string, PluginStateSnapshotManager.Entry> }> {
     readonly events = {
         changed: this.ev()
     };
 
-    get entries() { return this._entries; }
-
     getEntry(id: string) {
-        return this._entries.get(id);
+        return this.state.entries.get(id);
     }
 
     remove(id: string) {
-        if (!this._entries.has(id)) return;
-        this._entries.delete(id);
+        if (!this.state.entries.has(id)) return;
+        this.updateState({ entries: this.state.entries.delete(id) });
         this.events.changed.next();
     }
 
     add(e: PluginStateSnapshotManager.Entry) {
-        this._entries.set(e.id, e);
+        this.updateState({ entries: this.state.entries.set(e.id, e) });
         this.events.changed.next();
     }
 
     clear() {
-        if (this._entries.size === 0) return;
-        this._entries = OrderedMap<string, PluginStateSnapshotManager.Entry>().asMutable();
+        if (this.state.entries.size === 0) return;
+        this.updateState({ entries: OrderedMap<string, PluginStateSnapshotManager.Entry>() });
         this.events.changed.next();
     }
 
-    dispose() {
-        this.ev.dispose();
+    constructor() {
+        super({ entries: OrderedMap<string, PluginStateSnapshotManager.Entry>() });
     }
 }
 
diff --git a/src/mol-plugin/ui/state.tsx b/src/mol-plugin/ui/state.tsx
index ed8c45d00..6e13eb42e 100644
--- a/src/mol-plugin/ui/state.tsx
+++ b/src/mol-plugin/ui/state.tsx
@@ -110,7 +110,7 @@ class LocalStateSnapshotList extends PluginUIComponent<{ }, { }> {
 
     render() {
         return <ul style={{ listStyle: 'none' }} className='msp-state-list'>
-            {this.plugin.state.snapshots.entries.valueSeq().map(e =><li key={e!.id}>
+            {this.plugin.state.snapshots.state.entries.valueSeq().map(e =><li key={e!.id}>
                 <button className='msp-btn msp-btn-block msp-form-control' onClick={this.apply(e!.id)}>{e!.name || e!.timestamp} <small>{e!.description}</small></button>
                 <button onClick={this.remove(e!.id)} className='msp-btn msp-btn-link msp-state-list-remove-button'>
                     <span className='msp-icon msp-icon-remove' />
-- 
GitLab