-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontrol.js
79 lines (66 loc) · 1.97 KB
/
control.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
'use strict';
let memoryLeakThreshold = 32768;
const _ = require('lodash'),
log = console.log, // Figure out logging
minute = 60000;
function defineReadOnly(definition) {
if (!definition.get) {
definition.writable = false;
}
definition.enumerable = false;
definition.configurable = false;
delete definition.set;
return definition;
}
function defineWritable(definition) {
if (!definition.set && !definition.get) {
definition.writable = true;
}
definition.enumerable = false;
definition.configurable = false;
return definition;
}
/**
* Report that a memory leak occurred
* @param {function} fn
* @param {object} cache
*/
function reportMemoryLeak(fn, cache) {
console.log('HI');
log('warn', 'memory leak', fn.name, cache);
}
/**
* Memoize, but warn if the target is not suitable for memoization
* @param {function} fn
* @returns {function}
*/
function memoize(fn) {
const dataProp = '__data__.string.__data__',
memFn = _.memoize.apply(_, _.slice(arguments)),
report = _.throttle(reportMemoryLeak, minute),
controlFn = function () {
const result = memFn.apply(null, _.slice(arguments));
if (_.size(_.get(memFn, `cache.${dataProp}`)) >= memoryLeakThreshold) {
report(fn, _.get(memFn, `cache.${dataProp}`));
}
return result;
};
Object.defineProperty(controlFn, 'cache', defineWritable({
get() { return memFn.cache; },
set(value) { memFn.cache = value; }
}));
return controlFn;
}
function setMemoryLeakThreshold(value) {
memoryLeakThreshold = value;
}
function getMemoryLeakThreshold() {
return memoryLeakThreshold;
}
// module.exports.setReadOnly = setReadOnly;
// module.exports.setDeepObjectTypesReadOnly = setDeepObjectTypesReadOnly;
module.exports.defineReadOnly = defineReadOnly;
module.exports.defineWritable = defineWritable;
module.exports.memoize = memoize;
module.exports.setMemoryLeakThreshold = setMemoryLeakThreshold;
module.exports.getMemoryLeakThreshold = getMemoryLeakThreshold;