Skip to content

Commit 9bd633e

Browse files
committed
IT WORKS!!!
PASS: unscoped derived can change to dependent scoped and back
1 parent 0892c1c commit 9bd633e

File tree

3 files changed

+14
-25
lines changed

3 files changed

+14
-25
lines changed

src/ScopeProvider/scope.ts

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,8 @@ export function createScope(props: CreateScopeProps): ScopedStore {
249249
const mountedMap = buildingBlocks[1]
250250
const invalidatedAtoms = buildingBlocks[2]
251251
const changedAtoms = buildingBlocks[3]
252+
const ensureAtomState = buildingBlocks[11]
253+
const mountDependencies = buildingBlocks[17]
252254

253255
const proxyState: ProxyState = {
254256
get originalAtom() {
@@ -261,7 +263,6 @@ export function createScope(props: CreateScopeProps): ScopedStore {
261263
}
262264

263265
const proxyAtom = createAtom(customRead) as T
264-
const ensureAtomState = buildingBlocks[11]
265266
const proxyAtomState = ensureAtomState(store, proxyAtom)
266267

267268
function customRead(get: <V>(a: Atom<V>) => V) {
@@ -302,30 +303,20 @@ export function createScope(props: CreateScopeProps): ScopedStore {
302303
const [fromAtom, toAtom] = isScoped
303304
? [originalAtom, scopedAtom]
304305
: [scopedAtom, originalAtom]
305-
306-
if (isScoped !== proxyState.hasScoped || proxyAtomState.d.size === 0) {
307-
// update proxyAtomState dep to be toAtom
306+
const scopeChange = isScoped !== proxyState.hasScoped
307+
proxyState.hasScoped = isScoped
308+
if (scopeChange || !isAtomStateInitialized(proxyAtomState)) {
308309
const toAtomState = ensureAtomState(store, toAtom)
309310
proxyAtomState.d.delete(fromAtom)
310311
proxyAtomState.d.set(toAtom, toAtomState.n)
311-
const mounted = mountedMap.get(proxyAtom)
312-
if (mounted) {
313-
mounted.d.delete(fromAtom)
314-
mounted.d.add(toAtom)
315-
}
316-
const mountedScoped = mountedMap.get(fromAtom)
317-
if (mountedScoped) {
318-
mountedScoped.t.delete(proxyAtom)
319-
}
320-
const mountedOriginal = mountedMap.get(toAtom)
321-
if (mountedOriginal) {
322-
mountedOriginal.t.add(proxyAtom)
323-
}
312+
proxyAtomState.v = toAtomState.v
313+
// TODO: Do we need this?
314+
// proxyAtomState.n = toAtomState.n - 1
315+
mountDependencies(store, proxyAtom)
324316
if ('e' in atomState) {
325317
throw atomState.e
326318
}
327319
}
328-
proxyState.hasScoped = isScoped
329320
return isScoped
330321
}
331322

tests/ScopeProvider/01_basic_spec.test.tsx

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -492,14 +492,13 @@ describe('Counter', () => {
492492
b: v=0
493493
c: v=0
494494
b@S1: v=0
495-
d?@S1: v=00
495+
_d@S1: v=00
496496
d@S1: v=00
497497
b@S1: v=0
498498
c: v=0
499499
d@S1: v=00
500500
b@S1: v=0
501501
c: v=0
502-
--------------------
503502
`)
504503
console.log('set d in S0')
505504
s[0].set(d)
@@ -518,14 +517,13 @@ describe('Counter', () => {
518517
b: v=1
519518
c: v=1
520519
b@S1: v=0
521-
d?@S1: v=01
520+
_d@S1: v=01
522521
d@S1: v=01
523522
b@S1: v=0
524523
c: v=1
525524
d@S1: v=01
526525
b@S1: v=0
527526
c: v=1
528-
--------------------
529527
`)
530528
})
531529

tests/ScopeProvider/dependent.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ describe('open issues', () => {
110110
a: v=scoped_2
111111
b@S1: v=0
112112
`)
113-
expect(cReadCount).toHaveBeenCalledTimes(2) // called once for c0 and once for c1
113+
expect(cReadCount).toHaveBeenCalledTimes(2) // called for c0 and c1
114114
cReadCount.mockClear()
115115

116116
s0.set(c, 1) // c0 writes to b0
@@ -161,10 +161,10 @@ describe('open issues', () => {
161161
_c@S1: v=undefined
162162
c: v=undefined
163163
a: v=unscoped_3
164-
c@S1: v=2
164+
c@S1: v=undefined
165165
a: v=unscoped_3
166166
`)
167-
expect(cReadCount).toHaveBeenCalledTimes(1) // called for c1
167+
expect(cReadCount).toHaveBeenCalledTimes(2) // called for c0 and c1
168168
cReadCount.mockClear()
169169
})
170170
})

0 commit comments

Comments
 (0)