Skip to content

Commit a685dfc

Browse files
arjunvegdadai-shi
andauthored
feat: add back dev4_restore_atoms to store v2 (#2509)
* feat: add back dev4_restore_atoms to store v2 * refactor: remove internal types for store1 * fix: add primitive atom check when restoring Co-authored-by: Daishi Kato <[email protected]> * refactor: adjust to new changes * test: add failing test for store.sub not being called afer restore * test: simplify failing test * fix: add atoms to pending during restore * Update src/vanilla/store2.ts --------- Co-authored-by: Daishi Kato <[email protected]>
1 parent c9b504f commit a685dfc

File tree

3 files changed

+54
-3
lines changed

3 files changed

+54
-3
lines changed

src/vanilla/store.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -153,9 +153,6 @@ type PrdStore = {
153153
}
154154
type Store = PrdStore & Partial<DevStoreRev2>
155155

156-
export type INTERNAL_DevStoreRev2 = DevStoreRev2
157-
export type INTERNAL_PrdStore = PrdStore
158-
159156
/**
160157
* Create a new store. Each store is an independent, isolated universe of atom
161158
* states.

src/vanilla/store2.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,7 @@ type DevStoreRev4 = {
247247
key: K,
248248
fn: PrdStore[K],
249249
) => void
250+
dev4_restore_atoms: (values: Iterable<readonly [AnyAtom, AnyValue]>) => void
250251
}
251252

252253
type PrdStore = {
@@ -679,6 +680,23 @@ export const createStore = (): Store => {
679680
dev4_override_method: (key, fn) => {
680681
;(store as any)[key] = fn
681682
},
683+
dev4_restore_atoms: (values) => {
684+
const pending = createPending()
685+
for (const [atom, value] of values) {
686+
if (hasInitialValue(atom)) {
687+
const aState = getAtomState(atom)
688+
const hasPrevValue = 'v' in aState
689+
const prevValue = aState.v
690+
setAtomStateValueOrPromise(atom, aState, value)
691+
mountDependencies(pending, atom, aState)
692+
if (!hasPrevValue || !Object.is(prevValue, aState.v)) {
693+
addPendingAtom(pending, atom, aState)
694+
recomputeDependents(pending, atom)
695+
}
696+
}
697+
}
698+
flushPending(pending)
699+
},
682700
}
683701
return store
684702
}

tests/vanilla/storedev.test.tsx

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,4 +136,40 @@ describe.skipIf(!IS_DEV_STORE2)('[DEV-ONLY] dev-only methods rev4', () => {
136136
const weakMap = store.dev4_get_internal_weak_map()
137137
expect(weakMap.get(countAtom)?.v).toEqual(1)
138138
})
139+
140+
it('should restore atoms and its dependencies correctly', () => {
141+
const store = createStore() as any
142+
if (!('dev4_restore_atoms' in store)) {
143+
throw new Error('dev methods are not available')
144+
}
145+
const countAtom = atom(0)
146+
const derivedAtom = atom((get) => get(countAtom) * 2)
147+
store.set(countAtom, 1)
148+
store.dev4_restore_atoms([[countAtom, 2]])
149+
expect(store.get(countAtom)).toBe(2)
150+
expect(store.get?.(derivedAtom)).toBe(4)
151+
})
152+
153+
it('should restore atoms and call store listeners correctly', () => {
154+
const store = createStore() as any
155+
if (!('dev4_restore_atoms' in store)) {
156+
throw new Error('dev methods are not available')
157+
}
158+
const countAtom = atom(0)
159+
const derivedAtom = atom((get) => get(countAtom) * 2)
160+
const countCb = vi.fn()
161+
const derivedCb = vi.fn()
162+
store.set(countAtom, 2)
163+
const unsubCount = store.sub(countAtom, countCb)
164+
const unsubDerived = store.sub(derivedAtom, derivedCb)
165+
store.dev4_restore_atoms([
166+
[countAtom, 1],
167+
[derivedAtom, 2],
168+
])
169+
170+
expect(countCb).toHaveBeenCalled()
171+
expect(derivedCb).toHaveBeenCalled()
172+
unsubCount()
173+
unsubDerived()
174+
})
139175
})

0 commit comments

Comments
 (0)