mirror of
https://github.com/invoke-ai/InvokeAI
synced 2024-08-30 20:32:17 +00:00
feat(ui): add useAssertSingleton hook
Use this to enforce singleton components and hooks.
This commit is contained in:
parent
17493f4ae0
commit
c296ae8cfe
18
invokeai/frontend/web/src/common/hooks/useAssertSingleton.ts
Normal file
18
invokeai/frontend/web/src/common/hooks/useAssertSingleton.ts
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
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]);
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user