@gjsify/async_hooks 0.3.12 → 0.3.14
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/esm/index.js +122 -129
- package/package.json +4 -4
package/lib/esm/index.js
CHANGED
|
@@ -1,138 +1,131 @@
|
|
|
1
|
+
//#region src/index.ts
|
|
1
2
|
let _id = 1;
|
|
2
3
|
function executionAsyncId() {
|
|
3
|
-
|
|
4
|
+
return _id;
|
|
4
5
|
}
|
|
5
6
|
function triggerAsyncId() {
|
|
6
|
-
|
|
7
|
+
return 0;
|
|
7
8
|
}
|
|
8
9
|
function createHook(_callbacks) {
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
10
|
+
return {
|
|
11
|
+
enable() {
|
|
12
|
+
return this;
|
|
13
|
+
},
|
|
14
|
+
disable() {
|
|
15
|
+
return this;
|
|
16
|
+
}
|
|
17
|
+
};
|
|
17
18
|
}
|
|
18
|
-
class AsyncResource {
|
|
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
|
-
const _allInstances =
|
|
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
|
-
var index_default = {
|
|
125
|
-
executionAsyncId,
|
|
126
|
-
triggerAsyncId,
|
|
127
|
-
createHook,
|
|
128
|
-
AsyncResource,
|
|
129
|
-
AsyncLocalStorage
|
|
19
|
+
var AsyncResource = class AsyncResource {
|
|
20
|
+
_type;
|
|
21
|
+
_asyncId;
|
|
22
|
+
_triggerAsyncId;
|
|
23
|
+
constructor(type, triggerAsyncIdOrOpts) {
|
|
24
|
+
this._type = type;
|
|
25
|
+
this._asyncId = _id++;
|
|
26
|
+
if (typeof triggerAsyncIdOrOpts === "number") {
|
|
27
|
+
this._triggerAsyncId = triggerAsyncIdOrOpts;
|
|
28
|
+
} else if (triggerAsyncIdOrOpts && typeof triggerAsyncIdOrOpts.triggerAsyncId === "number") {
|
|
29
|
+
this._triggerAsyncId = triggerAsyncIdOrOpts.triggerAsyncId;
|
|
30
|
+
} else {
|
|
31
|
+
this._triggerAsyncId = executionAsyncId();
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
runInAsyncScope(fn, thisArg, ...args) {
|
|
35
|
+
return fn.apply(thisArg, args);
|
|
36
|
+
}
|
|
37
|
+
emitDestroy() {
|
|
38
|
+
return this;
|
|
39
|
+
}
|
|
40
|
+
asyncId() {
|
|
41
|
+
return this._asyncId;
|
|
42
|
+
}
|
|
43
|
+
triggerAsyncId() {
|
|
44
|
+
return this._triggerAsyncId;
|
|
45
|
+
}
|
|
46
|
+
bind(fn) {
|
|
47
|
+
return ((...args) => {
|
|
48
|
+
return this.runInAsyncScope(fn, undefined, ...args);
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
static bind(fn, type) {
|
|
52
|
+
const resource = new AsyncResource(type || fn.name || "bound-anonymous-fn");
|
|
53
|
+
return resource.bind(fn);
|
|
54
|
+
}
|
|
55
|
+
};
|
|
56
|
+
const _allInstances = new Set();
|
|
57
|
+
var AsyncLocalStorage = class {
|
|
58
|
+
_store;
|
|
59
|
+
constructor() {
|
|
60
|
+
_allInstances.add(this);
|
|
61
|
+
}
|
|
62
|
+
getStore() {
|
|
63
|
+
return this._store;
|
|
64
|
+
}
|
|
65
|
+
run(store, callback, ...args) {
|
|
66
|
+
const prev = this._store;
|
|
67
|
+
this._store = store;
|
|
68
|
+
try {
|
|
69
|
+
const result = callback(...args);
|
|
70
|
+
if (result != null && typeof result.then === "function") {
|
|
71
|
+
return result.then((value) => {
|
|
72
|
+
this._store = prev;
|
|
73
|
+
return value;
|
|
74
|
+
}, (err) => {
|
|
75
|
+
this._store = prev;
|
|
76
|
+
throw err;
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
this._store = prev;
|
|
80
|
+
return result;
|
|
81
|
+
} catch (err) {
|
|
82
|
+
this._store = prev;
|
|
83
|
+
throw err;
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
exit(callback, ...args) {
|
|
87
|
+
const prev = this._store;
|
|
88
|
+
this._store = undefined;
|
|
89
|
+
try {
|
|
90
|
+
return callback(...args);
|
|
91
|
+
} finally {
|
|
92
|
+
this._store = prev;
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
enterWith(store) {
|
|
96
|
+
this._store = store;
|
|
97
|
+
}
|
|
98
|
+
disable() {
|
|
99
|
+
this._store = undefined;
|
|
100
|
+
}
|
|
101
|
+
static snapshot() {
|
|
102
|
+
const captured = new Map();
|
|
103
|
+
for (const instance of _allInstances) {
|
|
104
|
+
captured.set(instance, instance.getStore());
|
|
105
|
+
}
|
|
106
|
+
return (fn, ...args) => {
|
|
107
|
+
const saved = new Map();
|
|
108
|
+
for (const [instance, store] of captured) {
|
|
109
|
+
saved.set(instance, instance.getStore());
|
|
110
|
+
instance._store = store;
|
|
111
|
+
}
|
|
112
|
+
try {
|
|
113
|
+
return fn(...args);
|
|
114
|
+
} finally {
|
|
115
|
+
for (const [instance, store] of saved) {
|
|
116
|
+
instance._store = store;
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
};
|
|
120
|
+
}
|
|
130
121
|
};
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
triggerAsyncId
|
|
122
|
+
var src_default = {
|
|
123
|
+
executionAsyncId,
|
|
124
|
+
triggerAsyncId,
|
|
125
|
+
createHook,
|
|
126
|
+
AsyncResource,
|
|
127
|
+
AsyncLocalStorage
|
|
138
128
|
};
|
|
129
|
+
|
|
130
|
+
//#endregion
|
|
131
|
+
export { AsyncLocalStorage, AsyncResource, createHook, src_default as default, executionAsyncId, triggerAsyncId };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@gjsify/async_hooks",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.14",
|
|
4
4
|
"description": "Node.js async_hooks module for Gjs",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"module": "lib/esm/index.js",
|
|
@@ -30,11 +30,11 @@
|
|
|
30
30
|
"async_hooks"
|
|
31
31
|
],
|
|
32
32
|
"dependencies": {
|
|
33
|
-
"@gjsify/events": "^0.3.
|
|
33
|
+
"@gjsify/events": "^0.3.14"
|
|
34
34
|
},
|
|
35
35
|
"devDependencies": {
|
|
36
|
-
"@gjsify/cli": "^0.3.
|
|
37
|
-
"@gjsify/unit": "^0.3.
|
|
36
|
+
"@gjsify/cli": "^0.3.14",
|
|
37
|
+
"@gjsify/unit": "^0.3.14",
|
|
38
38
|
"@types/node": "^25.6.0",
|
|
39
39
|
"typescript": "^6.0.3"
|
|
40
40
|
}
|