Skip to content

Commit 500b567

Browse files
jasnelladuh95
authored andcommitted
stream: make Broadcast.from() only return the Broadcast
Signed-off-by: James M Snell <jasnell@gmail.com> Assisted-by: Opencode PR-URL: #66030 Reviewed-By: Filip Skokan <panva.ip@gmail.com> Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
1 parent d40236c commit 500b567

4 files changed

Lines changed: 16 additions & 6 deletions

File tree

‎doc/api/stream_iter.md‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1330,7 +1330,8 @@ added: v25.9.0
13301330

13311331
* `input` {AsyncIterable|Iterable|BroadcastChannel}
13321332
* `options` {Object} Same as `broadcast()`.
1333-
* Returns: {Object} `{ writer, broadcast }`
1333+
* Returns: {BroadcastChannel|Object} A `broadcastProtocol` input returns its
1334+
{BroadcastChannel} directly. Other inputs return `{ writer, broadcast }`.
13341335

13351336
Create a {BroadcastChannel} from an existing source. The source is consumed
13361337
automatically and pushed to all subscribers.
@@ -1823,7 +1824,7 @@ class MessageBus {
18231824
}
18241825

18251826
const bus = new MessageBus();
1826-
const { broadcast } = Broadcast.from(bus);
1827+
const broadcast = Broadcast.from(bus);
18271828
const consumer = broadcast.push();
18281829
bus.send('hello');
18291830
bus.close();
@@ -1859,7 +1860,7 @@ class MessageBus {
18591860
}
18601861

18611862
const bus = new MessageBus();
1862-
const { broadcast } = Broadcast.from(bus);
1863+
const broadcast = Broadcast.from(bus);
18631864
const consumer = broadcast.push();
18641865
bus.send('hello');
18651866
bus.close();

‎lib/internal/streams/iter/broadcast.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -923,7 +923,7 @@ const Broadcast = {
923923
throw new ERR_INVALID_RETURN_VALUE(
924924
'an object', '[Symbol.for(\'Stream.broadcastProtocol\')]', bc);
925925
}
926-
return { __proto__: null, writer: { __proto__: null }, broadcast: bc };
926+
return bc;
927927
}
928928

929929
const source = from(input);

‎test/parallel/test-stream-iter-broadcast-from.js‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,15 @@ async function testBroadcastFromSourceError() {
181181
// Protocol validation
182182
// =============================================================================
183183

184+
function testBroadcastProtocolReturnsBroadcast() {
185+
const { broadcast: expected } = broadcast();
186+
const obj = {
187+
[Symbol.for('Stream.broadcastProtocol')]() { return expected; },
188+
};
189+
assert.strictEqual(Broadcast.from(obj), expected);
190+
expected.cancel();
191+
}
192+
184193
function testBroadcastProtocolReturnsNull() {
185194
const obj = {
186195
[Symbol.for('Stream.broadcastProtocol')]() { return null; },
@@ -223,6 +232,7 @@ Promise.all([
223232
testAlreadyAbortedSignal(),
224233
testBroadcastFromCancelWhileBlocked(),
225234
testBroadcastFromSourceError(),
235+
testBroadcastProtocolReturnsBroadcast(),
226236
testBroadcastProtocolReturnsNull(),
227237
testBroadcastProtocolReturnsString(),
228238
testBroadcastProtocolReturnsUndefined(),

‎test/parallel/test-stream-iter-property-access.js‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,8 +140,7 @@ async function testMultiConsumerProtocolsSnapshotMethods() {
140140
const broadcastTarget = broadcast().broadcast;
141141
const broadcastFixture = protocolFixture(
142142
broadcastProtocol, broadcastTarget);
143-
assert.strictEqual(
144-
Broadcast.from(broadcastFixture.input).broadcast, broadcastTarget);
143+
assert.strictEqual(Broadcast.from(broadcastFixture.input), broadcastTarget);
145144
assert.strictEqual(broadcastFixture.accesses, 1);
146145

147146
const shareTarget = share('abc');

0 commit comments

Comments
 (0)