Skip to content

Commit

Permalink
fix(persist): snapshotAtom init state #885
Browse files Browse the repository at this point in the history
  • Loading branch information
artalar committed Jun 26, 2024
1 parent 94e1367 commit 209613f
Showing 1 changed file with 18 additions and 21 deletions.
39 changes: 18 additions & 21 deletions packages/persist/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,29 +194,26 @@ export const createMemStorage = ({
}): PersistStorage & { snapshotAtom: AtomMut<Rec<PersistRecord>> } => {
const timestamp = Date.now()
const to = timestamp + MAX_SAFE_TIMEOUT
// eslint-disable-next-line @reatom/atom-rule
const snapshotAtom = atom(
Object.entries(snapshot).reduce(
(acc, [key, data]) => (
(acc[key] = {
data,
fromState: false,
id: 0,
timestamp,
to,
version: 0,
}),
acc
),
{} as Rec<PersistRecord>,
const initState = Object.entries(snapshot).reduce(
(acc, [key, data]) => (
(acc[key] = {
data,
fromState: false,
id: 0,
timestamp,
to,
version: 0,
}),
acc
),
`${name}._snapshotAtom`,
)
// eslint-disable-next-line @reatom/atom-rule
const listenersAtom = atom(
(ctx, state = new Map<string, Set<Fn<[PersistRecord]>>>()) => state,
`${name}._listenersAtom`,
{} as Rec<PersistRecord>,
)
const snapshotAtom = atom(initState, `${name}._snapshotAtom`)
snapshotAtom.__reatom.initState = () => ({ ...initState })

const listenersInitState = new Map<string, Set<Fn<[PersistRecord]>>>()
const listenersAtom = atom(listenersInitState, `${name}._listenersAtom`)
listenersAtom.__reatom.initState = () => new Map(listenersInitState)

return {
name,
Expand Down

0 comments on commit 209613f

Please sign in to comment.