Skip to content

Commit 07af354

Browse files
fix writable derived waiting flag after refresh (#378)
Co-authored-by: Tim Smart <[email protected]>
1 parent 5557486 commit 07af354

File tree

3 files changed

+46
-4
lines changed

3 files changed

+46
-4
lines changed

.changeset/wicked-pianos-speak.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
"@effect-atom/atom": patch
3+
"@effect-atom/atom-livestore": patch
4+
"@effect-atom/atom-react": patch
5+
"@effect-atom/atom-vue": patch
6+
---
7+
8+
establish atom relationship after rebuilds

packages/atom/src/internal/registry.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import * as Effect from "effect/Effect"
22
import * as Equal from "effect/Equal"
33
import * as Exit from "effect/Exit"
4-
import { pipe } from "effect/Function"
4+
import { constVoid, pipe } from "effect/Function"
55
import { globalValue } from "effect/GlobalValue"
66
import * as Option from "effect/Option"
77
import * as Queue from "effect/Queue"
@@ -11,7 +11,6 @@ import type * as Registry from "../Registry.js"
1111
import * as Result from "../Result.js"
1212

1313
const constImmediate = { immediate: true }
14-
function constListener(_: any) {}
1514

1615
/** @internal */
1716
export const TypeId: Registry.TypeId = "~effect-atom/atom/Registry"
@@ -116,7 +115,7 @@ class RegistryImpl implements Registry.Registry {
116115
}
117116

118117
mount<A>(atom: Atom.Atom<A>) {
119-
return this.subscribe(atom, constListener, constImmediate)
118+
return this.subscribe(atom, constVoid, constImmediate)
120119
}
121120

122121
atomHasTtl(atom: Atom.Atom<any>): boolean {
@@ -735,8 +734,9 @@ const makeLifetime = <A>(node: Node<A>): Lifetime<A> => {
735734
return node.registry.get(atom)
736735
}
737736
const parent = node.registry.ensureNode(atom)
737+
const value = parent.value()
738738
node.addParent(parent)
739-
return parent.value()
739+
return value
740740
}
741741
Object.setPrototypeOf(get, LifetimeProto)
742742
get.isFn = false

packages/atom/test/Atom.test.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1388,6 +1388,40 @@ describe("Atom", () => {
13881388
const result = r.get(atom)
13891389
expect(Result.isInterrupted(result)).toBeTruthy()
13901390
})
1391+
1392+
it("writable derived clears waiting after refresh", async () => {
1393+
let count = 0
1394+
const base = Atom.make(Effect.sync(() => ++count).pipe(Effect.delay(100))).pipe(
1395+
Atom.withLabel("base")
1396+
)
1397+
const derived = Atom.writable(
1398+
(get) => get(base),
1399+
() => {},
1400+
(refresh) => refresh(base)
1401+
).pipe(
1402+
Atom.withLabel("derived")
1403+
)
1404+
const r = Registry.make()
1405+
1406+
const unmount1 = r.mount(derived)
1407+
await vitest.advanceTimersByTimeAsync(100)
1408+
let result = r.get(derived)
1409+
assert(Result.isSuccess(result))
1410+
expect(result.value).toEqual(1)
1411+
expect(result.waiting).toEqual(false)
1412+
unmount1()
1413+
1414+
r.refresh(derived)
1415+
const unmount2 = r.mount(derived)
1416+
await vitest.advanceTimersByTimeAsync(100)
1417+
1418+
result = r.get(derived)
1419+
expect(result.waiting).toEqual(false)
1420+
assert(Result.isSuccess(result))
1421+
expect(result.value).toEqual(2)
1422+
1423+
unmount2()
1424+
})
13911425
})
13921426

13931427
interface BuildCounter {

0 commit comments

Comments
 (0)