From 0d12a9e118c1c4faf9c2a1d313e818c7958abe9a Mon Sep 17 00:00:00 2001
From: David Sehnal <dsehnal@users.noreply.github.com>
Date: Thu, 27 Apr 2023 12:34:44 +0200
Subject: [PATCH] PluginState.setSnapshot fix (#805)

---
 CHANGELOG.md                                        | 1 +
 src/mol-plugin-state/manager/structure/component.ts | 8 +++++++-
 src/mol-plugin/state.ts                             | 2 +-
 3 files changed, 9 insertions(+), 2 deletions(-)

diff --git a/CHANGELOG.md b/CHANGELOG.md
index 45a70d71a..e648baa5f 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -8,6 +8,7 @@ Note that since we don't clearly distinguish between a public and private interf
 
 - Add a uniform color theme for NtC tube that still paints residue and segment dividers in a different color
 - Fix bond assignments `struct_conn` records referencing waters
+- Fix `PluginState.setSnapshot` triggering unnecessary state updates
 
 ## [v3.34.0] - 2023-04-16
 
diff --git a/src/mol-plugin-state/manager/structure/component.ts b/src/mol-plugin-state/manager/structure/component.ts
index 5c72786cf..08233712b 100644
--- a/src/mol-plugin-state/manager/structure/component.ts
+++ b/src/mol-plugin-state/manager/structure/component.ts
@@ -1,5 +1,5 @@
 /**
- * Copyright (c) 2019-2022 mol* contributors, licensed under MIT, See LICENSE file for more info.
+ * Copyright (c) 2019-2023 mol* contributors, licensed under MIT, See LICENSE file for more info.
  *
  * @author David Sehnal <david.sehnal@gmail.com>
  * @author Alexander Rose <alexander.rose@weirdbyte.de>
@@ -54,6 +54,12 @@ class StructureComponentManager extends StatefulPluginComponent<StructureCompone
         return this.currentStructures[0];
     }
 
+    // To be used only from PluginState.setSnapshot
+    _setSnapshotState(options: StructureComponentManager.Options) {
+        this.updateState({ options });
+        this.events.optionsUpdated.next(void 0);
+    }
+
     async setOptions(options: StructureComponentManager.Options) {
         const interactionChanged = options.interactions !== this.state.options.interactions;
         this.updateState({ options });
diff --git a/src/mol-plugin/state.ts b/src/mol-plugin/state.ts
index e1aa41abf..b5853e996 100644
--- a/src/mol-plugin/state.ts
+++ b/src/mol-plugin/state.ts
@@ -76,7 +76,7 @@ class PluginState extends PluginComponent {
         await this.animation.stop();
 
         // this needs to go 1st since these changes are already baked into the behavior and data state
-        if (snapshot.structureComponentManager?.options) await this.plugin.managers.structure.component.setOptions(snapshot.structureComponentManager?.options);
+        if (snapshot.structureComponentManager?.options) this.plugin.managers.structure.component._setSnapshotState(snapshot.structureComponentManager?.options);
         if (snapshot.behaviour) await this.plugin.runTask(this.behaviors.setSnapshot(snapshot.behaviour));
         if (snapshot.data) await this.plugin.runTask(this.data.setSnapshot(snapshot.data));
         if (snapshot.canvas3d?.props) {
-- 
GitLab