-
Notifications
You must be signed in to change notification settings - Fork 1
/
tcacher.js
128 lines (118 loc) · 3.8 KB
/
tcacher.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
const tcacher = module.exports;
tcacher.toCachingFunction = function(method, options) {
if (!options) options = {};
const resultProp = options.resultProp || 'id';
const listIndex = options.listIndex || 0;
const wait = createWaiter(options.delay);
var nextTasks = {};
var waitings = {};
return function() {
if (Object.keys(arguments).length > (listIndex + 1)) {
return method.apply(this, arguments)
}
var args = arguments[listIndex];
if (!Array.isArray(args)) args = [args];
var deffer = getDeffer();
var hash = JSON.stringify(getFirstItems(listIndex, arguments));
if (!nextTasks[hash]) {
nextTasks[hash] = [];
}
nextTasks[hash].push({
args: args,
deffer: deffer,
context: this
});
apply(hash, wait);
return deffer;
}
function apply(hash, wait) {
if ((!waitings[hash]) && nextTasks[hash] && nextTasks[hash].length) {
waitings[hash] = true
wait(function() {
var tasks = nextTasks[hash];
delete nextTasks[hash];
var args = [];
var context;
tasks.forEach(function(task) {
context = task.context;
task.args.forEach(function(arg) {
if (args.indexOf(arg) === -1) {
args.push(arg);
}
});
});
method.apply(context, [args]).then(function(results) {
delete waitings[hash];
apply(hash, wait);
var resultIndex = {};
var resultPropName = resultProp;
// allow idName to be in argumentList
if (typeof resultProp === 'number') {
resultPropName = tasks[0].args[resultProp]
}
results.forEach(function(r) {
if (!resultIndex[r[resultPropName]]) resultIndex[r[resultPropName]] = [];
resultIndex[r[resultPropName]].push(r);
});
tasks.forEach(function(tasks) {
var result = [];
tasks.args.forEach(function(arg) {
var r = resultIndex[arg];
if (r) {
result = result.concat(r);
}
});
tasks.deffer.resolve(clone(result));
});
return results;
}).catch(function(err) {
delete waitings[hash];
apply(hash);
tasks.forEach(function(task) {
task.deffer.reject(err);
});
});
});
}
}
}
function getFirstItems(num, args) {
var out = [];
for (var i = 0; i < num; i++) {
out.push(args[i]);
}
return out;
}
function clone(obj) {
if (typeof obj !== obj) return obj;
if(Array.isArray(obj)) return obj.map(clone);
if (obj instanceof Date) {
const date = new Date();
date.setTime(obj.getTime());
return date;
}
const result = {};
Object.keys(obj).forEach(k=>{
result[k] = clone(obj[k]);
});
return result;
}
function getDeffer() {
var resolve;
var reject
var p = new Promise(function(res, rej) {
resolve = res;
reject = rej;
});
p.resolve = resolve;
p.reject = reject;
return p;
};
/**
*
* @param {number} delay
*/
function createWaiter(delay) {
if(!delay) return (cb)=>process.nextTick(cb);
return (cb)=>setTimeout(cb, delay);
}