Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Not For Merge Yet] BREAKING: unmount async dependencies #2940

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 3 additions & 9 deletions src/vanilla/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -342,11 +342,6 @@ const buildStore = (...storeArgs: StoreArgs): Store => {
// Compute a new state for this atom.
atomState.d.clear()
let isSync = true
const mountDependenciesIfAsync = () => {
if (atomState.m) {
runWithTransaction(() => mountDependencies(atom, atomState))
}
}
const getter: Getter = <V>(a: Atom<V>) => {
if (isSelfAtom(atom, a)) {
const aState = ensureAtomState(a)
Expand All @@ -366,8 +361,8 @@ const buildStore = (...storeArgs: StoreArgs): Store => {
return returnAtomValue(aState)
} finally {
addDependency(atom, atomState, a, aState)
if (!isSync) {
mountDependenciesIfAsync()
if (!isSync && atomState.m) {
runWithTransaction(() => mountDependencies(atom, atomState))
}
}
}
Expand Down Expand Up @@ -405,7 +400,6 @@ const buildStore = (...storeArgs: StoreArgs): Store => {
setAtomStateValueOrPromise(atom, atomState, valueOrPromise)
if (isPromiseLike(valueOrPromise)) {
valueOrPromise.onCancel?.(() => controller?.abort())
valueOrPromise.then(mountDependenciesIfAsync, mountDependenciesIfAsync)
}
return atomState
} catch (error) {
Expand Down Expand Up @@ -565,7 +559,7 @@ const buildStore = (...storeArgs: StoreArgs): Store => {
): Result => runWithTransaction(() => writeAtomState(atom, ...args))

const mountDependencies = (atom: AnyAtom, atomState: AtomState) => {
if (atomState.m && !isPendingPromise(atomState.v)) {
if (atomState.m) {
for (const a of atomState.d.keys()) {
if (!atomState.m.d.has(a)) {
const aMounted = mountAtom(a, ensureAtomState(a))
Expand Down
101 changes: 98 additions & 3 deletions tests/vanilla/dependency.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ it('correctly handles the same promise being returned twice from an atom getter
await expect(store.get(derivedAtom)).resolves.toBe('Asynchronous Data')
})

it('keeps atoms mounted between recalculations', async () => {
it('keeps sync atoms mounted between recalculations', async () => {
const metrics1 = {
mounted: 0,
unmounted: 0,
Expand Down Expand Up @@ -148,9 +148,11 @@ it('keeps atoms mounted between recalculations', async () => {
mounted: 1,
unmounted: 0,
})
await Promise.resolve()
// async atom has been re-mounted
expect(metrics2).toEqual({
mounted: 1,
unmounted: 0,
mounted: 2,
unmounted: 1,
})
})

Expand Down Expand Up @@ -425,3 +427,96 @@ it('batches sync writes', () => {
expect(fetch).toBeCalledWith(1)
expect(store.get(a)).toBe(1)
})

it('mounts and unmounts sync and async dependencies correctly', async () => {
const mounted = [0, 0, 0, 0, 0] as [number, number, number, number, number]
const a = atom(0)
a.debugLabel = 'a'
a.onMount = () => {
++mounted[0]
return () => {
--mounted[0]
}
}

const b = atom(0)
b.debugLabel = 'b'
b.onMount = () => {
++mounted[1]
return () => {
--mounted[1]
}
}

const c = atom(0)
c.debugLabel = 'c'
c.onMount = () => {
++mounted[2]
return () => {
--mounted[2]
}
}

const d = atom(0)
d.debugLabel = 'd'
d.onMount = () => {
++mounted[3]
return () => {
--mounted[3]
}
}

const e = atom(0)
e.debugLabel = 'e'
e.onMount = () => {
++mounted[4]
return () => {
--mounted[4]
}
}

let resolve: (() => Promise<void>) | undefined
const f = atom((get) => {
if (!get(a)) {
get(b)
} else {
get(c)
}
const promise = new Promise<void>((r) => {
resolve = () => {
r()
return promise
}
}).then(() => {
if (!get(a)) {
get(d)
} else {
get(e)
}
})
return promise
})
f.debugLabel = 'f'

const store = createStore()
// mount a, b synchronously
const unsub = store.sub(f, () => {})
expect(mounted).toEqual([1, 1, 0, 0, 0])

// mount d asynchronously
await resolve!()
expect(mounted).toEqual([1, 1, 0, 1, 0])

// unmount b, mount c synchronously
// d is also unmounted
store.set(a, 1)
expect(mounted).toEqual([1, 0, 1, 0, 0])

// mount e asynchronously
await resolve!()
expect(mounted).toEqual([1, 0, 1, 0, 1])

// unmount a, b, d, e synchronously
unsub()
expect(mounted).toEqual([0, 0, 0, 0, 0])
})
18 changes: 18 additions & 0 deletions tests/vanilla/memoryleaks.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,4 +132,22 @@ describe('memory leaks (with dependencies)', () => {
objAtom = undefined
expect(await detector.isLeaking()).toBe(false)
})

it('infinite async dependency', async () => {
const store = createStore()
let objAtom: Atom<object> | undefined = atom({})
const detector = new LeakDetector(store.get(objAtom))
const atom1 = atom(0)
const atom2 = atom(async (get) => {
if (get(atom1)) {
return new Promise(() => {})
}
return objAtom && get(objAtom)
})
store.sub(atom2, () => {})
store.set(atom1, 1)
objAtom = undefined
await Promise.resolve()
expect(await detector.isLeaking()).toBe(false)
})
})
Loading