forked from vicanso/async-local-storage
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
155 lines (141 loc) · 3.1 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
const asyncHooks = require('async_hooks');
const nano = require('nano-seconds');
const pkg = require('./package');
const debug = require('debug')(pkg.name);
const map = new Map();
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;
}
let currentId = 0;
const hooks = asyncHooks.createHook({
init: function init(id, type, triggerId) {
if (type === 'TickObject') {
return;
}
// init, set the created time
const data = {
created: nano.now(),
};
const parentId = triggerId || currentId;
// not tigger 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;
/**
* Set the key/value for this score
* @param {String} key The key of value
* @param {String} value The value
* @returns {Boolean} if sucess, will return true, otherwise false
*/
exports.set = function setValue(key, value) {
/* istanbul ignore if */
if (key === 'created' || key === 'paraent') {
throw new Error('can\'t set created and parent');
}
const id = getCurrentId();
debug(`set ${key}:${value} to ${id}`);
const data = map.get(id);
/* istanbul ignore if */
if (!data) {
return false;
}
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;
};
/**
* 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 tigger 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);
};