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
Open
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ parameters have their XHR default values:
| [`responseType`](https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/responseType) | `''` | Specifies type of [response](#XHR-response) data. |
| [`timeout`](https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/timeout) | `0` | Number of milliseconds or `0` for infinite. |
| [`withCredentials`](https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/withCredentials) | `false` | Whether cross-site `Access-Control` should use credentials. |
| [`onAbort`](https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/abort) | `undefined` | A function to be called if the request is aborted as a consequence of the observable being unsubscribed. |

In addition to a plain object, the argument to `XHR.perform` is allowed to be an
observable property or contain observable properties, in which case the property
Expand Down
7 changes: 6 additions & 1 deletion src/karet.xhr.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ const performPlain = (process.env.NODE_ENV === 'production'
body: V.optional(V.accept),
headers: V.propsOr(headerValue, I.object0),
method: V.optional(string),
onAbort: V.optional(I.isFunction),
overrideMimeType: V.optional(string),
password: V.optional(string),
responseType: V.optional(string),
Expand All @@ -135,6 +136,7 @@ const performPlain = (process.env.NODE_ENV === 'production'
return delayUnsub(
K.stream(({emit, end}) => {
const method = args.method
const onAbort = args.onAbort
const user = args.user
const password = args.password
const headers = args.headers
Expand Down Expand Up @@ -176,7 +178,10 @@ const performPlain = (process.env.NODE_ENV === 'production'
}
xhr.send(isNil(body) ? null : body)
return () => {
if (!xhr[STATUS]) xhr.abort()
if (!xhr[STATUS]) {
xhr.abort()
onAbort && onAbort()
}
}
})
)
Expand Down