diff --git a/ivette/src/dome/renderer/data/settings.ts b/ivette/src/dome/renderer/data/settings.ts index 4421943d26bd8d03cbb1626e8743950bd3b95566..585fe1405557d4015898dcea5ca6bd0428bd2cfa 100644 --- a/ivette/src/dome/renderer/data/settings.ts +++ b/ivette/src/dome/renderer/data/settings.ts @@ -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]; } diff --git a/ivette/src/dome/renderer/dome.tsx b/ivette/src/dome/renderer/dome.tsx index 1770a29b7f2d5412424b59ab9fcdcd04940ed798..2b083a73ee982c656767deb4ec20b2d9829b2af2 100644 --- a/ivette/src/dome/renderer/dome.tsx +++ b/ivette/src/dome/renderer/dome.tsx @@ -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. */