forked from ColinTimBarndt/javascript-object-destructor
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
61 lines (54 loc) · 1.43 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/**
* @typedef {{
* deref: () => T|undefined;
* }} WeakRef<T>
* @template T
*/
/**
* @typedef {{
* new(obj: T) => WeakRef<T>;
* }} WeakRefConstructor<T>
* @template T
*/
/**
* @type {Map<WeakRef<Object>, () => void>}
*/
const destructors = new Map();
let destructionInterval = undefined;
/**
* @param {() => T} objCreator A function that creates an object.
* @param {() => void} objDestructor A function that is called if the object is garbage collected.
* @returns {T}
* @template T
*/
function addDestructor(objCreator, objDestructor) {
const sym = Symbol("destructor");
{
const obj = objCreator();
if (typeof obj !== "object")
throw new TypeError("The created object is not an object.");
const desObj = Object.freeze({ destructor: sym });
destructors.set(new WeakRef(desObj), objDestructor);
Object.defineProperty(obj, sym, { value: desObj, writable: false });
if (destructionInterval === undefined) {
destructionInterval = setInterval(() => {
destructors.forEach((des, ref) => {
if (ref.deref()) return;
// Garbage collected
destructors.delete(ref);
des();
});
if (destructors.size < 1) {
clearInterval(destructionInterval);
destructionInterval = undefined;
}
}, 500);
}
return obj;
}
}
if (typeof process === 'object' && typeof require === 'function') {
module.exports = {
addDestructor,
};
}