Stream.shareSyncProtocol


  • 值:Symbol.for('Stream.shareSyncProtocol')

该值必须是一个函数。当被 SyncShare.fromSync() 调用时,它会接收到传递给 SyncShare.fromSync() 的选项,并且必须返回一个符合 SyncShare 接口的对象。实现完全自定义——它可以按照自己的方式管理共享源、消费者和缓冲。

🌐 The value must be a function. When called by SyncShare.fromSync(), it receives the options passed to SyncShare.fromSync() and must return an object conforming to the SyncShare interface. The implementation is fully custom -- it can manage the shared source, consumers, and buffering however it wants.

import { shareSync, SyncShare, textSync } from 'node:stream/iter';
// This example defers to the built-in shareSync(), but a custom
// implementation could use any mechanism.
class SyncDataPool {
 #share;
 constructor(source) {
 this.#share = shareSync(source);
 }
 [Symbol.for('Stream.shareSyncProtocol')](options) {
 return this.#share;
 }
}
const encoder = new TextEncoder();
const pool = new SyncDataPool(
 function* () {
 yield [encoder.encode('hello')];
 }(),
);
const shared = SyncShare.fromSync(pool);
const consumer = shared.pull();
console.log(textSync(consumer)); // 'hello'const { shareSync, SyncShare, textSync } = require('node:stream/iter');
// This example defers to the built-in shareSync(), but a custom
// implementation could use any mechanism.
class SyncDataPool {
 #share;
 constructor(source) {
 this.#share = shareSync(source);
 }
 [Symbol.for('Stream.shareSyncProtocol')](options) {
 return this.#share;
 }
}
const encoder = new TextEncoder();
const pool = new SyncDataPool(
 function* () {
 yield [encoder.encode('hello')];
 }(),
);
const shared = SyncShare.fromSync(pool);
const consumer = shared.pull();
console.log(textSync(consumer)); // 'hello'

AltStyle によって変換されたページ (->オリジナル) /