InvokeAI/invokeai/frontend/web/src/common/hooks/useAssertSingleton.ts
psychedelicious c296ae8cfe feat(ui): add useAssertSingleton hook
Use this to enforce singleton components and hooks.
2024-07-24 14:10:16 +10:00

19 lines
472 B
TypeScript

import { useEffect } from 'react';
import { assert } from 'tsafe';
const IDS = new Set<string>();
/**
* Asserts that there is only one instance of a singleton entity. It can be a hook or a component.
* @param id The ID of the singleton entity.
*/
export function useAssertSingleton(id: string) {
useEffect(() => {
assert(!IDS.has(id), `There should be only one instance of ${id}`);
IDS.add(id);
return () => {
IDS.delete(id);
};
}, [id]);
}