Skip to content

Add signal as an option to .subscribe #18

@hntrl

Description

@hntrl

Describe the solution

To create better hooks for common web APIs, it would be good to allow for an AbortSignal to be passed as an option to a subscriber:

const handler = () => {}
const controller = new AbortController();
const sub = obs.subscribe(handler, { signal: controller.signal });

(see #16, WICG/observable#12)


Basic framework for how this implementation should look like:

  • An optional options argument can be passed into .subscribe that has a field for an AbortSignal
  • An event listener gets added to the abort signal that kicks off the cleanup work for a subscriber when the signal gets aborted (effectively the same as calling sub.cancel

Where we'd add the options argument:

subscribe(callback?: SubscriberCallback<T>): Subscriber<T> {
if (!callback) callback = () => {};
const subscriber = new CallbackSubscriber(this, callback);
return subscriber;
}

Some Q's to answer with this implementation --

  • Say an AbortSignal gets emitted which kicks off the cleanup action. Now there isn't an entry point for us to await for the cleanup work to be finished! This is because the only place that we wait for cleanup work is in .dispose (which is called by .cancel, see ref)
    • A potential solution is to have all the cleanup work be added to the promise set for the subscriber instead

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions