@etsoo/react 1.5.98 → 1.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/__tests__/EventWatcher.tsx +27 -0
- package/__tests__/States.tsx +5 -1
- package/lib/app/EventWatcher.d.ts +35 -0
- package/lib/app/EventWatcher.js +43 -0
- package/lib/index.d.ts +1 -0
- package/lib/index.js +1 -0
- package/lib/uses/useAsyncState.d.ts +4 -0
- package/package.json +1 -1
- package/src/app/EventWatcher.ts +66 -0
- package/src/index.ts +1 -0
- package/src/uses/useAsyncState.ts +24 -4
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { render, screen } from '@testing-library/react';
|
|
2
|
+
import React from 'react';
|
|
3
|
+
import { EventWatcher } from '../src/app/EventWatcher';
|
|
4
|
+
|
|
5
|
+
function App(props: { callback: () => void }) {
|
|
6
|
+
const { callback } = props;
|
|
7
|
+
const watcher = new EventWatcher();
|
|
8
|
+
|
|
9
|
+
React.useEffect(() => {
|
|
10
|
+
watcher.add({
|
|
11
|
+
type: 'click',
|
|
12
|
+
action: (_event) => callback(),
|
|
13
|
+
once: true
|
|
14
|
+
});
|
|
15
|
+
}, []);
|
|
16
|
+
return <button onClick={(event) => watcher.do(event)}></button>;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
test('Tests for EventWatcher', () => {
|
|
20
|
+
const callback = jest.fn();
|
|
21
|
+
render(<App callback={callback} />);
|
|
22
|
+
const button = screen.getByRole<HTMLButtonElement>('button');
|
|
23
|
+
button.click();
|
|
24
|
+
expect(callback).toBeCalled();
|
|
25
|
+
button.click();
|
|
26
|
+
expect(callback).toBeCalledTimes(1);
|
|
27
|
+
});
|
package/__tests__/States.tsx
CHANGED
|
@@ -1,11 +1,15 @@
|
|
|
1
1
|
import React from 'react';
|
|
2
|
-
import { render, screen
|
|
2
|
+
import { render, screen } from '@testing-library/react';
|
|
3
3
|
import { useAsyncState } from '../src/uses/useAsyncState';
|
|
4
4
|
import { act } from 'react-dom/test-utils';
|
|
5
5
|
|
|
6
6
|
function App(props: { callback: (state: number) => void }) {
|
|
7
7
|
const { callback } = props;
|
|
8
8
|
const [state, setState] = useAsyncState(0);
|
|
9
|
+
|
|
10
|
+
const [state1] = useAsyncState<number>();
|
|
11
|
+
expect(state1).toBeUndefined();
|
|
12
|
+
|
|
9
13
|
const click = async () => {
|
|
10
14
|
const currentState = await setState((prev) => prev + 1);
|
|
11
15
|
callback(currentState + 1);
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
/**
|
|
3
|
+
* Event watcher action
|
|
4
|
+
*/
|
|
5
|
+
export interface EventWatcherAction {
|
|
6
|
+
/**
|
|
7
|
+
* Event type
|
|
8
|
+
*/
|
|
9
|
+
type?: string | string[];
|
|
10
|
+
/**
|
|
11
|
+
* Once action or not
|
|
12
|
+
*/
|
|
13
|
+
once?: boolean;
|
|
14
|
+
/**
|
|
15
|
+
* Action
|
|
16
|
+
*/
|
|
17
|
+
action: (event: React.BaseSyntheticEvent | string) => void | PromiseLike<void>;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Event watcher
|
|
21
|
+
*/
|
|
22
|
+
export declare class EventWatcher {
|
|
23
|
+
private actions;
|
|
24
|
+
/**
|
|
25
|
+
* Add action
|
|
26
|
+
* @param action Action
|
|
27
|
+
*/
|
|
28
|
+
add(action: EventWatcherAction): void;
|
|
29
|
+
/**
|
|
30
|
+
* Do the event
|
|
31
|
+
* @param event Event
|
|
32
|
+
*/
|
|
33
|
+
do(event: React.BaseSyntheticEvent | string): void;
|
|
34
|
+
private isMatch;
|
|
35
|
+
}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Event watcher
|
|
3
|
+
*/
|
|
4
|
+
export class EventWatcher {
|
|
5
|
+
constructor() {
|
|
6
|
+
this.actions = [];
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* Add action
|
|
10
|
+
* @param action Action
|
|
11
|
+
*/
|
|
12
|
+
add(action) {
|
|
13
|
+
this.actions.push(action);
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Do the event
|
|
17
|
+
* @param event Event
|
|
18
|
+
*/
|
|
19
|
+
do(event) {
|
|
20
|
+
// Type
|
|
21
|
+
const type = typeof event === 'string' ? event : event.type;
|
|
22
|
+
// Execute
|
|
23
|
+
const removeIndices = [];
|
|
24
|
+
this.actions.forEach((item, index) => {
|
|
25
|
+
if (!this.isMatch(item, type))
|
|
26
|
+
return;
|
|
27
|
+
item.action(event);
|
|
28
|
+
if (item.once)
|
|
29
|
+
removeIndices.push(index);
|
|
30
|
+
});
|
|
31
|
+
// Remove all once actions
|
|
32
|
+
removeIndices
|
|
33
|
+
.reverse()
|
|
34
|
+
.forEach((index) => this.actions.splice(index, 1));
|
|
35
|
+
}
|
|
36
|
+
isMatch(action, type) {
|
|
37
|
+
if (action.type == null)
|
|
38
|
+
return true;
|
|
39
|
+
if (typeof action.type === 'string')
|
|
40
|
+
return action.type === type;
|
|
41
|
+
return action.type.includes(type);
|
|
42
|
+
}
|
|
43
|
+
}
|
package/lib/index.d.ts
CHANGED
package/lib/index.js
CHANGED
|
@@ -1,4 +1,8 @@
|
|
|
1
1
|
import React from 'react';
|
|
2
|
+
export declare function useAsyncState<S = undefined>(): [
|
|
3
|
+
S | undefined,
|
|
4
|
+
(newState: React.SetStateAction<S | undefined>) => Promise<S | undefined>
|
|
5
|
+
];
|
|
2
6
|
/**
|
|
3
7
|
* Returns a stateful value, and a async function to update it.
|
|
4
8
|
* @param initialState initial stat
|
package/package.json
CHANGED
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Event watcher action
|
|
5
|
+
*/
|
|
6
|
+
export interface EventWatcherAction {
|
|
7
|
+
/**
|
|
8
|
+
* Event type
|
|
9
|
+
*/
|
|
10
|
+
type?: string | string[];
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Once action or not
|
|
14
|
+
*/
|
|
15
|
+
once?: boolean;
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Action
|
|
19
|
+
*/
|
|
20
|
+
action: (
|
|
21
|
+
event: React.BaseSyntheticEvent | string
|
|
22
|
+
) => void | PromiseLike<void>;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Event watcher
|
|
27
|
+
*/
|
|
28
|
+
export class EventWatcher {
|
|
29
|
+
private actions: EventWatcherAction[] = [];
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Add action
|
|
33
|
+
* @param action Action
|
|
34
|
+
*/
|
|
35
|
+
add(action: EventWatcherAction) {
|
|
36
|
+
this.actions.push(action);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Do the event
|
|
41
|
+
* @param event Event
|
|
42
|
+
*/
|
|
43
|
+
do(event: React.BaseSyntheticEvent | string) {
|
|
44
|
+
// Type
|
|
45
|
+
const type = typeof event === 'string' ? event : event.type;
|
|
46
|
+
|
|
47
|
+
// Execute
|
|
48
|
+
const removeIndices: number[] = [];
|
|
49
|
+
this.actions.forEach((item, index) => {
|
|
50
|
+
if (!this.isMatch(item, type)) return;
|
|
51
|
+
item.action(event);
|
|
52
|
+
if (item.once) removeIndices.push(index);
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
// Remove all once actions
|
|
56
|
+
removeIndices
|
|
57
|
+
.reverse()
|
|
58
|
+
.forEach((index) => this.actions.splice(index, 1));
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
private isMatch(action: EventWatcherAction, type: string) {
|
|
62
|
+
if (action.type == null) return true;
|
|
63
|
+
if (typeof action.type === 'string') return action.type === type;
|
|
64
|
+
return action.type.includes(type);
|
|
65
|
+
}
|
|
66
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -1,5 +1,10 @@
|
|
|
1
1
|
import React from 'react';
|
|
2
2
|
|
|
3
|
+
export function useAsyncState<S = undefined>(): [
|
|
4
|
+
S | undefined,
|
|
5
|
+
(newState: React.SetStateAction<S | undefined>) => Promise<S | undefined>
|
|
6
|
+
];
|
|
7
|
+
|
|
3
8
|
/**
|
|
4
9
|
* Returns a stateful value, and a async function to update it.
|
|
5
10
|
* @param initialState initial stat
|
|
@@ -7,12 +12,27 @@ import React from 'react';
|
|
|
7
12
|
*/
|
|
8
13
|
export function useAsyncState<S>(
|
|
9
14
|
initialState: S | (() => S)
|
|
10
|
-
): [S, (newState: React.SetStateAction<S>) => Promise<S>]
|
|
15
|
+
): [S, (newState: React.SetStateAction<S>) => Promise<S>];
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Returns a stateful value, and a async function to update it.
|
|
19
|
+
* @param initialState initial stat
|
|
20
|
+
* @returns Current state and update action
|
|
21
|
+
*/
|
|
22
|
+
export function useAsyncState<S>(
|
|
23
|
+
initialState?: S | (() => S)
|
|
24
|
+
): [
|
|
25
|
+
S | undefined,
|
|
26
|
+
(newState: React.SetStateAction<S | undefined>) => Promise<S | undefined>
|
|
27
|
+
] {
|
|
11
28
|
// State
|
|
12
29
|
const [state, setState] = React.useState(initialState);
|
|
13
30
|
|
|
14
31
|
// Resolve sate
|
|
15
|
-
const resolveState =
|
|
32
|
+
const resolveState =
|
|
33
|
+
React.useRef<
|
|
34
|
+
(value: S | undefined | PromiseLike<S | undefined>) => void
|
|
35
|
+
>();
|
|
16
36
|
|
|
17
37
|
// Is mounted or not
|
|
18
38
|
const isMounted = React.useRef(false);
|
|
@@ -32,8 +52,8 @@ export function useAsyncState<S>(
|
|
|
32
52
|
}, [state]);
|
|
33
53
|
|
|
34
54
|
const setAsyncState = React.useCallback(
|
|
35
|
-
(newState: React.SetStateAction<S>) =>
|
|
36
|
-
new Promise<S>((resolve) => {
|
|
55
|
+
(newState: React.SetStateAction<S | undefined>) =>
|
|
56
|
+
new Promise<S | undefined>((resolve) => {
|
|
37
57
|
if (isMounted.current) {
|
|
38
58
|
resolveState.current = resolve;
|
|
39
59
|
setState(newState);
|