Skip to content

Commit

Permalink
fix(zustand): fix update state issue
Browse files Browse the repository at this point in the history
  • Loading branch information
unadlib committed Dec 20, 2024
1 parent f272a3b commit fed8ec0
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion packages/coaction-zustand/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,27 @@ export const bindZustand = ((initializer: StateCreator<any, [], []>) =>
const internalBindZustand = createBinder<BindZustand>({
handleStore: (store, rawState, state, internal) => {
if (zustandStore.getState() === internal.rootState) return;
let isCoactionUpdated = false;
internal.rootState = zustandStore.getState() as object;
coactionStore = store;
zustandStore.subscribe(() => {
if (!isCoactionUpdated) {
internal.rootState = zustandStore.getState() as object;
if (coactionStore.share === 'client') {
throw new Error('client zustand store cannot be updated');
} else if (coactionStore.share === 'main') {
// TODO: emit to all clients
}
}
internal.listeners.forEach((listener) => listener());
});
internal.updateImmutable = (state: any) => {
zustandStore.setState(state, true);
isCoactionUpdated = true;
try {
zustandStore.setState(state, true);
} finally {
isCoactionUpdated = false;
}
};
},
handleState: (externalState) => {
Expand Down

0 comments on commit fed8ec0

Please sign in to comment.