Merge remote-tracking branch 'ui/master'

This commit is contained in:
Zaid Abdulla committed 2018-12-27 22:22:44 -08:00
commit 4530967b6b
70 files changed
+10951 -8

No files matched your search

+47
View File
@@ -0,0 +1,47 @@
"use strict";
let cache1 = new Map();
let cache2 = new Map();
const keys = new WeakMap();
function getKey(obj) {
if (obj === null) return "null";
else if (obj === undefined) return "undefined";
const t = typeof obj;
if (t === "number" || t === "boolean" || t === "string") return `${t}:${obj}`;
if (t !== "function" && t !== "object")
throw new Error(`Cannot memoize ${t} arguments`);
let k = keys.get(obj);
if (!k) {
const rnd = Math.trunc(Math.random() * Number.MAX_SAFE_INTEGER);
k = `${t}:${rnd.toString(36)}`;
keys.set(obj, k);
}
return k;
}
function memoize(func) {
return (...args) => {
const key = JSON.stringify(args.map(getKey)) + getKey(func);
if (cache1.has(key)) return cache1.get(key);
let r;
if (cache2.has(key)) r = cache2.get(key);
else r = func(...args);
cache1.set(key, r);
return r;
};
}
const interval = setInterval(() => {
cache2 = cache1;
cache1 = new Map();
}, 120000);
// Don't hold Node.js process
if (interval.unref) interval.unref();
module.exports = memoize;