Skip to content

Commit

Permalink
improve reshow-flux-base type
Browse files Browse the repository at this point in the history
  • Loading branch information
HillLiu committed Mar 17, 2024
1 parent abbdd6c commit 1e2ab66
Show file tree
Hide file tree
Showing 15 changed files with 187 additions and 137 deletions.
2 changes: 1 addition & 1 deletion packages/reshow-flux-base/package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "0.17.48",
"version": "0.18.2",
"name": "reshow-flux-base",
"repository": {
"type": "git",
Expand Down
26 changes: 25 additions & 1 deletion packages/reshow-flux-base/src/__tests__/createReducerTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@
*/

import { expect } from "chai";
import sinon from "sinon";
import * as sinon from "sinon";
import createReducer from "../createReducer";
import { ActionObject } from "../type";

describe("Test createReducer", () => {
let reducer;
Expand Down Expand Up @@ -50,6 +51,29 @@ describe("Test createReducer", () => {
});
});

describe("Test dispatch more type", () => {
it("dispatch with number also could include string", () => {
const [store, dispatch] = createReducer(
(_state, /**@type number*/ action) => ({ foo: action }),
{ foo: 1 }
);
expect(store.getState()).to.deep.equal({ foo: 1 });
dispatch("");
expect(store.getState()).to.deep.equal({ foo: { type: "" } });
});
it("dispatch with string", () => {
const [store, dispatch] = createReducer(
(_state, /**@type ActionObject*/ action) => {
return { foo: action.type };
},
{ foo: "bar" }
);
expect(store.getState()).to.deep.equal({ foo: "bar" });
dispatch("");
expect(store.getState()).to.deep.equal({ foo: "" });
});
});

it("Emit with custom event", (done) => {
const [store, dispatch] = reducer;
const callback = sinon.spy();
Expand Down
29 changes: 17 additions & 12 deletions packages/reshow-flux-base/src/createReducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import callfunc from "call-func";
import { UNDEFINED, T_UNDEFINED, STRING, NEW_OBJ } from "reshow-constant";
import { StoreObject, Emiter } from "./type";
import { StoreObject, Emiter, ActionObject } from "./type";

/**
* @template StateType
Expand All @@ -12,7 +12,7 @@ import { StoreObject, Emiter } from "./type";

/**
* @template ActionType
* @typedef {import('./type').RefineAction<ActionType>} RefineAction
* @typedef {import('./type').RefinedAction<ActionType>} RefinedAction
*/

/**
Expand All @@ -22,6 +22,7 @@ import { StoreObject, Emiter } from "./type";
/**
* @template StateType
* @template ActionType
*
* @callback DispatchCallback
* @param {StateType} State
* @returns {ActionType}
Expand All @@ -30,7 +31,7 @@ import { StoreObject, Emiter } from "./type";
/**
* @template StateType
* @template ActionType
* @typedef {ActionType|DispatchCallback<StateType, ActionType>} DispatchAction
* @typedef {string|ActionType|DispatchCallback<StateType, ActionType>} DispatchAction
*/

/**
Expand Down Expand Up @@ -78,10 +79,11 @@ const getMitt = () => {
*
* @template StateType
* @template ActionType
*
* @param {DispatchAction<StateType, ActionType>} action
* @param {Payload} [params]
* @param {StateType} [prevState]
* @returns {RefineAction<ActionType>} lazy actions
* @returns {ActionObject|ActionType} lazy actions
*/
export const refineAction = (action, params, prevState) => {
let nextAction = NEW_OBJ();
Expand All @@ -103,9 +105,10 @@ export const refineAction = (action, params, prevState) => {
/**
* @template StateType
* @template ActionType
*
* @callback ReducerType
* @param {StateType} StateArg
* @param {RefineAction<ActionType>} ActionArg
* @param {StateType} ReducerState
* @param {ActionType} ReducerAction
* @returns {StateType}
*/

Expand All @@ -117,7 +120,8 @@ export const refineAction = (action, params, prevState) => {
/**
* @template StateType
* @template ActionType
* @callback DispatchType
*
* @callback DispatchFunction
* @param {DispatchAction<StateType, ActionType>} action
* @param {Payload} [actionParams]
* @returns {StateType} endingState
Expand All @@ -126,27 +130,28 @@ export const refineAction = (action, params, prevState) => {
/**
* @template StateType
* @template ActionType
*
* @param {ReducerType<StateType, ActionType>} reducer
* @param {InitStateType<StateType>} [initState]
* @returns {[StoreObject<StateType, ActionType>, DispatchType<StateType, ActionType>]}
* @returns {[StoreObject<StateType, ActionType>, DispatchFunction<StateType, ActionType>]}
*/
const createReducer = (reducer, initState) => {
const state = { current: callfunc(initState) };
const mitt = getMitt();
/**
* @type {DispatchType<StateType, ActionType>}
* @type {DispatchFunction<StateType, ActionType>}
*/
const dispatch = (action, actionParams) => {
const startingState = state.current;
const thisAction = refineAction(action, actionParams, startingState);
const endingState = reducer(startingState, thisAction);
const refinedAction = refineAction(action, actionParams, startingState);
const endingState = reducer(startingState, /**@type any*/ (refinedAction));
if (endingState === T_UNDEFINED) {
console.trace();
throw `reducer() return ${UNDEFINED}.`;
}
if (startingState !== endingState) {
state.current = endingState;
mitt.emit(endingState, thisAction, startingState);
mitt.emit(endingState, refinedAction, startingState);
}
return endingState;
};
Expand Down
2 changes: 1 addition & 1 deletion packages/reshow-flux-base/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
/**
* @template StateType
* @template ActionType
* @typedef {import("./createReducer").DispatchType<StateType, ActionType>} DispatchType
* @typedef {import("./createReducer").DispatchFunction<StateType, ActionType>} DispatchFunction
*/

export { default as createReducer, refineAction } from "./createReducer";
Expand Down
30 changes: 16 additions & 14 deletions packages/reshow-flux-base/src/type.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,27 +12,38 @@
* @interface
*/
export class ActionObject {
/** @type {string|SAFE_UNDEFINED=} */
/** @type {string} */
type;
/** @type {Payload=} */
/** @type {?Payload=} */
params;
}

/**
* @template [ActionType=ActionObject]
* @typedef {ActionType} RefineAction
* @template [ActionType = ActionObject]
* @typedef {ActionType|ActionObject} RefinedAction
*/

/**
* @template StateType
* @template ActionType
*
* @callback FluxHandler
* @param {StateType} NextState
* @param {RefineAction<ActionType>} Action
* @param {RefinedAction<ActionType>} Action
* @param {StateType} PrevState
* @returns{any}
*/

/**
* @template StateType
* @template ActionType
*
* @callback EmitterEmitCall
* @param {StateType} state
* @param {RefinedAction<ActionType>} action
* @param {StateType} prevState
*/

/**
* @template StateType
* @template ActionType
Expand All @@ -56,15 +67,6 @@ export class ActionObject {
* @returns {FluxHandler<StateType, ActionType>[]}
*/

/**
* @template StateType
* @template ActionType
* @callback EmitterEmitCall
* @param {StateType} state
* @param {RefineAction<ActionType>} action
* @param {StateType} prevState
*/

/**
* @template StateType
* @template ActionType
Expand Down
24 changes: 14 additions & 10 deletions packages/reshow-flux-base/types/createReducer.d.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
export function refineAction<StateType, ActionType>(action: DispatchAction<StateType, ActionType>, params?: Payload, prevState?: StateType): ActionType;
export function refineAction<StateType, ActionType>(action: DispatchAction<StateType, ActionType>, params?: Payload, prevState?: StateType): ActionObject | ActionType;
export default createReducer;
export type FluxHandler<StateType, ActionType> = import('./type').FluxHandler<StateType, ActionType>;
export type RefineAction<ActionType> = import('./type').RefineAction<ActionType>;
export type RefinedAction<ActionType> = import('./type').RefinedAction<ActionType>;
export type Payload = import('./type').Payload;
export type DispatchCallback<StateType, ActionType> = (State: StateType) => ActionType;
export type DispatchAction<StateType, ActionType> = ActionType | DispatchCallback<StateType, ActionType>;
export type ReducerType<StateType, ActionType> = (StateArg: StateType, ActionArg: RefineAction<ActionType>) => StateType;
export type DispatchAction<StateType, ActionType> = string | ActionType | DispatchCallback<StateType, ActionType>;
export type ReducerType<StateType, ActionType> = (ReducerState: StateType, ReducerAction: ActionType) => StateType;
export type InitStateType<StateType> = StateType | (() => StateType);
export type DispatchType<StateType, ActionType> = (action: DispatchAction<StateType, ActionType>, actionParams?: Payload) => StateType;
export type DispatchFunction<StateType, ActionType> = (action: DispatchAction<StateType, ActionType>, actionParams?: Payload) => StateType;
import { ActionObject } from "./type";
/**
* @template StateType
* @template ActionType
*
* @callback ReducerType
* @param {StateType} StateArg
* @param {RefineAction<ActionType>} ActionArg
* @param {StateType} ReducerState
* @param {ActionType} ReducerAction
* @returns {StateType}
*/
/**
Expand All @@ -23,17 +25,19 @@ export type DispatchType<StateType, ActionType> = (action: DispatchAction<StateT
/**
* @template StateType
* @template ActionType
* @callback DispatchType
*
* @callback DispatchFunction
* @param {DispatchAction<StateType, ActionType>} action
* @param {Payload} [actionParams]
* @returns {StateType} endingState
*/
/**
* @template StateType
* @template ActionType
*
* @param {ReducerType<StateType, ActionType>} reducer
* @param {InitStateType<StateType>} [initState]
* @returns {[StoreObject<StateType, ActionType>, DispatchType<StateType, ActionType>]}
* @returns {[StoreObject<StateType, ActionType>, DispatchFunction<StateType, ActionType>]}
*/
declare function createReducer<StateType, ActionType>(reducer: ReducerType<StateType, ActionType>, initState?: InitStateType<StateType>): [StoreObject<StateType, ActionType>, DispatchType<StateType, ActionType>];
declare function createReducer<StateType, ActionType>(reducer: ReducerType<StateType, ActionType>, initState?: InitStateType<StateType>): [StoreObject<StateType, ActionType>, DispatchFunction<StateType, ActionType>];
import { StoreObject } from "./type";
2 changes: 1 addition & 1 deletion packages/reshow-flux-base/types/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export { default as SimpleMap } from "./SimpleMap";
export type InitStateType<StateType> = import("./createReducer").InitStateType<StateType>;
export type ReducerType<StateType, ActionType> = import("./createReducer").ReducerType<StateType, ActionType>;
export type DispatchType<StateType, ActionType> = import("./createReducer").DispatchType<StateType, ActionType>;
export type DispatchFunction<StateType, ActionType> = import("./createReducer").DispatchFunction<StateType, ActionType>;
export { default as createReducer, refineAction } from "./createReducer";
export { StoreObject, ActionObject } from "./type";
38 changes: 20 additions & 18 deletions packages/reshow-flux-base/types/type.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,34 @@
* @interface
*/
export class ActionObject {
/** @type {string|SAFE_UNDEFINED=} */
type: (string | SAFE_UNDEFINED) | undefined;
/** @type {Payload=} */
params: Payload | undefined;
/** @type {string} */
type: string;
/** @type {?Payload=} */
params: (Payload | null) | undefined;
}
/**
* @template [ActionType=ActionObject]
* @typedef {ActionType} RefineAction
* @template [ActionType = ActionObject]
* @typedef {ActionType|ActionObject} RefinedAction
*/
/**
* @template StateType
* @template ActionType
*
* @callback FluxHandler
* @param {StateType} NextState
* @param {RefineAction<ActionType>} Action
* @param {RefinedAction<ActionType>} Action
* @param {StateType} PrevState
* @returns{any}
*/
/**
* @template StateType
* @template ActionType
*
* @callback EmitterEmitCall
* @param {StateType} state
* @param {RefinedAction<ActionType>} action
* @param {StateType} prevState
*/
/**
* @template StateType
* @template ActionType
Expand All @@ -46,14 +56,6 @@ export class ActionObject {
* @param {FluxHandler<StateType, ActionType>} handler
* @returns {FluxHandler<StateType, ActionType>[]}
*/
/**
* @template StateType
* @template ActionType
* @callback EmitterEmitCall
* @param {StateType} state
* @param {RefineAction<ActionType>} action
* @param {StateType} prevState
*/
/**
* @template StateType
* @template ActionType
Expand Down Expand Up @@ -88,9 +90,9 @@ export type Payload = {
[x: string]: any;
};
export type SAFE_UNDEFINED = import("reshow-constant").SAFE_UNDEFINED;
export type RefineAction<ActionType = ActionObject> = ActionType;
export type FluxHandler<StateType, ActionType> = (NextState: StateType, Action: RefineAction<ActionType>, PrevState: StateType) => any;
export type RefinedAction<ActionType = ActionObject> = ActionType | ActionObject;
export type FluxHandler<StateType, ActionType> = (NextState: StateType, Action: RefinedAction<ActionType>, PrevState: StateType) => any;
export type EmitterEmitCall<StateType, ActionType> = (state: StateType, action: RefinedAction<ActionType>, prevState: StateType) => any;
export type EmitterResetCall<StateType, ActionType> = () => FluxHandler<StateType, ActionType>[];
export type EmitterAddCall<StateType, ActionType> = (handler: FluxHandler<StateType, ActionType>) => number;
export type EmitterRemoveCall<StateType, ActionType> = (handler: FluxHandler<StateType, ActionType>) => FluxHandler<StateType, ActionType>[];
export type EmitterEmitCall<StateType, ActionType> = (state: StateType, action: RefineAction<ActionType>, prevState: StateType) => any;
2 changes: 1 addition & 1 deletion packages/reshow-flux/package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "0.18.0",
"version": "0.18.2",
"name": "reshow-flux",
"repository": {
"type": "git",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const { PureComponent } = React;

import { expect } from "chai";
import { render, act } from "reshow-unit";
import sinon from "sinon";
import * as sinon from "sinon";
import { createReducer } from "reshow-flux-base";

import useConnect from "../useConnect";
Expand Down
2 changes: 1 addition & 1 deletion packages/reshow-flux/src/__tests__/useStoreTest.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
// @ts-check

import * as React from "react";
import * as sinon from "sinon";
import { expect } from "chai";
import { act, render, waitFor } from "reshow-unit";
import { createReducer } from "reshow-flux-base";
import sinon from "sinon";

import useStore from "../useStore";
import ImmutableStore from "../ImmutableStore";
Expand Down
2 changes: 1 addition & 1 deletion packages/reshow-flux/src/useImmutable.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import { ActionObject } from "reshow-flux-base";
*
* @template [StateType=StateMap]
* @param {InitStateType<StateType>} [initialState]
* @returns {[StateMap, import("reshow-flux-base").DispatchType<StateMap, ActionObject>]}
* @returns {[StateMap, import("reshow-flux-base").DispatchFunction<StateMap, ActionObject>]}
*/
const useImmutable = (initialState) => {
const lastReduce = useRef(/** @type any */ (null));
Expand Down
2 changes: 1 addition & 1 deletion packages/reshow-flux/types/ImmutableStore.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export type forEachCb = (Value: any, Key: any) => any;
*
* @returns {[ImmutableStoreObject<StateType, ActionType>, dispatch]}
*/
declare function ImmutableStore<StateType = StateMap, ActionType = MaybeMapType>(reducer?: ReducerTypeWithMap<StateType, ActionType>, initState?: import("reshow-flux-base").InitStateType<StateType>): [ImmutableStoreObject<StateType, ActionType>, import("reshow-flux-base/types/createReducer").DispatchType<StateType, ActionType>];
declare function ImmutableStore<StateType = StateMap, ActionType = MaybeMapType>(reducer?: ReducerTypeWithMap<StateType, ActionType>, initState?: import("reshow-flux-base").InitStateType<StateType>): [ImmutableStoreObject<StateType, ActionType>, import("reshow-flux-base/types/createReducer").DispatchFunction<StateType, ActionType>];
import { is as equal } from "immutable";
/**
* @param {MaybeMapType} maybeMap
Expand Down
Loading

0 comments on commit 1e2ab66

Please sign in to comment.