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 support for onAbort argument on XHR.perform #48

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

bpinto
Copy link

@bpinto bpinto commented Dec 5, 2021

XHR.perform method creates a K.stream and emits value by binding to
some XMLHttpRequest events. That way you can be notified about this
request state changes through observable events.

As you might already guess activation and deactivation propagates
up the observables chain. For instance if one create a long chain like
Kefir.fromEvents(...).map(...).filter(...).take(...), the whole chain
will be inactive until first subscriber is added, and then it will
activate up to .fromEvents. Same for deactivation.

*Extracted from Kefir docs.

While the request is correctly aborted when the request observable is
unsubscribed, the event is not propagated to the observable chain given
they have already been unsubscribed. I'm not familiar with any solution
within Kefir realm that would allow us to be notified of such changes,
and while one could probably create an abstraction on top of
XHR.perform to run a custom code on abortion (unsubscription), it
seems weird not to provide a solution for this problem.

The proposal is to support a custom onAbort argument when calling
XHR.perform that will be executed when the request is deactivated and
subsequently aborted.

@bpinto bpinto force-pushed the on-abort-argument branch from 2a7701b to 127786b Compare December 5, 2021 13:39
`XHR.perform` method creates a `K.stream` and emits value by binding to
some `XMLHttpRequest` events. That way you can be notified about this
request state changes through observable events.

> As you might already guess *activation* and *deactivation* propagates
> up the observables chain. For instance if one create a long chain like
> `Kefir.fromEvents(...).map(...).filter(...).take(...)`, the whole chain
> will be *inactive* until first subscriber is added, and then it will
> *activate* up to `.fromEvents`. Same for *deactivation*.

*Extracted from [Kefir docs].

While the request is correctly aborted when the request observable is
unsubscribed, the abort event is not propagated to the observable chain
given they have already been unsubscribed. I'm not familiar with any
solution within Kefir realm that would allow us to be notified of such
changes, and while one could probably create an abstraction on top of
`XHR.perform` to run a custom code on abortion (unsubscription), it
seems weird not to provide a solution for this problem.

The proposal is to support a custom `onAbort` argument when calling
`XHR.perform` that will be executed when the request is deactivated and
subsequently aborted.

[Kefir docs]: https://kefirjs.github.io/kefir/#active-state
@bpinto bpinto force-pushed the on-abort-argument branch from 127786b to dcd95db Compare December 5, 2021 13:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant