Browse Source

fix: notify ValueChange listener after updating the store (#650)

tophf 6 years ago
parent
commit
59d258824d
2 changed files with 10 additions and 8 deletions
  1. 6 4
      src/injected/web/gm-api.js
  2. 4 4
      src/injected/web/gm-values.js

+ 6 - 4
src/injected/web/gm-api.js

@@ -23,10 +23,11 @@ export function createGmApiProps() {
     GM_deleteValue(key) {
       const { id } = this;
       const values = loadValues(id);
+      const oldRaw = values[key];
+      delete values[key];
       dumpValue({
-        id, key, oldRaw: values[key],
+        id, key, oldRaw,
       });
-      delete values[key];
     },
     GM_getValue(key, def) {
       const raw = loadValues(this.id)[key];
@@ -40,10 +41,11 @@ export function createGmApiProps() {
       const dumped = jsonDump(val);
       const raw = dumped ? `o${dumped}` : null;
       const values = loadValues(id);
+      const oldRaw = values[key];
+      values[key] = raw;
       dumpValue({
-        id, key, val, raw, oldRaw: values[key],
+        id, key, val, raw, oldRaw,
       });
-      values[key] = raw;
     },
     /**
      * @callback GMValueChangeListener

+ 4 - 4
src/injected/web/gm-values.js

@@ -25,9 +25,10 @@ const dataDecoders = {
 bridge.addHandlers({
   UpdatedValues(updates) {
     objectKeys(updates)::forEach((id) => {
-      if (id in store.values) {
-        if (id in changeHooks) changedRemotely(id, updates);
+      const oldData = store.values[id];
+      if (oldData) {
         store.values[id] = updates[id];
+        if (id in changeHooks) changedRemotely(id, oldData, updates);
       }
     });
   },
@@ -74,9 +75,8 @@ function changedLocally(change) {
   }
 }
 
-function changedRemotely(id, updates) {
+function changedRemotely(id, oldData, updates) {
   const data = updates[id];
-  const oldData = loadValues(id);
   const keyHooks = changeHooks[id];
   // the remote id is a string, but all local data structures use a number
   id = +id;