Skip to content

Commit

Permalink
🏭 Abortable requests
Browse files Browse the repository at this point in the history
Closes #7
  • Loading branch information
elbywan committed Oct 9, 2017
1 parent f297512 commit c5888fe
Show file tree
Hide file tree
Showing 19 changed files with 291 additions and 60 deletions.
90 changes: 90 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -639,6 +639,96 @@ wretch("...").get().text(txt => console.log(txt))
## Extras
### Abortable requests (experimental)
*No polyfills for node.js yet ! Your browser absolutely needs to support [AbortControllers](https://developer.mozilla.org/en-US/docs/Web/API/AbortController).*
Use case :
```js
const [c, w] = wretch("...")
.get()
.onAbort(_ => console.log("Aborted !"))
.controller()

w.text(_ => console.log("should never be called"))
c.abort()

// Or :

const controller = new AbortController()

wretch("...")
.signal(controller)
.get()
.onAbort(_ => console.log("Aborted !"))
.text(_ => console.log("should never be called"))

c.abort()
```
### signal(controller: AbortController)
*Used at "request time", like an helper.*
Associates a custom controller with the request.
Useful when you need to use your own AbortController, otherwise wretch will create a new controller itself.
```js
const controller = new AbortController()

// Associates the same controller with multiple requests

wretch("url1")
.signal(controller)
.get()
.json(_ => /* ... */)
wretch("url2")
.signal(controller)
.get()
.json(_ => /* ... */)

// Aborts both requests

controller.abort()
```
#### setTimeout(time: number, controller?: AbortController)
*Used at "response time".*
Aborts the request after a fixed time. If you use a custom AbortController associated with the request, pass it as the second argument.
```js
// 1 second timeout
wretch("...").get().setTimeout(1000).json(_ => /* will not be called in case of a timeout */)
```
#### controller()
*Used at "response time".*
Returns the automatically generated AbortController alongside the current wretch response as a pair.
```js
// We need the controller outside the chain
const [c, w] = wretch("url")
.get()
.controller()

// Resume with the chain
w.onAbort(_ => console.log("ouch")).json(_ => /* ... */)

/* Later on ... */
c.abort()
```
#### onAbort(cb: (error: AbortError) => any)
*Used at "response time" like a catcher.*
Catch an AbortError and perform the callback.
### Performance API (experimental)
#### perfs(cb: (timings: PerformanceTiming) => void)
Expand Down
2 changes: 1 addition & 1 deletion dist/bundle/wretch.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/bundle/wretch.js.map

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion dist/config.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ declare const config: {
URLSearchParams: any;
performance: any;
PerformanceObserver: any;
AbortController: any;
};
polyfill(p: string, doThrow?: boolean): any;
polyfill(p: string, doThrow?: boolean, instance?: boolean, ...args: any[]): any;
};
export default config;
12 changes: 9 additions & 3 deletions dist/config.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/config.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion dist/resolver.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,14 @@ export declare const resolver: (url: any) => (catchers?: Map<number, (error: Wre
arrayBuffer: <Result = ArrayBuffer>(cb?: (type: ArrayBuffer) => Result) => Promise<Result>;
text: <Result = string>(cb?: (type: string) => Result) => Promise<Result>;
perfs: (cb?: (type: any) => void) => any;
error: (code: number, cb: any) => any;
setTimeout: (time: number, controller: any) => any;
controller: () => [any, any];
error: (code: string | number, cb: any) => any;
badRequest: (cb: (error: WretcherError) => void) => any;
unauthorized: (cb: (error: WretcherError) => void) => any;
forbidden: (cb: (error: WretcherError) => void) => any;
notFound: (cb: (error: WretcherError) => void) => any;
timeout: (cb: (error: WretcherError) => void) => any;
internalError: (cb: (error: WretcherError) => void) => any;
onAbort: (cb: (error: Error) => void) => any;
};
Loading

0 comments on commit c5888fe

Please sign in to comment.