Skip to content

Commit 1a233aa

Browse files
author
Stephen Belanger
committed
diagnostics_channel: split active/inactive prototype for StorageChannel
1 parent 00f4f7c commit 1a233aa

File tree

2 files changed

+81
-23
lines changed

2 files changed

+81
-23
lines changed

lib/_http_server.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1010,8 +1010,8 @@ function parserOnIncoming(server, socket, state, req, keepAlive) {
10101010
});
10111011
}
10121012

1013-
if (storageChannel._enter.hasSubscribers) {
1014-
storageChannel._enter.publish({
1013+
if (storageChannel.hasSubscribers) {
1014+
storageChannel._enter({
10151015
request: req,
10161016
response: res,
10171017
socket,
@@ -1077,8 +1077,8 @@ function parserOnIncoming(server, socket, state, req, keepAlive) {
10771077
server.emit('request', req, res);
10781078
}
10791079

1080-
if (storageChannel._exit.hasSubscribers) {
1081-
storageChannel._exit.publish();
1080+
if (storageChannel.hasSubscribers) {
1081+
storageChannel._exit();
10821082
}
10831083

10841084
return 0; // No special treatment.

lib/diagnostics_channel.js

Lines changed: 77 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
const {
44
ArrayPrototypeIndexOf,
55
ArrayPrototypePush,
6+
ArrayPrototypeSome,
67
ArrayPrototypeSplice,
78
ObjectCreate,
89
ObjectGetPrototypeOf,
@@ -160,43 +161,100 @@ class StoreBinding {
160161

161162
// TODO(qard): figure out if putting everything on the channel has the same GC
162163
// issue as channel.subscribe(...) did.
163-
class StorageChannel {
164-
constructor(name) {
165-
this._enter = channel(`${name}.enter-store`);
166-
this._exit = channel(`${name}.exit-store`);
167-
this.kBinding = Symbol('binding');
164+
class ActiveStorageChannel {
165+
get hasSubscribers () {
166+
return true;
168167
}
169168

170-
isBoundToStore (store) {
171-
return !!store[this.kBinding];
169+
isBoundToStore(store) {
170+
return ArrayPrototypeSome(this.bindings, v => v.store === store);
172171
}
173172

174173
bindStore(store, build) {
175-
if (this.isBoundToStore(store)) return;
176-
const binding = new StoreBinding(store, build);
177-
store[this.kBinding] = binding;
178-
this._enter.subscribe(binding.onEnter);
179-
this._exit.subscribe(binding.onExit);
174+
if (this.isBoundToStore(store)) return false;
175+
ArrayPrototypePush(this.bindings, new StoreBinding(store, build));
176+
return true;
180177
}
181178

182179
unbindStore(store) {
183-
if (!this.isBoundToStore(store)) return;
184-
const binding = store[this.kBinding];
185-
this._enter.unsubscribe(binding.onEnter);
186-
this._exit.unsubscribe(binding.onExit);
187-
delete store[this.kBinding];
180+
if (!this.isBoundToStore(store)) return false;
181+
182+
let found = false;
183+
for (let index = 0; index < this.bindings.length; index++) {
184+
if (this.bindings[index].store === store) {
185+
ArrayPrototypeSplice(this.bindings, index, 1);
186+
found = true;
187+
break;
188+
}
189+
}
190+
191+
if (!this.bindings.length) {
192+
ObjectSetPrototypeOf(this, StorageChannel.prototype);
193+
this.bindings = undefined;
194+
}
195+
196+
return found;
197+
}
198+
199+
_enter(data) {
200+
for (const binding of this.bindings) {
201+
binding.onEnter(data);
202+
}
203+
}
204+
205+
_exit() {
206+
for (const binding of this.bindings) {
207+
binding.onExit();
208+
}
188209
}
189210

190211
run(data, fn) {
191-
this._enter.publish(data);
212+
this._enter(data);
192213
try {
193214
return fn();
194215
} finally {
195-
this._exit.publish();
216+
this._exit();
196217
}
197218
}
198219
}
199220

221+
class StorageChannel {
222+
constructor() {
223+
this.bindings = undefined;
224+
}
225+
226+
static [SymbolHasInstance](instance) {
227+
const prototype = ObjectGetPrototypeOf(instance);
228+
return prototype === StorageChannel.prototype ||
229+
prototype === ActiveStorageChannel.prototype;
230+
}
231+
232+
get hasSubscribers() {
233+
return false;
234+
}
235+
236+
isBoundToStore(_) {
237+
return false;
238+
}
239+
240+
bindStore(store, build) {
241+
ObjectSetPrototypeOf(this, ActiveStorageChannel.prototype);
242+
this.bindings = [];
243+
return this.bindStore(store, build);
244+
}
245+
246+
unbindStore(_) {
247+
return false;
248+
}
249+
250+
_enter(_) {}
251+
_exit() {}
252+
253+
run(_, fn) {
254+
return fn();
255+
}
256+
}
257+
200258
const storageChannels = ObjectCreate(null);
201259

202260
function storageChannel(name) {

0 commit comments

Comments
 (0)