Skip to content

Commit 8fc3c9c

Browse files
committed
Add type tests
1 parent 10b8ae7 commit 8fc3c9c

File tree

10 files changed

+1390
-43
lines changed

10 files changed

+1390
-43
lines changed

.flowconfig

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
[ignore]
2+
.*/node_modules/.*
3+
4+
[include]
5+
6+
[libs]
7+
8+
[lints]
9+
10+
[options]
11+
12+
[strict]

README.md

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,31 @@
33
# use-async-effect
44
:running: Asynchronous side effects, without the nonsense
55

6-
Installation:
6+
## Installation
77

8-
`npm install use-async-effect`
8+
```
9+
npm install use-async-effect
10+
```
11+
or
12+
```
13+
yarn add use-async-effect
14+
```
15+
16+
This package includes TypeScript and Flow types.
917

10-
Example:
18+
## Examples
1119

20+
Basic mount/unmount
1221
```javascript
1322
useAsyncEffect(async () => console.log('mount'), () => console.log('unmount'), []);
1423
```
1524

16-
Roadmap:
25+
Omitting destroy
26+
```javascript
27+
useAsyncEffect(async () => console.log('mount'), []);
28+
```
1729

18-
- [x] Typescript support.
30+
Handle effect result in destroy
31+
```javascript
32+
useAsyncEffect(() => fetch('url'), (result) => console.log(result));
33+
```

index.d.ts

Lines changed: 0 additions & 3 deletions
This file was deleted.

index.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
const { useEffect } = require('react');
22

33
module.exports.useAsyncEffect = (effect, destroy, inputs) => {
4+
const hasDestroy = typeof destroy === 'function';
5+
46
useEffect(() => {
57
let result;
68
effect().then((value) => result = value);
7-
8-
if(typeof destroy === 'function'){
9+
10+
if (hasDestroy) {
911
return () => destroy(result);
1012
}
11-
}, inputs);
13+
}, hasDestroy ? inputs : destroy);
1214
};

index.js.flow

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
declare module 'use-async-effect' {
2-
declare function useAsyncEffect<V>(
3-
effect: () => Promise<V>,
4-
destroy?: ?(result?: V) => void,
5-
inputs?: any[]
6-
): void;
2+
declare function useAsyncEffect(effect: () => Promise<any>, inputs?: any[]): void;
3+
4+
declare function useAsyncEffect<V>(effect: () => Promise<V>, destroy?: ?(result?: V) => void, inputs?: any[]): void;
75
}

0 commit comments

Comments
 (0)