File size: 472 Bytes
8a37e0a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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]);
}