electron-userland / electron-json-storage

:package: Easily write and read user settings in Electron apps
1.43k stars 79 forks source link

storage.set does not work in reducer #111

Open Xsaven opened 6 years ago

Xsaven commented 6 years ago

i have a react reducer:

const fs = window.require('fs');
const path = window.require('path');
const settings = window.require('electron-settings');
const storage = window.require('electron-json-storage');
let server = settings.get('server') || false;
storage.setDataPath(path.join(server, '.lia'));

export default (state = (storage.get('dummy_list') || []), action) => {
  if(action.type=='DUMMY_ADD') {
    state[action.data.name] = action.data;
    console.log(state);
    storage.set('dummy_list', state, (error)=>{ console.log(error); });
    return state;
  }else
    return state;
};

and when this part is executed: storage.set('dummy_list', state, (error)=>{ console.log(error); }); ... nothing happens, the file creates, empty, but does not store the data there.

jviotti commented 6 years ago

@Xsaven Can you provide a runnable example that I can use to reproduce?

Based on what I can see, though, you shouldn't be running an async function (like storage.set) in a sync reducer, since you're not waiting for this module to write the data before returning. If your reducer is called often enough, you could be overriding yourself each time.