electron-store icon indicating copy to clipboard operation
electron-store copied to clipboard

Watch for Changes not working across processes

Open sachinjain024 opened this issue 4 years ago • 3 comments

Inside the main process, I am trying to listen to the changes done in Renderer Process (Preload script).

Main

const Store = require('electron-store');
const store = new Store({ watch: true });

const browserWindow = new BrowserWindow({
    width: 800,
    height: 600,
    show: false,
    webPreferences: {
      preload: MAIN_WINDOW_PRELOAD_WEBPACK_ENTRY,
      nodeIntegration: false,
      contextIsolation: false,
      enableRemoteModule: true
    }
  });

browserWindow.once('ready-to-show', () => {
    browserWindow.show();
    browserWindow.webContents.send('test-event', {
      'foo': 'bar'
    });

    console.log('Setting up onDidAnyChange listener');
    store.onDidAnyChange(function(newValue, oldValue) {
      console.log('newValue:', newValue);
      console.log('oldValue:', oldValue);
    });

    console.log('Setting up onDidChange listener');
    store.onDidChange('a', function(newValue, oldValue) {
      console.log('newValue:', newValue);
      console.log('oldValue:', oldValue);
    });
  });

Browser Window (preload script)

const Store = require('electron-store');
const store = new Store({ watch: true });

store.set({''a': 1});
store.set({''a': 2});
store.set({''a': 5});

Even tried adding & changing the values from the Renderer Window Chrome Dev console. Am I missing something here or Is it not supposed to happen this way?

Issue is Callbacks for onDidChange and onDidAnyChange not firing at all.

sachinjain024 avatar Feb 10 '21 01:02 sachinjain024

Sounds related to https://github.com/sindresorhus/electron-store/issues/93.

sindresorhus avatar Feb 10 '21 06:02 sindresorhus

@sindresorhus No, I don't think this is related to #93. Here's the difference, In #93 Issue, onDidChange was getting fired at least once but in this case, onDidChange is not getting fired at all when changes are done from the Renderer Process and onDidChange is applied in the main process.

However, if I apply the onDidChange listener in Main and make the change in the main process, then it does fire exactly once which is understandable as mentioned in #93.

sachinjain024 avatar Feb 10 '21 09:02 sachinjain024

I have the same problem, but it's already here https://github.com/sindresorhus/conf/issues/108

my workaround:
after each store.set() it sends messages to other processes ( through IPC ), and they call store.events.emit('change')

martinboksa avatar Mar 12 '21 08:03 martinboksa