Skip to content

Commit

Permalink
Some fresh air
Browse files Browse the repository at this point in the history
  • Loading branch information
OEvgeny committed Dec 9, 2024
1 parent 12ab82c commit 1dc8d6d
Showing 1 changed file with 6 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,12 @@ export function createElementRegistryWithUpdater<T extends Element = HTMLElement
const updateCallback = useCallback(updateFn, deps);

const disposeCallback = useRef<() => void>();

disposeCallback.current = useMemo(() => {
disposeCallback.current?.();

updateCallbacks.add(updateCallback);

const localUpdaters = new Set([...elements].map(element => new ElementUpdater(element, updateCallback)));

updaters = updaters.union(localUpdaters);
Expand All @@ -62,18 +64,22 @@ export function createElementRegistryWithUpdater<T extends Element = HTMLElement

function removeFromRegistry(element: T) {
const disposeUpdaters = new Set([...updaters].filter(updater => updater.element === element));

disposeUpdaters.forEach(updater => updater.dispose());
updaters = updaters.difference(disposeUpdaters);

elements.delete(element);
}

function addToRegistry(element: T) {
if (elements.has(element)) {
removeFromRegistry(element);
}

updaters = updaters.union(
new Set([...updateCallbacks].map(updateCallback => new ElementUpdater(element, updateCallback)))
);

elements.add(element);
}

Expand Down

0 comments on commit 1dc8d6d

Please sign in to comment.