This repository has been archived by the owner on Dec 1, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
als.js
255 lines (229 loc) · 5.06 KB
/
als.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
const asyncHooks = require('async_hooks');
const nano = require('nano-seconds');
const util = require('util');
const fs = require('fs');
const map = new Map();
const enabledDebug = process.env.DEBUG === 'als';
function debug(...args) {
if (!enabledDebug) {
return;
}
// use a function like this one when debugging inside an AsyncHooks callback
fs.writeSync(1, `${util.format(...args)}\n`);
}
let defaultLinkedTop = false;
let enabledCreatedAt = true;
function isUndefined(value) {
return value === undefined;
}
/**
* Get data from itself or parent
* @param {any} data The map data
* @param {any} key The key
* @returns {any}
*/
function get(data, key) {
/* istanbul ignore if */
if (!data) {
return null;
}
let currentData = data;
let value = currentData[key];
while (isUndefined(value) && currentData.parent) {
currentData = currentData.parent;
value = currentData[key];
}
return value;
}
/**
* Get the top data
*/
function getTop(data) {
let result = data;
while (result && result.parent) {
result = result.parent;
}
return result;
}
let currentId = 0;
const hooks = asyncHooks.createHook({
init: function init(id, type, triggerId) {
const data = {};
// init, set the created time
if (enabledCreatedAt) {
data.created = nano.now();
}
const parentId = triggerId || currentId;
// not trigger by itself, add parent
if (parentId !== id) {
const parent = map.get(parentId);
if (parent) {
data.parent = parent;
}
}
debug(`${id}(${type}) init by ${triggerId}`);
map.set(id, data);
},
/**
* Set the current id
*/
before: function before(id) {
currentId = id;
},
/**
* Remove the data
*/
destroy: function destroy(id) {
if (!map.has(id)) {
return;
}
debug(`destroy ${id}`);
map.delete(id);
},
});
/**
* Get the current id
*/
function getCurrentId() {
if (asyncHooks.executionAsyncId) {
return asyncHooks.executionAsyncId();
}
return asyncHooks.currentId() || currentId;
}
/**
* Get the current id
*/
exports.currentId = getCurrentId;
/**
* Enable the async hook
*/
exports.enable = () => hooks.enable();
/**
* Disable the async hook
*/
exports.disable = () => hooks.disable();
/**
* Get the size of map
*/
exports.size = () => map.size;
/**
* Enable linked top
*/
exports.enableLinkedTop = () => {
defaultLinkedTop = true;
};
/**
* Disable linked top
*/
exports.disableLinkedTop = () => {
defaultLinkedTop = false;
};
/**
* Set the key/value for this score
* @param {String} key The key of value
* @param {String} value The value
* @param {Boolean} linkedTop The value linked to top
* @returns {Boolean} if success, will return true, otherwise false
*/
exports.set = function setValue(key, value, linkedTop) {
/* istanbul ignore if */
if (key === 'created' || key === 'parent') {
throw new Error("can't set created and parent");
}
const id = getCurrentId();
debug(`set ${key}:${value} to ${id}`);
let data = map.get(id);
/* istanbul ignore if */
if (!data) {
return false;
}
let setToLinkedTop = linkedTop;
if (isUndefined(linkedTop)) {
setToLinkedTop = defaultLinkedTop;
}
if (setToLinkedTop) {
data = getTop(data);
}
data[key] = value;
return true;
};
/**
* Get the value by key
* @param {String} key The key of value
*/
exports.get = function getValue(key) {
const data = map.get(getCurrentId());
const value = get(data, key);
debug(`get ${key}:${value} from ${currentId}`);
return value;
};
/**
* 获取当前current data
*/
exports.getCurrentData = () => map.get(getCurrentId());
/**
* Get the value by key from parent
* @param {String} key The key of value
*/
exports.getFromParent = key => {
const currentData = map.get(getCurrentId());
if (!currentData) {
return null;
}
const value = get({parent: currentData.parent}, key);
return value;
};
/**
* Remove the data of the current id
*/
exports.remove = function removeValue() {
const id = getCurrentId();
if (id) {
map.delete(id);
}
};
/**
* Get the use the of id
* @param {Number} id The trigger id, is optional, default is `als.currentId()`
* @returns {Number} The use time(ns) of the current id
*/
exports.use = function getUse(id) {
const data = map.get(id || getCurrentId());
/* istanbul ignore if */
if (!data || !enabledCreatedAt) {
return -1;
}
return nano.difference(data.created);
};
/**
* Get the top value
*/
exports.top = function top() {
const data = map.get(getCurrentId());
return getTop(data);
};
/**
* Set the scope (it will change the top)
*/
exports.scope = function scope() {
const data = map.get(getCurrentId());
delete data.parent;
};
/**
* Get all data of async locatl storage, please don't modify the data
*/
exports.getAllData = function getAllData() {
return map;
};
/**
* Enable the create time of data
*/
exports.enableCreateTime = function enableCreateTime() {
enabledCreatedAt = true;
};
/**
* Disable the create time of data
*/
exports.disableCreateTime = function disableCreateTime() {
enabledCreatedAt = false;
};