Documentation / @eventkit/base / CallbackSubscriber
Class: CallbackSubscriber<T>
A specialized Subscriber that invokes a callback function for each value emitted by the observable.
CallbackSubscriber extends the base Subscriber class to provide a convenient way to process observable values through a callback function. When a value is emitted by the observable, the callback is scheduled to be executed via the observable's scheduler.
This class is typically used internally by the AsyncObservable.subscribe method to create a subscription that processes values through user-provided callbacks.
Extends
Subscriber
<T
>
Type Parameters
Type Parameter | Description |
---|---|
T | The type of values emitted by the observable |
Constructors
Constructor
new CallbackSubscriber<T>(observable, callback): CallbackSubscriber<T>
Creates a new CallbackSubscriber instance.
Parameters
Parameter | Type | Description |
---|---|---|
observable | AsyncObservable <T > | The AsyncObservable to subscribe to |
callback | SubscriberCallback <T > | The function to be called for each emitted value |
Returns
CallbackSubscriber
<T
>
Overrides
Properties
callback
protected callback: SubscriberCallback<T>;
Methods
cancel()
cancel(): Promise<void>
Cancels the subscriber, meaning that the generator will be disposed of, and any resources held by the subscriber will be released.
Calling this method starts an immediate cleanup of the Subscriber. In the case that you want to be notified of when the subscriber has closed without causing an interrupt, you can use the #then method.
Note that the promise returned by this method doesn't represent the actual execution of the generator, meaning that any errors that occur during the execution of the generator will not be reflected in the promise returned by this method. You can observe the status of the current execution by using the #then method or catching any errors using the #catch method. Because this class implements PromiseLike, you can also use the Subscriber in an await expression to yield the state of the generator's execution.
Returns
Promise
<void
>
A promise that resolves when the subscriber has been cleaned up.
Inherited from
catch()
catch<TResult>(onrejected?): Promise<TResult>
Attaches a callback for only the rejection of the Promise returned by this AsyncObservable. This is a shorthand for .then(undefined, onrejected).
Type Parameters
Type Parameter | Default type |
---|---|
TResult | never |
Parameters
Parameter | Type | Description |
---|---|---|
onrejected ? | null | (reason ) => TResult | PromiseLike <TResult > | The callback to execute when the Promise is rejected. This callback takes a reason parameter which contains the rejection reason. |
Returns
Promise
<TResult
>
A Promise for the completion of the callback. If the callback returns a value or a Promise that resolves, the returned Promise will resolve with that value. If the callback throws or returns a rejected Promise, the returned Promise will reject with that reason.
Inherited from
finally()
finally(onfinally?): Promise<any>
Schedules a cleanup action that gets executed when the subscriber 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
then()
then<TResult1, TResult2>(onfulfilled?, onrejected?): Promise<TResult1 | TResult2>
Returns a promise that resolves when the subscriber has completed execution and cleaned up, or rejects if an error occurs during. This allows Subscriber instances to be used with await expressions and Promise methods like then(), catch(), and finally().
It's worth noting that while the Promise returned by this object is representative of the execution of the generator, that doesn't mean that this is the only place where errors will be thrown. When using control flow statements like next()
or for-await-of loops, errors that occur either in evaluating the next value or in the cleanup when there are no more values will also be thrown there. In those cases, you can still use the promise returned here as a "catch all" for any errors that occur during the execution of the generator. This is helpful when you don't have visibility into the logic that iterates over the generator, but you still want to be notified of any errors that occur.
Type Parameters
Type Parameter | Default type |
---|---|
TResult1 | void |
TResult2 | never |
Parameters
Parameter | Type | Description |
---|---|---|
onfulfilled ? | null | (value ) => TResult1 | PromiseLike <TResult1 > | Optional callback to execute when the promise resolves successfully |
onrejected ? | null | (reason ) => TResult2 | PromiseLike <TResult2 > | Optional callback to execute when the promise rejects with an error |
Returns
Promise
<TResult1
| TResult2
>
A promise that resolves with the result of the onfulfilled/onrejected handlers