diff --git a/README.md b/README.md index f0e275e..d04a967 100644 --- a/README.md +++ b/README.md @@ -307,6 +307,7 @@ interface Subscriber { undefined next(any result); undefined complete(); undefined error(any error); + undefined addTeardown(VoidFunction teardown); readonly attribute AbortSignal signal; }; @@ -378,9 +379,6 @@ observable.subscribe({ }); ``` -**Issue**: See https://github.com/domfarolino/observable/issues/3 about having -the Observable constructor being able to register teardown upon unsubscription. - While custom Observables can be useful on their own, the primary use case they unlock is with event handling. Observables returned by the new `EventTarget#on()` method are created natively with an internal callback that @@ -457,6 +455,23 @@ observable.subscribe({next: data => { }, signal: controller.signal}); ``` +#### Teardown + +It is critical for an Observable subscriber to be able to register an arbitrary +teardown callback to clean up any resources relevant to the subscription. The +teardown can be registered from within the subscription callback passed into the +`Observable` constructor. When run (upon subscribing), the subscription callback +can register a teardown function via `subscriber.addTeardown()`. + +If the subscriber has already been aborted (i.e., `subscriber.signal.aborted` is +`true`), then the given teardown callback is invoked immediately from within +`addTeardown()`. Otherwise, it is invoked synchronously: + + - From `complete()`, after the subscriber's complete handler (if any) is + invoked + - From `error()`, after the subscriber's error handler (if any) is invoked + - The signal passed to the subscription is aborted by the user. + ### Operators