base-store.ts fix

Signed-off-by: Roman <ixrock@gmail.com>
This commit is contained in:
Roman authored and Sebastian Malton committed 2020-07-27 09:14:16 -04:00
1 parent 65f757bade
commit 5026072dfa
1 file changed
+10 -7
+10 -7
View File
@@ -1,7 +1,7 @@
import path from "path" import path from "path"
import Config from "conf" import Config from "conf"
import { Options as ConfOptions } from "conf/dist/source/types" import { Options as ConfOptions } from "conf/dist/source/types"
import { app, ipcMain, ipcRenderer, remote } from "electron" import { app, ipcMain, IpcMainEvent, ipcRenderer, IpcRendererEvent, remote } from "electron"
import { action, observable, reaction, runInAction, toJS, when } from "mobx"; import { action, observable, reaction, runInAction, toJS, when } from "mobx";
import Singleton from "./utils/singleton"; import Singleton from "./utils/singleton";
import { getAppVersion } from "./utils/app-version"; import { getAppVersion } from "./utils/app-version";
@@ -80,18 +80,20 @@ export class BaseStore<T = any> extends Singleton {
reaction(() => this.toJSON(), model => this.onModelChange(model)), reaction(() => this.toJSON(), model => this.onModelChange(model)),
); );
if (ipcMain) { if (ipcMain) {
ipcMain.on(this.syncChannel, (event, model: T) => { const callback = (event: IpcMainEvent, model: T) => {
logger.debug(`[STORE]: SYNC ${this.name} from renderer`, { model }); logger.debug(`[STORE]: SYNC ${this.name} from renderer`, { model });
this.onSync(model); this.onSync(model);
}); };
this.syncDisposers.push(() => ipcMain.removeAllListeners(this.syncChannel)); ipcMain.on(this.syncChannel, callback);
this.syncDisposers.push(() => ipcMain.off(this.syncChannel, callback));
} }
if (ipcRenderer) { if (ipcRenderer) {
ipcRenderer.on(this.syncChannel, (event, model: T) => { const callback = (event: IpcRendererEvent, model: T) => {
logger.debug(`[STORE]: SYNC ${this.name} from main`, { model }); logger.debug(`[STORE]: SYNC ${this.name} from main`, { model });
this.onSync(model); this.onSync(model);
}); };
this.syncDisposers.push(() => ipcRenderer.removeAllListeners(this.syncChannel)); ipcRenderer.on(this.syncChannel, callback);
this.syncDisposers.push(() => ipcRenderer.off(this.syncChannel, callback));
} }
} }
@@ -109,6 +111,7 @@ export class BaseStore<T = any> extends Singleton {
} }
protected onSync(model: T) { protected onSync(model: T) {
// todo: use "resourceVersion" if merge required (to avoid equality checks => better performance)
if (!isEqual(this.toJSON(), model)) { if (!isEqual(this.toJSON(), model)) {
this.fromStore(model); this.fromStore(model);
} }