Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lib: make diagnostics_channel async iterable #35532

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 73 additions & 1 deletion lib/diagnostics_channel.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,14 @@
const {
ArrayPrototypeIndexOf,
ArrayPrototypePush,
ArrayPrototypeShift,
ArrayPrototypeSplice,
ObjectCreate,
ObjectGetPrototypeOf,
ObjectSetPrototypeOf,
Promise,
PromiseResolve,
SymbolAsyncIterator,
SymbolHasInstance,
WeakRefPrototypeGet
} = primordials;
Expand All @@ -21,8 +25,72 @@ const { triggerUncaughtException } = internalBinding('errors');

const { WeakReference } = internalBinding('util');

class AsyncIterableChannel {
constructor(channel) {
this.channel = channel;
this.events = [];
this.waiting = [];

this.subscriber = (message) => {
const resolve = ArrayPrototypeShift(this.waiting);
if (resolve) {
return resolve({
value: message,
done: false
});
}

ArrayPrototypePush(this.events, message);
};

channel.subscribe(this.subscriber);
}

[SymbolAsyncIterator]() {
return this;
}

return() {
const data = { done: true };
this.done = true;

this.channel.unsubscribe(this.subscriber);

for (let i = 0; i < this.waiting.length; i++) {
const resolve = this.waiting[i];
resolve(data);
}

return PromiseResolve(data);
}

next() {
const event = ArrayPrototypeShift(this.events);
if (event) {
return PromiseResolve({
value: event,
done: false
});
}

if (this.done) {
return PromiseResolve({
done: true
});
}

return new Promise((resolve) => {
ArrayPrototypePush(this.waiting, resolve);
});
}
}

// TODO(qard): should there be a C++ channel interface?
class ActiveChannel {
[SymbolAsyncIterator]() {
return new AsyncIterableChannel(this);
}

subscribe(subscription) {
if (typeof subscription !== 'function') {
throw new ERR_INVALID_ARG_TYPE('subscription', ['function'],
Expand Down Expand Up @@ -71,7 +139,11 @@ class Channel {
static [SymbolHasInstance](instance) {
const prototype = ObjectGetPrototypeOf(instance);
return prototype === Channel.prototype ||
prototype === ActiveChannel.prototype;
prototype === ActiveChannel.prototype;
}

[SymbolAsyncIterator]() {
return new AsyncIterableChannel(this);
}

subscribe(subscription) {
Expand Down
31 changes: 31 additions & 0 deletions test/parallel/test-diagnostics-channel-async-iterable.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
'use strict';

const common = require('../common');
const dc = require('diagnostics_channel');
const assert = require('assert');

const input = {
foo: 'bar'
};

const channel = dc.channel('test');

const done = common.mustCall();

async function main() {
for await (const message of channel) {
assert.strictEqual(channel.hasSubscribers, true);
assert.strictEqual(message, input);
break;
}

// Make sure the subscription is cleaned up when breaking the loop!
assert.strictEqual(channel.hasSubscribers, false);
done();
}

main();

setTimeout(common.mustCall(() => {
channel.publish(input);
}), 1);