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
new Stream<T>(init?): Stream<T>
Creates a new Stream instance.
Parameters
Parameter | Type |
---|---|
init ? | StreamInit <T > |
Returns
Stream
<T
>
Overrides
AsyncObservable<T>.constructor
Accessors
closed
Get Signature
get closed(): boolean
Returns true if this stream has been closed and is no longer accepting new values.
Returns
boolean
observed
Get Signature
get observed(): boolean
Returns true if this stream has any active observers.
Returns
boolean
subscribers
Get Signature
get subscribers(): Subscriber<T>[]
Returns
Subscriber
<T
>[]
Inherited from
Methods
asObservable()
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 that this Stream casts to
cancel()
cancel(): Promise<void>
Signals completion to all observers and closes the stream.
Returns
Promise
<void
>
Overrides
drain()
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
finally()
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
Parameter | Type | Description |
---|---|---|
onfinally ? | null | () => void | Optional callback to execute after completion or error |
Returns
Promise
<any
>
A promise that resolves when the action has completed
Inherited from
push()
push(value): void
Feeds a new value to all observers of this stream.
Parameters
Parameter | Type | Description |
---|---|---|
value | T | The value to emit to all observers |
Returns
void
subscribe()
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
Parameter | Type |
---|---|
callback ? | SubscriberCallback <T > |
Returns
Subscriber
<T
>