-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpolyfill.js
172 lines (148 loc) · 4.55 KB
/
polyfill.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
160
161
162
163
164
165
166
167
168
169
170
171
172
function MemoryObserver(callback) {
if (!new.target) {
throw new TypeError("calling MemoryObserver without new is forbidden");
}
let cancelToken;
function notify() {
// jsHeapSizeLimit
// totalJSHeapSize
// usedJSHeapSize
callback(performance.memory);
// check again later
cancelToken = window.requestIdleCallback(notify, {
timeout: 500
});
}
this.disconnect = () => window.cancelIdleCallback(cancelToken);
this.observe = notify;
this.takeRecords = () => [performance.memory];
}
Function.memoize = (function() {
const NO_VALUE = Symbol("NO_VALUE");
const cacheInstances = new Set();
let isSystemMemoryConstrained = false;
const memoryObserver = new MemoryObserver(
({ jsHeapSizeLimit, usedJSHeapSize } = {}) => {
const shouldBeConstrained = usedJSHeapSize / jsHeapSizeLimit >= 0.8;
if (shouldBeConstrained && !isSystemMemoryConstrained) {
flushCaches();
}
isSystemMemoryConstrained = shouldBeConstrained;
}
);
function flushCaches() {
cacheInstances.forEach(wrappedCacheInstance => {
const cacheInstance = wrappedCacheInstance.deref();
if (!cacheInstance) {
cacheInstances.delete(cacheInstance); // remove empty wrappers
} else {
cacheInstance = createNode(); // effectively delete the entire cache
}
});
}
memoryObserver.observe();
const registry = new FinalizationRegistry(function schedulePruning(
cacheInstance
) {
// An object key in the cacheInstance has been Garbage Collected.
// Schedule a cleanup of the cache at the next idle period.
window.requestIdleCallback(() => pruneNodeCache(cacheInstance));
});
// remove all branches that are keyed off of empty (GC'd) WeakRefs
function pruneNodeCache(nodeCache) {
const cache = nodeCache.deref();
if (!cache) return; // the node no longer exists
for (const [k, v] of cache.entries()) {
if (isWeakRef(k) && !k.deref()) {
cache.delete(k);
}
}
}
function isWeakRef(wr) {
return Object.prototype.toString.call(wr).slice(8, -1) === "WeakRef";
}
function isNonNullObject(o) {
return o !== null && typeof o === "object";
}
function createNode() {
const node = {
cache: new Map(),
value: NO_VALUE
};
return node;
}
function getNode(node, key, compare) {
for (const [k, nextNode] of node.cache.entries()) {
const _key = isWeakRef(k) ? k.deref() : k;
if (compare(_key, key)) {
return nextNode;
}
}
return null;
}
function getValueAt(node, compare, ...keys) {
let currentNode = node;
while (keys.length) {
currentNode = getNode(currentNode, keys.shift(), compare);
if (currentNode === null) {
return NO_VALUE;
}
}
// unbox value
return isWeakRef(currentNode.value)
? currentNode.value.deref()
: currentNode.value;
}
function setValueAt(node, compare, value, ...keys) {
let currentNode = node;
// traverse to the leaf node
while (keys.length) {
let key = keys.shift();
let nextNode = getNode(currentNode, key, compare);
if (nextNode === null) {
nextNode = createNode();
if (isNonNullObject(key)) {
registry.register(key, new WeakRef(currentNode.cache));
key = new WeakRef(key);
}
currentNode.cache.set(key, nextNode);
}
currentNode = nextNode;
}
currentNode.value = isNonNullObject(value) ? new WeakRef(value) : value;
}
function referenitalEquality(a, b) {
return a === b;
}
return function(fn, opts) {
const rootNode = createNode();
const hash = opts && typeof opts.hash === "function" ? opts.hash : null;
const select =
opts && typeof opts.select === "function" ? opts.select : null;
const compare =
opts && typeof opts.compare === "function"
? opts.compare
: referenitalEquality;
cacheInstances.add(new WeakRef(rootNode));
return function() {
const keys =
hash && select
? [hash(select(...arguments))]
: hash
? [hash(...arguments)]
: select
? select(...arguments)
: arguments;
let value = getValueAt(rootNode, compare, ...keys);
if (value === NO_VALUE) {
value = fn.apply(null, arguments);
if (isSystemMemoryConstrained) {
// in a constrained environment, only store the last set of arguments
rootNode = createNode();
}
setValueAt(rootNode, compare, value, ...keys);
}
return value;
};
};
})();