mirror of
https://github.com/zadam/trilium.git
synced 2025-10-22 16:18:54 +02:00
fix(react/settings): event leak in useOption
This commit is contained in:
parent
d2bc72d54f
commit
e7b448e2bc
@ -1,4 +1,4 @@
|
|||||||
import { type Dispatch, type StateUpdater, useContext, useEffect, useMemo, useRef, useState } from "preact/hooks";
|
import { useCallback, useContext, useEffect, useMemo, useRef, useState } from "preact/hooks";
|
||||||
import { EventData, EventNames } from "../../components/app_context";
|
import { EventData, EventNames } from "../../components/app_context";
|
||||||
import { ParentComponent } from "./ReactBasicWidget";
|
import { ParentComponent } from "./ReactBasicWidget";
|
||||||
import SpacedUpdate from "../../services/spaced_update";
|
import SpacedUpdate from "../../services/spaced_update";
|
||||||
@ -73,20 +73,22 @@ export function useTriliumOption(name: OptionNames, needsRefresh?: boolean): [st
|
|||||||
const initialValue = options.get(name);
|
const initialValue = options.get(name);
|
||||||
const [ value, setValue ] = useState(initialValue);
|
const [ value, setValue ] = useState(initialValue);
|
||||||
|
|
||||||
async function wrappedSetValue(newValue: string) {
|
const wrappedSetValue = useMemo(() => {
|
||||||
|
return async (newValue: string) => {
|
||||||
await options.save(name, newValue);
|
await options.save(name, newValue);
|
||||||
|
|
||||||
if (needsRefresh) {
|
if (needsRefresh) {
|
||||||
reloadFrontendApp(`option change: ${name}`);
|
reloadFrontendApp(`option change: ${name}`);
|
||||||
}
|
}
|
||||||
};
|
}
|
||||||
|
}, [ name, needsRefresh ]);
|
||||||
|
|
||||||
useTriliumEvent("entitiesReloaded", ({ loadResults }) => {
|
useTriliumEvent("entitiesReloaded", useCallback(({ loadResults }) => {
|
||||||
if (loadResults.getOptionNames().includes(name)) {
|
if (loadResults.getOptionNames().includes(name)) {
|
||||||
const newValue = options.get(name);
|
const newValue = options.get(name);
|
||||||
setValue(newValue);
|
setValue(newValue);
|
||||||
}
|
}
|
||||||
});
|
}, [ name ]));
|
||||||
|
|
||||||
return [
|
return [
|
||||||
value,
|
value,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user