nathanbuchar / electron-settings

📝 A simple persistent user settings framework for Electron.
https://electron-settings.js.org
MIT License
817 stars 60 forks source link

time delay in .get() ? #113

Closed mbykov closed 5 years ago

mbykov commented 5 years ago

.get() does not work for me.

let state = settings.get('state') log('STATE', state) gives {},

end when I click on it, shows correct values. But

log('FOO', state.foo) gives undefined, and let foo = settings.get('state.foo') gives undefined

Linux, Electron 4.1.1, "electron-settings": "^3.2.0"

nathanbuchar commented 5 years ago

Electron Settings is synchronous, so there shouldn't be any race conditions here. Can you run the following for me and tell me the output?

console.log(settings.get('_test'));
settings.set('_test', { foo: 'bar' });
console.log(settings.get('_test'));
settings.delete('_test');
console.log(settings.get('_test'));

Thanks :)

nathanbuchar commented 5 years ago

Oh I see, you're accessing state as if it were dynamic. The value of state will NOT change if you update settings via settings.set(). Electron-settings does not maintain an internal cache (for a number of reasons, the main being that Electron can spawn multiple processes with their own dedicated memory). Electron-settings must read from the settings file on disk. If you update the settings, you need to do settings.get().

settings.get() returns the settings object at the single point in time; it will not update it if the settings change. When attempting to read the settings, always use settings.get()

mbykov commented 5 years ago

But how I should do set, then get?

console.log('get_test',settings.get('_test')); settings.set('_test', { foo: 'bar' }); console.log('get_test', settings.get('_test')); console.log('get_test2', settings.get('_test')); settings.delete('_test'); console.log('get_test', settings.get('_test'));

Now I've got, I already click on second {} - see picture also

get_test undefined get_test {} get_test2 {}foo: (...)get foo: () => {…}set foo: (value) => {…}proto: Object get_test undefined set_get

mbykov commented 5 years ago

Oh, I see. I can not use E.S. as internal cache at all, only after restart. Well, ok, thank you!