Skip to content

Documentation / @eventkit/base / Stream

Class: Stream<T> ​

A Stream is a special type of AsyncObservable that allows values to be multicasted to many observers. Streams are like EventEmitters.

Every Stream is an AsyncObservable and can be used as a value producer. You can subscribe to a Stream, and you can call push to feed values to all observers.

Extends ​

Type Parameters ​

Type Parameter
T

Constructors ​

Constructor ​

ts
new Stream<T>(init?): Stream<T>

Creates a new Stream instance.

Parameters ​

ParameterType
init?StreamInit<T>

Returns ​

Stream<T>

Overrides ​

ts
AsyncObservable<T>.constructor

Accessors ​

closed ​

Get Signature ​

ts
get closed(): boolean

Returns true if this stream has been closed and is no longer accepting new values.

Returns ​

boolean


observed ​

Get Signature ​

ts
get observed(): boolean

Returns true if this stream has any active observers.

Returns ​

boolean


subscribers ​

Get Signature ​

ts
get subscribers(): Subscriber<T>[]
Returns ​

Subscriber<T>[]

Inherited from ​

AsyncObservable.subscribers

Methods ​

asObservable() ​

ts
asObservable(): AsyncObservable<T>

Creates a new AsyncObservable with this Stream as the source. You can do this to create custom observer-side logic of the Stream and conceal it from code that uses the AsyncObservable.

Returns ​

AsyncObservable<T>

AsyncObservable that this Stream casts to


cancel() ​

ts
cancel(): Promise<void>

Signals completion to all observers and closes the stream.

Returns ​

Promise<void>

Overrides ​

AsyncObservable.cancel


drain() ​

ts
drain(): Promise<void>

Returns a promise that resolves when all the work scheduled against the observable has completed (i.e. subscriber callbacks or cleanup handlers).

Returns ​

Promise<void>

Inherited from ​

AsyncObservable.drain


finally() ​

ts
finally(onfinally?): Promise<any>

Schedules a cleanup action that gets executed when the observable is disposed of. Optionally, a callback can be provided to inform the behavior of the created action.

Parameters ​

ParameterTypeDescription
onfinally?null | () => voidOptional callback to execute after completion or error

Returns ​

Promise<any>

A promise that resolves when the action has completed

Inherited from ​

AsyncObservable.finally


push() ​

ts
push(value): void

Feeds a new value to all observers of this stream.

Parameters ​

ParameterTypeDescription
valueTThe value to emit to all observers

Returns ​

void


subscribe() ​

ts
subscribe(callback?): Subscriber<T>

Registers and returns a new Subscriber that will call the provided callback for each value emitted by the Stream. The callback will be passed the value emitted to the Stream as an argument.

While this method is similar to AsyncObservable's subscribe method, the key difference is that the generator that is passed to the Subscriber is a unique implementation that emulates the desired behavior of the Stream.

You can still use the returned Subscriber object like a Promise which can be awaited to wait for the Stream to be closed.

Parameters ​

ParameterType
callback?SubscriberCallback<T>

Returns ​

Subscriber<T>

Overrides ​

AsyncObservable.subscribe

Released under the MIT License.