Skip to content

Commit

Permalink
fix(coaction): fix @coaction/zustand sync issue
Browse files Browse the repository at this point in the history
  • Loading branch information
unadlib committed Dec 21, 2024
1 parent fed8ec0 commit 59cdbd2
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 2 deletions.
1 change: 1 addition & 0 deletions examples/zustand-base/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"preview": "vite preview"
},
"dependencies": {
"localforage": "^1.10.0",
"react": "^18.3.1",
"react-dom": "^18.3.1"
},
Expand Down
12 changes: 11 additions & 1 deletion examples/zustand-base/src/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,21 @@ import { create } from '@coaction/react';
import { logger } from '@coaction/logger';
import { create as createWithZustand } from 'zustand';
import { bindZustand, adapt } from '@coaction/zustand';
import { persist, createJSONStorage } from 'zustand/middleware';
import localforage from 'localforage';

import { counter, type Counter } from './counter';

export const useStore = create<Counter>(
() => adapt(createWithZustand(bindZustand(counter))),
() =>
adapt(
createWithZustand(
persist(bindZustand(counter), {
name: 'counter',
storage: createJSONStorage(() => localforage)
})
)
),
{
middlewares: [
logger({
Expand Down
19 changes: 19 additions & 0 deletions examples/zustand-base/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1075,6 +1075,11 @@ ignore@^5.2.0, ignore@^5.3.1:
resolved "https://registry.yarnpkg.com/ignore/-/ignore-5.3.2.tgz#3cd40e729f3643fd87cb04e50bf0eb722bc596f5"
integrity sha512-hsBTNUqQTDwkWtcdYI2i06Y/nUBEsNEDJKjWdigLvegy8kDuJAS8uRlpkkcQpyEXL0Z/pjDy5HBmMjRCJ2gq+g==

immediate@~3.0.5:
version "3.0.6"
resolved "https://registry.yarnpkg.com/immediate/-/immediate-3.0.6.tgz#9db1dbd0faf8de6fbe0f5dd5e56bb606280de69b"
integrity sha512-XXOFtyqDjNDAQxVfYxuF7g9Il/IbWmmlQg2MYKOH8ExIT1qg6xc4zyS3HaEEATgs1btfzxq15ciUiY7gjSXRGQ==

import-fresh@^3.2.1:
version "3.3.0"
resolved "https://registry.yarnpkg.com/import-fresh/-/import-fresh-3.3.0.tgz#37162c25fcb9ebaa2e6e53d5b4d88ce17d9e0c2b"
Expand Down Expand Up @@ -1162,6 +1167,20 @@ levn@^0.4.1:
prelude-ls "^1.2.1"
type-check "~0.4.0"

lie@3.1.1:
version "3.1.1"
resolved "https://registry.yarnpkg.com/lie/-/lie-3.1.1.tgz#9a436b2cc7746ca59de7a41fa469b3efb76bd87e"
integrity sha512-RiNhHysUjhrDQntfYSfY4MU24coXXdEOgw9WGcKHNeEwffDYbF//u87M1EWaMGzuFoSbqW0C9C6lEEhDOAswfw==
dependencies:
immediate "~3.0.5"

localforage@^1.10.0:
version "1.10.0"
resolved "https://registry.yarnpkg.com/localforage/-/localforage-1.10.0.tgz#5c465dc5f62b2807c3a84c0c6a1b1b3212781dd4"
integrity sha512-14/H1aX7hzBBmmh7sGPd+AOMkkIrHM3Z1PAyGgZigA1H1p5O5ANnMyWzvpAETtG68/dC4pC0ncy3+PPGzXZHPg==
dependencies:
lie "3.1.1"

locate-path@^6.0.0:
version "6.0.0"
resolved "https://registry.yarnpkg.com/locate-path/-/locate-path-6.0.0.tgz#55321eb309febbc59c4801d931a72452a681d286"
Expand Down
3 changes: 2 additions & 1 deletion packages/coaction-zustand/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ export const bindZustand = ((initializer: StateCreator<any, [], []>) =>
if (coactionStore.share === 'client') {
throw new Error('client zustand store cannot be updated');
} else if (coactionStore.share === 'main') {
// TODO: emit to all clients
// emit to all clients
coactionStore.setState(zustandStore.getState()!);
}
}
internal.listeners.forEach((listener) => listener());
Expand Down

0 comments on commit 59cdbd2

Please sign in to comment.