UNPKG

3.45 kBJavaScriptView Raw
1export var REGISTRY_FINALIZE_AFTER = 10000;
2export var REGISTRY_SWEEP_INTERVAL = 10000;
3var TimerBasedFinalizationRegistry = /** @class */ (function () {
4 function TimerBasedFinalizationRegistry(finalize) {
5 var _this = this;
6 Object.defineProperty(this, "finalize", {
7 enumerable: true,
8 configurable: true,
9 writable: true,
10 value: finalize
11 });
12 Object.defineProperty(this, "registrations", {
13 enumerable: true,
14 configurable: true,
15 writable: true,
16 value: new Map()
17 });
18 Object.defineProperty(this, "sweepTimeout", {
19 enumerable: true,
20 configurable: true,
21 writable: true,
22 value: void 0
23 });
24 // Bound so it can be used directly as setTimeout callback.
25 Object.defineProperty(this, "sweep", {
26 enumerable: true,
27 configurable: true,
28 writable: true,
29 value: function (maxAge) {
30 if (maxAge === void 0) { maxAge = REGISTRY_FINALIZE_AFTER; }
31 // cancel timeout so we can force sweep anytime
32 clearTimeout(_this.sweepTimeout);
33 _this.sweepTimeout = undefined;
34 var now = Date.now();
35 _this.registrations.forEach(function (registration, token) {
36 if (now - registration.registeredAt >= maxAge) {
37 _this.finalize(registration.value);
38 _this.registrations.delete(token);
39 }
40 });
41 if (_this.registrations.size > 0) {
42 _this.scheduleSweep();
43 }
44 }
45 });
46 // Bound so it can be exported directly as clearTimers test utility.
47 Object.defineProperty(this, "finalizeAllImmediately", {
48 enumerable: true,
49 configurable: true,
50 writable: true,
51 value: function () {
52 _this.sweep(0);
53 }
54 });
55 }
56 // Token is actually required with this impl
57 Object.defineProperty(TimerBasedFinalizationRegistry.prototype, "register", {
58 enumerable: false,
59 configurable: true,
60 writable: true,
61 value: function (target, value, token) {
62 this.registrations.set(token, {
63 value: value,
64 registeredAt: Date.now()
65 });
66 this.scheduleSweep();
67 }
68 });
69 Object.defineProperty(TimerBasedFinalizationRegistry.prototype, "unregister", {
70 enumerable: false,
71 configurable: true,
72 writable: true,
73 value: function (token) {
74 this.registrations.delete(token);
75 }
76 });
77 Object.defineProperty(TimerBasedFinalizationRegistry.prototype, "scheduleSweep", {
78 enumerable: false,
79 configurable: true,
80 writable: true,
81 value: function () {
82 if (this.sweepTimeout === undefined) {
83 this.sweepTimeout = setTimeout(this.sweep, REGISTRY_SWEEP_INTERVAL);
84 }
85 }
86 });
87 return TimerBasedFinalizationRegistry;
88}());
89export { TimerBasedFinalizationRegistry };
90export var UniversalFinalizationRegistry = typeof FinalizationRegistry !== "undefined"
91 ? FinalizationRegistry
92 : TimerBasedFinalizationRegistry;
93//# sourceMappingURL=UniversalFinalizationRegistry.js.map
\No newline at end of file