From 3ac6678557ee5ec0f287cb7d8c9552aec281486c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Loi=CC=88c=20Correnson?= <loic.correnson@cea.fr> Date: Fri, 28 Aug 2020 17:10:15 +0200 Subject: [PATCH] [dome] fix settings lifecycle --- ivette/src/dome/src/renderer/data/settings.ts | 39 +++++-------------- .../src/dome/src/renderer/layout/splitters.js | 2 +- 2 files changed, 11 insertions(+), 30 deletions(-) diff --git a/ivette/src/dome/src/renderer/data/settings.ts b/ivette/src/dome/src/renderer/data/settings.ts index 7d39c457339..506a36557f9 100644 --- a/ivette/src/dome/src/renderer/data/settings.ts +++ b/ivette/src/dome/src/renderer/data/settings.ts @@ -12,7 +12,7 @@ import React from 'react'; import { ipcRenderer } from 'electron'; import { debounce } from 'lodash'; import isEqual from 'react-fast-compare'; -import { DEVEL, emitter as SysEmitter } from 'dome/misc/system'; +import { emitter as SysEmitter } from 'dome/misc/system'; import * as JSON from './json'; import type { State } from './states'; @@ -115,6 +115,7 @@ export class GObject<A extends JSON.json> extends GlobalSettings<A> { // --- Generic Settings (private) // -------------------------------------------------------------------------- +type store = { [key: string]: JSON.json }; type patch = { key: string; value: JSON.json }; type driver = { evt: string; ipc: string; broadcast: boolean }; @@ -177,14 +178,12 @@ class Driver { // --- Initial Data - sync(data: patch[]) { + sync(data: store) { this.fire.cancel(); this.store.clear(); this.diffs.clear(); const m = this.store; - data.forEach(({ key, value }) => { - m.set(key, value); - }); + Object.keys(data).forEach((k) => { m.set(k, data[k]); }); SysEmitter.emit(this.evt); } @@ -215,25 +214,11 @@ class Driver { // --- Generic Settings Hook // -------------------------------------------------------------------------- -const keys = new Set<string>(); - function useSettings<A>( S: Settings<A>, D: Driver, K?: string, ): State<A> { - // Check for unique key - React.useEffect(() => { - if (K) { - if (keys.has(K) && DEVEL) - console.error('[Dome.settings] Duplicate key', K); - else { - keys.add(K); - return () => { keys.delete(K); }; - } - } - return undefined; - }); // Load value const loader = () => ( JSON.jCatch(S.decoder, S.defaultValue)(D.load(K)) @@ -354,7 +339,7 @@ export function offWindowSettings(callback: () => void) { const GlobalSettingsDriver = new Driver({ evt: 'dome.settings.global', - ipc: 'dome.ipc.settings.window', + ipc: 'dome.ipc.settings.global', broadcast: true, }); @@ -388,15 +373,11 @@ export const global = GlobalSettingsDriver.evt; /* @ internal */ export function synchronize() { - ipcRenderer.sendSync( - 'dome.ipc.settings.sync', - (_event: string, data: any) => { - const globals: patch[] = data.globals ?? []; - GlobalSettingsDriver.sync(globals); - const settings: patch[] = data.settings ?? []; - WindowSettingsDriver.sync(settings); - }, - ); + const data = ipcRenderer.sendSync('dome.ipc.settings.sync'); + const globals: store = data.store ?? {}; + GlobalSettingsDriver.sync(globals); + const settings: store = data.settings ?? {}; + WindowSettingsDriver.sync(settings); } // -------------------------------------------------------------------------- diff --git a/ivette/src/dome/src/renderer/layout/splitters.js b/ivette/src/dome/src/renderer/layout/splitters.js index c335ebcbb5a..eca1eca4322 100644 --- a/ivette/src/dome/src/renderer/layout/splitters.js +++ b/ivette/src/dome/src/renderer/layout/splitters.js @@ -197,7 +197,7 @@ export class Splitter extends React.Component { } handleReload() { - if (this.refs.container && this.refs.splitter) { + if (this.refs && this.refs.container && this.refs.splitter) { const container = this.refs.container.getBoundingClientRect() ; const dimension = this.lr ? container.width : container.height ; this.range = dimension - this.margin ; -- GitLab