UNPKG

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