real-cancellable-promise
Advanced tools
Comparing version 1.1.1 to 1.1.2
@@ -0,1 +1,8 @@ | ||
## 1.1.2 | ||
### Bug Fixes | ||
- Make the `capture` function of `buildCancellablePromise` an identity function | ||
from a type perspective. | ||
## 1.1.1 | ||
@@ -2,0 +9,0 @@ |
@@ -0,0 +0,0 @@ /** |
@@ -0,0 +0,0 @@ /** |
export * from './Cancellation'; | ||
export * from './CancellablePromise'; | ||
export * from './utils'; |
@@ -0,0 +0,0 @@ 'use strict'; |
/** @internal */ | ||
export declare const noop: () => void; |
@@ -14,3 +14,3 @@ import { CancellablePromise } from './CancellablePromise'; | ||
*/ | ||
export declare type CaptureCancellablePromise = <T>(promise: CancellablePromise<T>) => CancellablePromise<T>; | ||
export declare type CaptureCancellablePromise = <P extends CancellablePromise<unknown>>(promise: P) => P; | ||
/** | ||
@@ -40,2 +40,2 @@ * Used to build a single [[`CancellablePromise`]] from a multi-step asynchronous | ||
*/ | ||
export declare function buildCancellablePromise<T>(innerFunc: (capture: CaptureCancellablePromise) => Promise<T>): CancellablePromise<T>; | ||
export declare function buildCancellablePromise<T>(innerFunc: (capture: CaptureCancellablePromise) => PromiseLike<T>): CancellablePromise<T>; |
{ | ||
"name": "real-cancellable-promise", | ||
"version": "1.1.1", | ||
"version": "1.1.2", | ||
"description": "A simple cancellable promise implementation that cancels the underlying HTTP call.", | ||
@@ -68,3 +68,3 @@ "keywords": [ | ||
}, | ||
"packageManager": "yarn@3.0.2" | ||
"packageManager": "yarn@3.3.1" | ||
} |
@@ -7,3 +7,2 @@ # real-cancellable-promise | ||
- ⚛ Built with React in mind — no more "setState after unmount" warnings! | ||
- ⚡ Compatible with [fetch](#fetch), [axios](#axios), and | ||
@@ -15,2 +14,3 @@ [jQuery.ajax](#jQuery) | ||
- 💻 Optimized for TypeScript | ||
- ⚛ Built with React in mind | ||
- 🔎 Compatible with | ||
@@ -326,55 +326,2 @@ [react-query](https://react-query.tanstack.com/guides/query-cancellation) | ||
## React: `useCancellablePromiseCleanup` | ||
Here's a React hook that facilitates cancellation of `CancellablePromise`s that | ||
occur outside of `useEffect`. Any captured API calls will be canceled when the | ||
component unmounts. (Just be sure this is what you want to happen.) | ||
```ts | ||
export function useCancellablePromiseCleanup(): CaptureCancellablePromise { | ||
const cancellablePromisesRef = useRef<CancellablePromise<unknown>[]>([]) | ||
useEffect( | ||
() => () => { | ||
for (const promise of cancellablePromisesRef.current) { | ||
promise.cancel() | ||
} | ||
}, | ||
[] | ||
) | ||
const capture: CaptureCancellablePromise = useCallback((promise) => { | ||
cancellablePromisesRef.current.push(promise) | ||
return promise | ||
}, []) | ||
return capture | ||
} | ||
``` | ||
Then in your React components... | ||
```tsx | ||
function updateUser(id: number, name: string): CancellablePromise<void> { | ||
// call the API | ||
} | ||
export function UserDetail(props: UserDetailProps) { | ||
const capture = useCancellablePromiseCleanup() | ||
async function saveChanges(): Promise<void> { | ||
try { | ||
await capture(updateUser(id, name)) | ||
} catch { | ||
// ... | ||
} | ||
} | ||
return <div>...</div> | ||
} | ||
``` | ||
[CodeSandbox: | ||
useCancellablePromiseCleanup](https://codesandbox.io/s/real-cancellable-promise-usecancellablepromisecleanup-0jozc?file=/src/App.tsx) | ||
# Supported Platforms | ||
@@ -381,0 +328,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
44420
339