Skip to content

Commit 18cd920

Browse files
Fixed typings;
Updated README.md; Added ECOSYSTEM.md;
1 parent 81f20f3 commit 18cd920

File tree

3 files changed

+59
-22
lines changed

3 files changed

+59
-22
lines changed

ECOSYSTEM.md

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Ecosystem
2+
3+
This is a list of `CPromise` related libraries and resources.
4+
5+
## Libraries
6+
7+
### General
8+
9+
* [c-promise2](https://www.npmjs.com/package/c-promise2) - Advanced cancellable promise that should be used with [`use-async-effect2`](https://www.npmjs.com/package/use-async-effect2) to get the most out of it
10+
11+
### React
12+
* [use-async-effect](https://www.npmjs.com/package/use-async-effect2) (🔴 this library)- Feature-rich React async hooks that built on top of the cancellable promises ([`CPromise`](https://www.npmjs.com/package/c-promise2))
13+
14+
### Data fetching
15+
* [cp-axios](https://www.npmjs.com/package/cp-axios) - Axios cancellable wrapper that supports CPromise context. Can be directly used in [`use-async-effect2`](https://www.npmjs.com/package/use-async-effect2) or general CPromise context
16+
* [cp-fetch](https://www.npmjs.com/package/cp-fetch) - Cross-platform fetch wrapper that can be used in cooperation with [`use-async-effect2`](https://www.npmjs.com/package/use-async-effect2) or general [`CPromise`](https://www.npmjs.com/package/c-promise2) chains
17+
18+
### Server application framework
19+
* [cp-koa](https://www.npmjs.com/package/cp-koa) - Wrapper for [`koa`](https://www.npmjs.com/package/koa), that implements cancellable middlewares/routes to the framework

README.md

+28-21
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,15 @@
66

77
## useAsyncEffect2 :snowflake:
88

9-
This library makes it possible to use cancellable async effects inside React components by providing asynchronous
10-
versions of the `useEffect` and` useCallback` hooks. These hooks can cancel asynchronous code inside it due to timeouts,
11-
user requests, or automatically when a component is unmounted or some dependency has changed.
9+
This library provides an async belt for the React components as:
10+
- `useAsyncEffect` - deeply cancellable asynchronous effects that can be cleared (canceled) on component unmounting, timeout, or by user request.
11+
- `useAsyncCallback` - cancellable async callbacks
12+
- `useAsyncDeepState` - to define a deep state, the actual values of which can be accessed from an async routine
13+
- `useAsyncWatcher` - to watch for state updates in a promise flow
1214

1315
The library is designed to make it as easy as possible to use complex and composite asynchronous routines
1416
in React components. It works on top of a [custom cancellable promise](https://www.npmjs.com/package/c-promise2),
15-
simplifying the solution to many common challenges with asynchronous code. Can be composed with cancellable version of `Axios`
17+
simplifying the solution to many common challenges with asynchronous tasks. Can be composed with cancellable version of `Axios`
1618
([cp-axios](https://www.npmjs.com/package/cp-axios)) and `fetch API` ([cp-fetch](https://www.npmjs.com/package/cp-fetch))
1719
to get auto cancellable React async effects/callbacks with network requests.
1820

@@ -22,15 +24,18 @@ to get auto cancellable React async effects/callbacks with network requests.
2224
and `async()=>{}` or `async function()` with `function*`:
2325

2426
````javascript
25-
// plain React useEffect hook
27+
// plain React effect using `useEffect` hook
2628
useEffect(()=>{
27-
(async()=>{
29+
const doSomething = async()=>{
2830
await somePromiseHandle;
29-
})();
31+
setStateVar('foo');
32+
};
33+
doSomething();
3034
}, [])
31-
// useAsyncEffect React hook
35+
// auto-cleanable React async effect using `useAsyncEffect` hook
3236
useAsyncEffect(function*(){
3337
yield somePromiseHandle;
38+
setStateVar('foo');
3439
}, [])
3540
````
3641

@@ -110,21 +115,22 @@ you get a powerful tool for building asynchronous logic of React components.
110115

111116
A tiny `useAsyncEffect` demo with JSON fetching using internal states:
112117

113-
[Live demo to play](https://codesandbox.io/s/use-async-effect-fetch-tiny-ui-states-g5qd7)
118+
[Live demo to play](https://codesandbox.io/s/use-async-effect-axios-minimal-pdngg?file=/src/TestComponent.js)
114119

115120
````javascript
116-
export default function JSONViewer(props) {
117-
const [cancel, done, result]= useAsyncEffect(function*(){
118-
return (yield cpAxios(props.url)).data;
119-
}, {states: true})
120-
121-
return (
122-
<div className="component">
123-
<div className="caption">useAsyncEffect demo:</div>
124-
<div>{done? JSON.stringify(result) : "loading..."}</div>
125-
<button onClick={cancel}>Cancel async effect</button>
126-
</div>
127-
);
121+
function JSONViewer({ url, timeout }) {
122+
const [cancel, done, result, err] = useAsyncEffect(function* () {
123+
return (yield cpAxios(url).timeout(timeout)).data;
124+
}, { states: true });
125+
126+
return (
127+
<div>
128+
{done ? (err ? err.toString() : JSON.stringify(result)) : "loading..."}
129+
<button className="btn btn-warning" onClick={cancel} disabled={done}>
130+
Cancel async effect (abort request)
131+
</button>
132+
</div>
133+
);
128134
}
129135
````
130136

@@ -411,6 +417,7 @@ Generator context (`this`) refers to the CPromise instance.
411417
- `options.deps?: any[]` - effect dependencies
412418
- `options.skipFirst?: boolean` - skip first render
413419
- `options.states: boolean= false` - use states
420+
- `options.once: boolean= false` - run the effect only once (the effect's async routine should be fully completed)
414421

415422
#### Available states vars:
416423
- `done: boolean` - the function execution is completed (with success or failure)

use-async-effect.d.ts

+12-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,11 @@ export interface UseAsyncEffectOptions {
4545
/**
4646
* @default false
4747
*/
48-
states?: boolean
48+
states?: boolean,
49+
/**
50+
* @default false
51+
*/
52+
once?: boolean
4953
}
5054

5155
export type CancelReason = string | Error;
@@ -91,7 +95,14 @@ export interface CPromiseGenerator {
9195
}
9296

9397
export interface UseAsyncDeepStateOptions {
98+
/**
99+
* @default true
100+
*/
94101
watch?: boolean;
102+
/**
103+
* @default true
104+
*/
105+
defineSetters?: boolean;
95106
}
96107

97108
export function useAsyncEffect(generator: CPromiseGenerator, deps?: any[]): AsyncEffectCancelFn

0 commit comments

Comments
 (0)