Skip to content
Snippets Groups Projects
Commit 3ac66785 authored by Loïc Correnson's avatar Loïc Correnson
Browse files

[dome] fix settings lifecycle

parent a0b6b3cc
No related branches found
No related tags found
No related merge requests found
...@@ -12,7 +12,7 @@ import React from 'react'; ...@@ -12,7 +12,7 @@ import React from 'react';
import { ipcRenderer } from 'electron'; import { ipcRenderer } from 'electron';
import { debounce } from 'lodash'; import { debounce } from 'lodash';
import isEqual from 'react-fast-compare'; 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 * as JSON from './json';
import type { State } from './states'; import type { State } from './states';
...@@ -115,6 +115,7 @@ export class GObject<A extends JSON.json> extends GlobalSettings<A> { ...@@ -115,6 +115,7 @@ export class GObject<A extends JSON.json> extends GlobalSettings<A> {
// --- Generic Settings (private) // --- Generic Settings (private)
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
type store = { [key: string]: JSON.json };
type patch = { key: string; value: JSON.json }; type patch = { key: string; value: JSON.json };
type driver = { evt: string; ipc: string; broadcast: boolean }; type driver = { evt: string; ipc: string; broadcast: boolean };
...@@ -177,14 +178,12 @@ class Driver { ...@@ -177,14 +178,12 @@ class Driver {
// --- Initial Data // --- Initial Data
sync(data: patch[]) { sync(data: store) {
this.fire.cancel(); this.fire.cancel();
this.store.clear(); this.store.clear();
this.diffs.clear(); this.diffs.clear();
const m = this.store; const m = this.store;
data.forEach(({ key, value }) => { Object.keys(data).forEach((k) => { m.set(k, data[k]); });
m.set(key, value);
});
SysEmitter.emit(this.evt); SysEmitter.emit(this.evt);
} }
...@@ -215,25 +214,11 @@ class Driver { ...@@ -215,25 +214,11 @@ class Driver {
// --- Generic Settings Hook // --- Generic Settings Hook
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
const keys = new Set<string>();
function useSettings<A>( function useSettings<A>(
S: Settings<A>, S: Settings<A>,
D: Driver, D: Driver,
K?: string, K?: string,
): State<A> { ): 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 // Load value
const loader = () => ( const loader = () => (
JSON.jCatch(S.decoder, S.defaultValue)(D.load(K)) JSON.jCatch(S.decoder, S.defaultValue)(D.load(K))
...@@ -354,7 +339,7 @@ export function offWindowSettings(callback: () => void) { ...@@ -354,7 +339,7 @@ export function offWindowSettings(callback: () => void) {
const GlobalSettingsDriver = new Driver({ const GlobalSettingsDriver = new Driver({
evt: 'dome.settings.global', evt: 'dome.settings.global',
ipc: 'dome.ipc.settings.window', ipc: 'dome.ipc.settings.global',
broadcast: true, broadcast: true,
}); });
...@@ -388,15 +373,11 @@ export const global = GlobalSettingsDriver.evt; ...@@ -388,15 +373,11 @@ export const global = GlobalSettingsDriver.evt;
/* @ internal */ /* @ internal */
export function synchronize() { export function synchronize() {
ipcRenderer.sendSync( const data = ipcRenderer.sendSync('dome.ipc.settings.sync');
'dome.ipc.settings.sync', const globals: store = data.store ?? {};
(_event: string, data: any) => { GlobalSettingsDriver.sync(globals);
const globals: patch[] = data.globals ?? []; const settings: store = data.settings ?? {};
GlobalSettingsDriver.sync(globals); WindowSettingsDriver.sync(settings);
const settings: patch[] = data.settings ?? [];
WindowSettingsDriver.sync(settings);
},
);
} }
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
...@@ -197,7 +197,7 @@ export class Splitter extends React.Component { ...@@ -197,7 +197,7 @@ export class Splitter extends React.Component {
} }
handleReload() { handleReload() {
if (this.refs.container && this.refs.splitter) { if (this.refs && this.refs.container && this.refs.splitter) {
const container = this.refs.container.getBoundingClientRect() ; const container = this.refs.container.getBoundingClientRect() ;
const dimension = this.lr ? container.width : container.height ; const dimension = this.lr ? container.width : container.height ;
this.range = dimension - this.margin ; this.range = dimension - this.margin ;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment