Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add addTeardown(). Closes #3. Closes #19. #61

Merged
merged 4 commits into from
Sep 13, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 18 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,7 @@ interface Subscriber {
undefined next(any result);
undefined complete();
undefined error(any error);
undefined addTeardown(VoidFunction teardown);

readonly attribute AbortSignal signal;
};
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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.
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Technically I don't think this was needed because of https://github.com/domfarolino/observable/pull/61/files#diff-b335630551682c19a781afebcf4d07bf978fb1f8ac04c6bf87428ed5106870f5R466-R467 (i.e., we're already in the "Otherwise" branch here) but I guess it can't hurt?



### Operators

Expand Down