Skip to content

Commit

Permalink
fix reduxDevTools action error
Browse files Browse the repository at this point in the history
  • Loading branch information
MrWangJustToDo committed Nov 29, 2023
1 parent 9c57a12 commit 2b76695
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 20 deletions.
2 changes: 1 addition & 1 deletion packages/r-store/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ export declare type UseSelectorWithStore<T> = {
/**
* @public
*/
export declare const version = "0.2.8";
export declare const version = "0.2.9";

/**
* @public
Expand Down
2 changes: 1 addition & 1 deletion packages/r-store/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "reactivity-store",
"version": "0.2.8",
"version": "0.2.9",
"author": "MrWangJustToDo",
"license": "MIT",
"description": "a reactive store, make you write reactive logic in react app just like zustand",
Expand Down
25 changes: 7 additions & 18 deletions packages/r-store/src/shared/dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,7 @@ const devToolMap: Record<string, any> = {};

const globalName = "__reactivity-store-redux-devtools__";

const defaultAction = { type: "unknown", getUpdatedState: () => null };

const pendingAction = new Set();

let globalAction: { type: string; $payload?: any; getUpdatedState: () => any } = defaultAction;
type Action = { type: string; $payload?: any; getUpdatedState: () => any };

let globalDevTools = null;

Expand Down Expand Up @@ -109,24 +105,18 @@ export const connectDevTool = (name: string, actions: Record<string, Function>,

devTools.init(obj);

const action = { type: name };

return Object.keys(actions).reduce((p, c) => {
p[c] = (...args) => {
const len = actions[c].length || 0;

globalAction = { ...action, $payload: args.slice(0, len), getUpdatedState: () => ({ ...devToolMap, [name]: JSON.parse(JSON.stringify(state)) }) };

const re = actions[c](...args);

if (isPromise(re)) {
re.finally(() => {
sendToDevTools(true);
globalAction = defaultAction;
sendToDevTools({type: `asyncAction/change-${name}`, $payload: args.slice(0, len), getUpdatedState: () => ({ ...devToolMap, [name]: JSON.parse(JSON.stringify(state)) })});
});
} else {
sendToDevTools(false);
globalAction = defaultAction;
sendToDevTools({type: `syncAction/change-${name}`, $payload: args.slice(0, len), getUpdatedState: () => ({ ...devToolMap, [name]: JSON.parse(JSON.stringify(state)) })});
}
return re;
};
Expand All @@ -140,13 +130,12 @@ export const connectDevTool = (name: string, actions: Record<string, Function>,
/**
* @internal
*/
export const sendToDevTools = (asyncAction: boolean) => {
const { getUpdatedState, type, ...action } = globalAction;
export const sendToDevTools = (action: Action) => {
const { getUpdatedState, ...rest } = action;
try {
getDevToolInstance().send({ ...action, type: asyncAction ? `asyncAction/change-${type}` : `syncAction/change-${type}` }, getUpdatedState());
const state = getUpdatedState();
getDevToolInstance().send(rest, state);
} catch (e) {
console.log(e);
} finally {
pendingAction.delete(globalAction);
}
};

0 comments on commit 2b76695

Please sign in to comment.