Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

支持异步的 createReduxState 实现 #645

Closed
nuintun opened this issue Sep 13, 2021 · 0 comments
Closed

支持异步的 createReduxState 实现 #645

nuintun opened this issue Sep 13, 2021 · 0 comments

Comments

@nuintun
Copy link
Owner

nuintun commented Sep 13, 2021

createReduxState.ts

/**
 * @module createReduxState
 */

import React, { useCallback, useEffect, useRef, useState } from 'react';

import { isFunction } from '~js/utils/utils';

type Dispatch<A> = (action: A) => Promise<void>;

type Reducer<S, A> = (state: S, action: A) => S | PromiseLike<S>;

/**
 * @function createReduxState
 * @description 【Hook】生成类 Redux 状态,支持异步
 * @param reducer 状态生成器
 */
export default function createReduxState<S, A>(
  reducer: Reducer<S | undefined, A>
): () => [state: S | undefined, dispatch: Dispatch<A>];
/**
 * @function createReduxState
 * @description 【Hook】生成类 Redux 状态,支持异步
 * @param reducer 状态生成器
 * @param initialState 初始状态
 */
export default function createReduxState<S, A>(
  reducer: Reducer<S, A>,
  initialState: S | (() => S)
): () => [state: S, dispatch: Dispatch<A>];
export default function createReduxState<S, A>(
  reducer: Reducer<S | undefined, A>,
  initialState?: S | (() => S)
): () => [state: S | undefined, dispatch: Dispatch<A>] {
  let initialized = false;

  let sharedState: S | undefined;

  const dispatches = new Set<React.Dispatch<React.SetStateAction<S | undefined>>>();

  const dispatchAction = async (action: A, initializedRef: React.MutableRefObject<boolean>): Promise<void> => {
    if (initializedRef.current) {
      const nextState = await reducer(sharedState, action);

      if (initializedRef.current) {
        sharedState = nextState;

        for (const dispatch of dispatches) {
          dispatch(sharedState);
        }
      }
    }
  };

  const getInitialState = (): S | undefined => {
    if (initialized) return sharedState;

    initialized = true;

    sharedState = isFunction(initialState) ? initialState() : initialState;

    return sharedState;
  };

  return () => {
    const initializedRef = useRef(false);
    const [state, setState] = useState(getInitialState);

    if (!initializedRef.current) {
      dispatches.add(setState);

      initializedRef.current = true;
    }

    const dispatch = useCallback((action: A): Promise<void> => {
      return dispatchAction(action, initializedRef);
    }, []);

    useEffect(() => {
      return () => {
        dispatches.delete(setState);

        initializedRef.current = false;
      };
    }, []);

    return [state, dispatch];
  };
}
@nuintun nuintun closed this as completed Sep 13, 2021
@nuintun nuintun pinned this issue Sep 13, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant