forked from vicanso/async-local-storage
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
197 lines (176 loc) · 3.84 KB
/
index.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
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;
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;
}
const value = data[key];
if (isUndefined(value) && data.parent) {
return get(data.parent, key);
}
return value;
}
/**
* Get the top data
*/
function getTop(data) {
while (data.parent) {
data = data.parent
}
return data
}
let currentId = 0;
const hooks = asyncHooks.createHook({
init: function init(id, type, triggerId) {
// init, set the created time
const 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('%d(%s) init by %d', id, type, 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 %d', 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 {any} 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 %s:%j to %d', key, value, 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 %s:%j from %d', key, value, currentId);
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) {
return -1;
}
return nano.difference(data.created);
};