Documentation / @eventkit/base / Subscriber
Class: Subscriber<T>
Represents an active execution of an observable.
Subscribers are a common type in eventkit, but is rarely used as a public interface. They should be initialized using the AsyncObservable.subscribe method or by using the AsyncObservable like an async iterator.
Extended by
Type Parameters
Type Parameter | Description |
---|---|
T | The type of values being handled by the subscriber |
Implements
SubscriptionLike
PromiseLike
<void
>AsyncIterable
<T
,void
,void
>
Constructors
Constructor
new Subscriber<T>(observable): Subscriber<T>
Parameters
Parameter | Type |
---|---|
observable | AsyncObservable <T > |
Returns
Subscriber
<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.
Implementation of
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.
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
next()
next(): Promise<IteratorResult<T, any>>
Returns a promise that resolves with the next iterator result from the generator. You should only use this method if you constructed a Subscriber independently of AsyncObservable.subscribe or CallbackSubscriber. Using this method in that instance will hijack the execution of the generator which can cause unexpected behavior.
Returns
Promise
<IteratorResult
<T
, any
>>
A promise that resolves with the next iterator result from the generator.
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
Implementation of
PromiseLike.then