Skip to content
Snippets Groups Projects
Commit 5420e310 authored by Loïc Correnson's avatar Loïc Correnson Committed by David Bühler
Browse files

[dome] fix settings hooks

parent e9d8ccd7
No related branches found
No related tags found
No related merge requests found
......@@ -248,26 +248,29 @@ function useSettings<A>(
D: Driver,
K?: string,
): State<A> {
// Load value
const loader = () => (
// Local State
const [value, setValue] = React.useState<A>(() => (
JSON.jCatch(S.decoder, S.defaultValue)(D.load(K))
);
// Local state
const [value, setValue] = React.useState<A>(loader);
// Emit update event
));
// Foreign Settings Update
React.useEffect(() => {
const event = D.evt;
const callback = () => setValue(loader());
SysEmitter.on(event, callback);
return () => { SysEmitter.off(event, callback); };
const onUpdate = () => {
const fromSettings = JSON.jCatch(S.decoder, undefined)(D.load(K));
if (fromSettings !== undefined)
setValue(fromSettings);
};
SysEmitter.on(event, onUpdate);
return () => { SysEmitter.off(event, onUpdate); };
});
// Updates
// Hooked Settings Update
const updateValue = React.useCallback((newValue: A) => {
if (!isEqual(value, newValue)) {
setValue(newValue);
if (K) D.save(K, S.encoder(newValue));
}
}, [S, D, K, value]);
// Hook
return [value, updateValue];
}
......
......@@ -173,7 +173,7 @@ export function useEvent<A>(
return () => evt.off(callback);
}
return undefined;
});
}, [evt, callback]);
}
// --------------------------------------------------------------------------
......@@ -704,11 +704,7 @@ export function useFlipSettings(
const [state, setState] = Settings.useWindowSettings(
key, Json.jBoolean, defaultValue,
);
const flipState = React.useCallback(
() => setState(!state),
[state, setState],
);
return [state, flipState];
return [state, () => setState(!state)];
}
/** Number window settings helper. Default is `0` unless specified. */
......
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