1 | "use strict";
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 | Object.defineProperty(exports, "__esModule", { value: true });
|
18 |
|
19 |
|
20 | const asyncHook = require("async_hooks");
|
21 | const shimmer = require("shimmer");
|
22 | const WRAPPED = Symbol('context_wrapped');
|
23 |
|
24 | let contexts = new Map();
|
25 | let current = {};
|
26 |
|
27 | asyncHook.createHook({ init, before, destroy }).enable();
|
28 |
|
29 | const EVENT_EMITTER_METHODS = [
|
30 | 'addListener',
|
31 | 'on',
|
32 | 'once',
|
33 | 'prependListener',
|
34 | 'prependOnceListener',
|
35 | ];
|
36 | class AsyncHooksNamespace {
|
37 | get name() {
|
38 | throw new Error('Not implemented');
|
39 | }
|
40 | get active() {
|
41 | return current;
|
42 | }
|
43 | createContext() {
|
44 | throw new Error('Not implemented');
|
45 | }
|
46 | get(k) {
|
47 | return current[k];
|
48 | }
|
49 | set(k, v) {
|
50 | current[k] = v;
|
51 | return v;
|
52 | }
|
53 | run(fn) {
|
54 | this.runAndReturn(fn);
|
55 | return current;
|
56 | }
|
57 | runAndReturn(fn) {
|
58 | const oldContext = current;
|
59 | current = {};
|
60 | if (oldContext['current_tag_map']) {
|
61 | current['current_tag_map'] = oldContext['current_tag_map'];
|
62 | }
|
63 | const res = fn();
|
64 | current = oldContext;
|
65 | return res;
|
66 | }
|
67 | bind(cb) {
|
68 |
|
69 |
|
70 |
|
71 | if (cb[WRAPPED] || !current) {
|
72 | return cb;
|
73 | }
|
74 | const boundContext = current;
|
75 | const contextWrapper = function () {
|
76 | const oldContext = current;
|
77 | current = boundContext;
|
78 | const res = cb.apply(this, arguments);
|
79 | current = oldContext;
|
80 | return res;
|
81 | };
|
82 |
|
83 | contextWrapper[WRAPPED] = true;
|
84 | Object.defineProperty(contextWrapper, 'length', {
|
85 | enumerable: false,
|
86 | configurable: true,
|
87 | writable: false,
|
88 | value: cb.length,
|
89 | });
|
90 | return contextWrapper;
|
91 | }
|
92 |
|
93 |
|
94 |
|
95 |
|
96 |
|
97 | bindEmitter(ee) {
|
98 | const ns = this;
|
99 | EVENT_EMITTER_METHODS.forEach(method => {
|
100 | if (ee[method]) {
|
101 | shimmer.wrap(ee, method, oldMethod => {
|
102 | return function (event, cb) {
|
103 | return oldMethod.call(this, event, ns.bind(cb));
|
104 | };
|
105 | });
|
106 | }
|
107 | });
|
108 | }
|
109 | }
|
110 | const namespace = new AsyncHooksNamespace();
|
111 |
|
112 |
|
113 | function init(uid, provider, parentUid, parentHandle) {
|
114 | contexts.set(uid, current);
|
115 | }
|
116 |
|
117 | function before(uid) {
|
118 | const maybeCurrent = contexts.get(uid);
|
119 | if (maybeCurrent !== undefined) {
|
120 | current = maybeCurrent;
|
121 | }
|
122 | }
|
123 |
|
124 |
|
125 |
|
126 |
|
127 | function destroy(uid) {
|
128 | contexts.delete(uid);
|
129 | }
|
130 | function createNamespace() {
|
131 | return namespace;
|
132 | }
|
133 | exports.createNamespace = createNamespace;
|
134 | function destroyNamespace() {
|
135 | current = {};
|
136 | contexts = new Map();
|
137 | }
|
138 | exports.destroyNamespace = destroyNamespace;
|
139 | function getNamespace() {
|
140 | return namespace;
|
141 | }
|
142 | exports.getNamespace = getNamespace;
|
143 | function reset() {
|
144 | throw new Error('Not implemented');
|
145 | }
|
146 | exports.reset = reset;
|
147 |
|
\ | No newline at end of file |