|
1 | 1 | import { NUMBER } from 'proxy-target/types';
|
2 | 2 |
|
3 |
| -let uid = 0; |
4 |
| -const ids = new Map; |
5 |
| -const values = new Map; |
| 3 | +export const create = () => { |
| 4 | + const ids = new Map; |
| 5 | + const values = new Map; |
| 6 | + let uid = 0; |
| 7 | + return { |
| 8 | + /** |
| 9 | + * Remove by id or value any previously stored reference. |
| 10 | + * @param {number | unknown} id the held value by id or the value itself. |
| 11 | + * @returns {boolean} `true` if the operation was successful, `false` otherwise. |
| 12 | + */ |
| 13 | + drop: id => { |
| 14 | + const [a, b] = typeof id === NUMBER ? [values, ids] : [ids, values]; |
| 15 | + const had = a.has(id); |
| 16 | + if (had) { |
| 17 | + b.delete(a.get(id)); |
| 18 | + a.delete(id); |
| 19 | + } |
| 20 | + return had; |
| 21 | + }, |
6 | 22 |
|
7 |
| -/** |
8 |
| - * Remove by id or value any previously stored reference. |
9 |
| - * @param {number | unknown} id the held value by id or the value itself. |
10 |
| - * @returns {boolean} `true` if the operation was successful, `false` otherwise. |
11 |
| - */ |
12 |
| -export const drop = id => { |
13 |
| - const [a, b] = typeof id === NUMBER ? [values, ids] : [ids, values]; |
14 |
| - const had = a.has(id); |
15 |
| - if (had) { |
16 |
| - b.delete(a.get(id)); |
17 |
| - a.delete(id); |
18 |
| - } |
19 |
| - return had; |
20 |
| -}; |
21 |
| - |
22 |
| -/** |
23 |
| - * Return the held value reference by its unique identifier. |
24 |
| - * @param {number} id the unique identifier for the value reference. |
25 |
| - * @returns {unknown} the related value / reference or undefined. |
26 |
| - */ |
27 |
| -export const get = id => values.get(id); |
| 23 | + /** |
| 24 | + * Return the held value reference by its unique identifier. |
| 25 | + * @param {number} id the unique identifier for the value reference. |
| 26 | + * @returns {unknown} the related value / reference or undefined. |
| 27 | + */ |
| 28 | + get: id => values.get(id), |
28 | 29 |
|
29 |
| -/** |
30 |
| - * Create once a unique number id for a generic value reference. |
31 |
| - * @param {unknown} value a reference used to create a unique identifier. |
32 |
| - * @returns {number} a unique identifier for that reference. |
33 |
| - */ |
34 |
| -export const hold = value => { |
35 |
| - if (!ids.has(value)) { |
36 |
| - let id; |
37 |
| - // a bit apocalyptic scenario but if this thread runs forever |
38 |
| - // and the id does a whole int32 roundtrip we might have still |
39 |
| - // some reference dangling around |
40 |
| - while (/* c8 ignore next */ values.has(id = uid++)); |
41 |
| - ids.set(value, id); |
42 |
| - values.set(id, value); |
43 |
| - } |
44 |
| - return ids.get(value); |
| 30 | + /** |
| 31 | + * Create once a unique number id for a generic value reference. |
| 32 | + * @param {unknown} value a reference used to create a unique identifier. |
| 33 | + * @returns {number} a unique identifier for that reference. |
| 34 | + */ |
| 35 | + hold: value => { |
| 36 | + if (!ids.has(value)) { |
| 37 | + let id; |
| 38 | + // a bit apocalyptic scenario but if this thread runs forever |
| 39 | + // and the id does a whole int32 roundtrip we might have still |
| 40 | + // some reference dangling around |
| 41 | + while (/* c8 ignore next */ values.has(id = uid++)); |
| 42 | + ids.set(value, id); |
| 43 | + values.set(id, value); |
| 44 | + } |
| 45 | + return ids.get(value); |
| 46 | + }, |
| 47 | + }; |
45 | 48 | };
|
| 49 | + |
| 50 | +// globally shared heap |
| 51 | +const { drop, get, hold } = create(); |
| 52 | +export { drop, get, hold }; |
0 commit comments