-
Notifications
You must be signed in to change notification settings - Fork 207
/
context.js
204 lines (168 loc) · 4.91 KB
/
context.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
'use strict';
var assert = require('assert');
var wrapEmitter = require('emitter-listener');
/*
*
* CONSTANTS
*
*/
var CONTEXTS_SYMBOL = 'cls@contexts';
var ERROR_SYMBOL = 'error@context';
// load polyfill if native support is unavailable
if (!process.addAsyncListener) require('async-listener');
function Namespace(name) {
this.name = name;
// changed in 2.7: no default context
this.active = null;
this._set = [];
this.id = null;
}
Namespace.prototype.set = function (key, value) {
if (!this.active) {
throw new Error("No context available. ns.run() or ns.bind() must be called first.");
}
this.active[key] = value;
return value;
};
Namespace.prototype.get = function (key) {
if (!this.active) return undefined;
return this.active[key];
};
Namespace.prototype.createContext = function () {
return Object.create(this.active);
};
Namespace.prototype.run = function (fn) {
var context = this.createContext();
this.enter(context);
try {
fn(context);
return context;
}
catch (exception) {
if (exception) {
exception[ERROR_SYMBOL] = context;
}
throw exception;
}
finally {
this.exit(context);
}
};
Namespace.prototype.bind = function (fn, context) {
if (!context) {
if (!this.active) {
context = Object.create(this.active);
}
else {
context = this.active;
}
}
var self = this;
return function () {
self.enter(context);
try {
return fn.apply(this, arguments);
}
catch (exception) {
if (exception) {
exception[ERROR_SYMBOL] = context;
}
throw exception;
}
finally {
self.exit(context);
}
};
};
Namespace.prototype.enter = function (context) {
assert.ok(context, "context must be provided for entering");
this._set.push(this.active);
this.active = context;
};
Namespace.prototype.exit = function (context) {
assert.ok(context, "context must be provided for exiting");
// Fast path for most exits that are at the top of the stack
if (this.active === context) {
assert.ok(this._set.length, "can't remove top context");
this.active = this._set.pop();
return;
}
// Fast search in the stack using lastIndexOf
var index = this._set.lastIndexOf(context);
assert.ok(index >= 0, "context not currently entered; can't exit");
assert.ok(index, "can't remove top context");
this._set.splice(index, 1);
};
Namespace.prototype.bindEmitter = function (emitter) {
assert.ok(emitter.on && emitter.addListener && emitter.emit, "can only bind real EEs");
var namespace = this;
var thisSymbol = 'context@' + this.name;
// Capture the context active at the time the emitter is bound.
function attach(listener) {
if (!listener) return;
if (!listener[CONTEXTS_SYMBOL]) listener[CONTEXTS_SYMBOL] = Object.create(null);
listener[CONTEXTS_SYMBOL][thisSymbol] = {
namespace : namespace,
context : namespace.active
};
}
// At emit time, bind the listener within the correct context.
function bind(unwrapped) {
if (!(unwrapped && unwrapped[CONTEXTS_SYMBOL])) return unwrapped;
var wrapped = unwrapped;
var contexts = unwrapped[CONTEXTS_SYMBOL];
Object.keys(contexts).forEach(function (name) {
var thunk = contexts[name];
wrapped = thunk.namespace.bind(wrapped, thunk.context);
});
return wrapped;
}
wrapEmitter(emitter, attach, bind);
};
/**
* If an error comes out of a namespace, it will have a context attached to it.
* This function knows how to find it.
*
* @param {Error} exception Possibly annotated error.
*/
Namespace.prototype.fromException = function (exception) {
return exception[ERROR_SYMBOL];
};
function get(name) {
return process.namespaces[name];
}
function create(name) {
assert.ok(name, "namespace must be given a name!");
var namespace = new Namespace(name);
namespace.id = process.addAsyncListener({
create : function () { return namespace.active; },
before : function (context, storage) { if (storage) namespace.enter(storage); },
after : function (context, storage) { if (storage) namespace.exit(storage); },
error : function (storage) { if (storage) namespace.exit(storage); }
});
process.namespaces[name] = namespace;
return namespace;
}
function destroy(name) {
var namespace = get(name);
assert.ok(namespace, "can't delete nonexistent namespace!");
assert.ok(namespace.id, "don't assign to process.namespaces directly!");
process.removeAsyncListener(namespace.id);
process.namespaces[name] = null;
}
function reset() {
// must unregister async listeners
if (process.namespaces) {
Object.keys(process.namespaces).forEach(function (name) {
destroy(name);
});
}
process.namespaces = Object.create(null);
}
if (!process.namespaces) reset(); // call immediately to set up
module.exports = {
getNamespace : get,
createNamespace : create,
destroyNamespace : destroy,
reset : reset
};