@etsoo/react 1.5.96 → 1.5.98

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.
@@ -0,0 +1,35 @@
1
+ import React from 'react';
2
+ import { render, screen, waitFor } from '@testing-library/react';
3
+ import { useAsyncState } from '../src/uses/useAsyncState';
4
+ import { act } from 'react-dom/test-utils';
5
+
6
+ function App(props: { callback: (state: number) => void }) {
7
+ const { callback } = props;
8
+ const [state, setState] = useAsyncState(0);
9
+ const click = async () => {
10
+ const currentState = await setState((prev) => prev + 1);
11
+ callback(currentState + 1);
12
+ };
13
+ callback(state);
14
+ return <button onClick={click}>State: {state}</button>;
15
+ }
16
+
17
+ test('Tests for useAsyncState', (done) => {
18
+ const callback = jest.fn();
19
+ render(<App callback={callback} />);
20
+ const button = screen.getByRole<HTMLButtonElement>('button');
21
+ expect(button.innerHTML).toBe('State: 0');
22
+ act(() => {
23
+ button.click();
24
+ });
25
+
26
+ expect(button.innerHTML).toBe('State: 1');
27
+
28
+ setTimeout(function () {
29
+ // Expect to happen
30
+ expect(callback).lastCalledWith(2);
31
+
32
+ // Notify jest to complete
33
+ done();
34
+ }, 100);
35
+ });
package/lib/index.d.ts CHANGED
@@ -16,6 +16,7 @@ export * from './states/IState';
16
16
  export * from './states/PageState';
17
17
  export * from './states/State';
18
18
  export * from './states/UserState';
19
+ export * from './uses/useAsyncState';
19
20
  export * from './uses/useCombinedRefs';
20
21
  export * from './uses/useDelayedExecutor';
21
22
  export * from './uses/useDimensions';
package/lib/index.js CHANGED
@@ -21,6 +21,7 @@ export * from './states/PageState';
21
21
  export * from './states/State';
22
22
  export * from './states/UserState';
23
23
  // uses
24
+ export * from './uses/useAsyncState';
24
25
  export * from './uses/useCombinedRefs';
25
26
  export * from './uses/useDelayedExecutor';
26
27
  export * from './uses/useDimensions';
@@ -0,0 +1,7 @@
1
+ import React from 'react';
2
+ /**
3
+ * Returns a stateful value, and a async function to update it.
4
+ * @param initialState initial stat
5
+ * @returns Current state and update action
6
+ */
7
+ export declare function useAsyncState<S>(initialState: S | (() => S)): [S, (newState: React.SetStateAction<S>) => Promise<S>];
@@ -0,0 +1,33 @@
1
+ import React from 'react';
2
+ /**
3
+ * Returns a stateful value, and a async function to update it.
4
+ * @param initialState initial stat
5
+ * @returns Current state and update action
6
+ */
7
+ export function useAsyncState(initialState) {
8
+ // State
9
+ const [state, setState] = React.useState(initialState);
10
+ // Resolve sate
11
+ const resolveState = React.useRef();
12
+ // Is mounted or not
13
+ const isMounted = React.useRef(false);
14
+ React.useEffect(() => {
15
+ isMounted.current = true;
16
+ return () => {
17
+ isMounted.current = false;
18
+ };
19
+ }, []);
20
+ // When state update
21
+ React.useEffect(() => {
22
+ if (resolveState.current) {
23
+ resolveState.current(state);
24
+ }
25
+ }, [state]);
26
+ const setAsyncState = React.useCallback((newState) => new Promise((resolve) => {
27
+ if (isMounted.current) {
28
+ resolveState.current = resolve;
29
+ setState(newState);
30
+ }
31
+ }), []);
32
+ return [state, setAsyncState];
33
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@etsoo/react",
3
- "version": "1.5.96",
3
+ "version": "1.5.98",
4
4
  "description": "TypeScript ReactJs UI Independent Framework",
5
5
  "main": "lib/index.js",
6
6
  "types": "lib/index.d.ts",
@@ -70,8 +70,8 @@
70
70
  "@testing-library/jest-dom": "^5.16.5",
71
71
  "@testing-library/react": "^13.4.0",
72
72
  "@types/jest": "^29.0.3",
73
- "@typescript-eslint/eslint-plugin": "^5.37.0",
74
- "@typescript-eslint/parser": "^5.37.0",
73
+ "@typescript-eslint/eslint-plugin": "^5.38.0",
74
+ "@typescript-eslint/parser": "^5.38.0",
75
75
  "eslint": "^8.23.1",
76
76
  "eslint-config-airbnb-base": "^15.0.0",
77
77
  "eslint-plugin-import": "^2.26.0",
package/src/index.ts CHANGED
@@ -25,6 +25,7 @@ export * from './states/State';
25
25
  export * from './states/UserState';
26
26
 
27
27
  // uses
28
+ export * from './uses/useAsyncState';
28
29
  export * from './uses/useCombinedRefs';
29
30
  export * from './uses/useDelayedExecutor';
30
31
  export * from './uses/useDimensions';
@@ -0,0 +1,46 @@
1
+ import React from 'react';
2
+
3
+ /**
4
+ * Returns a stateful value, and a async function to update it.
5
+ * @param initialState initial stat
6
+ * @returns Current state and update action
7
+ */
8
+ export function useAsyncState<S>(
9
+ initialState: S | (() => S)
10
+ ): [S, (newState: React.SetStateAction<S>) => Promise<S>] {
11
+ // State
12
+ const [state, setState] = React.useState(initialState);
13
+
14
+ // Resolve sate
15
+ const resolveState = React.useRef<(value: S | PromiseLike<S>) => void>();
16
+
17
+ // Is mounted or not
18
+ const isMounted = React.useRef(false);
19
+ React.useEffect(() => {
20
+ isMounted.current = true;
21
+
22
+ return () => {
23
+ isMounted.current = false;
24
+ };
25
+ }, []);
26
+
27
+ // When state update
28
+ React.useEffect(() => {
29
+ if (resolveState.current) {
30
+ resolveState.current(state);
31
+ }
32
+ }, [state]);
33
+
34
+ const setAsyncState = React.useCallback(
35
+ (newState: React.SetStateAction<S>) =>
36
+ new Promise<S>((resolve) => {
37
+ if (isMounted.current) {
38
+ resolveState.current = resolve;
39
+ setState(newState);
40
+ }
41
+ }),
42
+ []
43
+ );
44
+
45
+ return [state, setAsyncState];
46
+ }