This repository has been archived by the owner on Jun 10, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
WeakMapStorage.js
159 lines (138 loc) · 3.63 KB
/
WeakMapStorage.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import MapStorage from './MapStorage';
/**
* A specialization of the {@codelink MapStorage} storage mimicking the native
* {@code WeakMap} using its internal garbage collector used once the size of
* the storage reaches the configured threshold.
*/
export default class WeakMapStorage extends MapStorage {
/**
* Initializes the storage.
*
* @param {{entryTtl: number}} config Weak map storage configuration. The
* fields have the following meaning:
* - entryTtl The time-to-live of a storage entry in milliseconds.
*/
constructor(config) {
super();
/**
* The time-to-live of a storage entry in milliseconds.
*
* @type {number}
*/
this._entryTtl = config.entryTtl;
}
/**
* @inheritdoc
*/
has(key) {
this._discardExpiredEntries();
return super.has(key);
}
/**
* @inheritdoc
*/
get(key) {
this._discardExpiredEntries();
if (!super.has(key)) {
return undefined;
}
return super.get(key).target;
}
/**
* @inheritdoc
*/
set(key, value) {
this._discardExpiredEntries();
return super.set(key, new WeakRef(value, this._entryTtl));
}
/**
* @inheritdoc
*/
delete(key) {
this._discardExpiredEntries();
return super.delete(key);
}
/**
* @inheritdoc
*/
keys() {
this._discardExpiredEntries();
return super.keys();
}
/**
* @inheritdoc
*/
size() {
this._discardExpiredEntries();
return super.size();
}
/**
* Deletes all expired entries from this storage.
*/
_discardExpiredEntries() {
for (let key of super.keys()) {
let targetReference = super.get(key);
if (!targetReference.target) {
// the reference has died
super.delete(key);
}
}
}
}
/**
* A simple reference wrapper that emulates a weak reference. We seem to have
* no other option, since WeakMap and WeakSet are not enumerable (so what is
* the point of WeakMap and WeakSet if you still need to manage the keys?!) and
* there is no native way to create a weak reference.
*/
class WeakRef {
/**
* Initializes the weak reference to the target reference.
*
* @param {Object} target The target reference that should be referenced by
* this weak reference.
* @param {number} ttl The maximum number of milliseconds the weak
* reference should be kept. The reference will be discarded once
* ACCESSED after the specified timeout.
*/
constructor(target, ttl) {
if ($Debug) {
if (!(target instanceof Object)) {
throw new TypeError(
'The target reference must point to an object, ' +
'primitive values are not allowed'
);
}
if (ttl <= 0) {
throw new Error('The time-to-live must be positive');
}
}
/**
* The actual target reference, or {@code null} if the reference has
* been already discarded.
*
* @type {?Object}
*/
this._reference = target;
/**
* The UNIX timestamp with millisecond precision marking the moment at
* or after which the reference will be discarded.
*
* @type {number}
*/
this._expiration = Date.now() + ttl;
}
/**
* Returns the target reference, provided that the target reference is
* still alive. Returns {@code null} if the reference has been discarded.
*
* @return {?Object} The target reference, or {@code null} if the reference
* has been discarded by the garbage collector.
*/
get target() {
if (this._reference && Date.now() >= this._expiration) {
this._reference = null; // let the GC do its job
}
return this._reference;
}
}