Skip to content

Commit

Permalink
feat(signal): remove passed signal from defautlMapOptionsToKey (#83)
Browse files Browse the repository at this point in the history
* feat(signal): remove passed signal from defautlMapOptionsToKey

* feat(signal): update test to test if signal is removed from snapshot

* feat(signal): update readme

* feat(signal): update readme

* feat(signal): update wording
  • Loading branch information
dogpatch626 authored Aug 24, 2023
1 parent eed68a4 commit 8bb4b36
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 2 deletions.
25 changes: 25 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,31 @@ const NewBookForm = () => {
};
```
### Abort Inflight Requests
When you neeed to abort the execution of requests inflight, passing a signal from the [Abort Controller](https://developer.mozilla.org/en-US/docs/Web/API/AbortController) API to `useFetchye` as an option will enable this.
Considering `useFetchye` is a wrapper around fetch, passing a signal is the same and provides the same functionality as demonstrated below.
```jsx
import React, { useEffect } from 'react';
import { useFetchye } from 'fetchye';

const AbortComponent = () => {
const controller = new AbortController();
useFetchye('http://example.com/api/books', { signal: controller.signal });

useEffect(() => () => controller.abort(), []);

return (
<div>
<h1>abortable component</h1>
</div>
);
};
```
Instead of setting up a `useEffect` within the component it's possible to pass a hook to signal using packages such as
[use-unmount-signal](https://www.npmjs.com/package/use-unmount-signal/v/1.0.0).
### Sequential API Execution
Passing the 'isLoading' value from one useFetchye call to the 'defer' field of the next will prevent the second call from being made until the first has loaded.
Expand Down
6 changes: 4 additions & 2 deletions packages/fetchye/__tests__/defaultMapOptionsToKey.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@
import { defaultMapOptionsToKey } from '../src/defaultMapOptionsToKey';

describe('defaultMapOptionsToKey', () => {
it('should return an object without passed defer or mapOptionsToKey', () => {
expect(defaultMapOptionsToKey({ defer: true, mapOptionsToKey: () => {}, method: 'POST' }))
it('should return an object without passed signal, defer, or mapOptionsToKey', () => {
expect(defaultMapOptionsToKey({
signal: {}, defer: true, mapOptionsToKey: () => { }, method: 'POST',
}))
.toMatchInlineSnapshot(`
Object {
"method": "POST",
Expand Down
1 change: 1 addition & 0 deletions packages/fetchye/src/defaultMapOptionsToKey.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

export const defaultMapOptionsToKey = (options) => {
const {
signal,
defer,
mapOptionsToKey,
initialData,
Expand Down

0 comments on commit 8bb4b36

Please sign in to comment.