Skip to content
Merged
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
8 changes: 8 additions & 0 deletions .changeset/wicked-pianos-speak.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
"@effect-atom/atom": patch
"@effect-atom/atom-livestore": patch
"@effect-atom/atom-react": patch
"@effect-atom/atom-vue": patch
---

establish atom relationship after rebuilds
8 changes: 4 additions & 4 deletions packages/atom/src/internal/registry.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as Effect from "effect/Effect"
import * as Equal from "effect/Equal"
import * as Exit from "effect/Exit"
import { pipe } from "effect/Function"
import { constVoid, pipe } from "effect/Function"
import { globalValue } from "effect/GlobalValue"
import * as Option from "effect/Option"
import * as Queue from "effect/Queue"
Expand All @@ -11,7 +11,6 @@ import type * as Registry from "../Registry.js"
import * as Result from "../Result.js"

const constImmediate = { immediate: true }
function constListener(_: any) {}

/** @internal */
export const TypeId: Registry.TypeId = "~effect-atom/atom/Registry"
Expand Down Expand Up @@ -116,7 +115,7 @@ class RegistryImpl implements Registry.Registry {
}

mount<A>(atom: Atom.Atom<A>) {
return this.subscribe(atom, constListener, constImmediate)
return this.subscribe(atom, constVoid, constImmediate)
}

atomHasTtl(atom: Atom.Atom<any>): boolean {
Expand Down Expand Up @@ -735,8 +734,9 @@ const makeLifetime = <A>(node: Node<A>): Lifetime<A> => {
return node.registry.get(atom)
}
const parent = node.registry.ensureNode(atom)
const value = parent.value()
node.addParent(parent)
return parent.value()
return value
}
Object.setPrototypeOf(get, LifetimeProto)
get.isFn = false
Expand Down
34 changes: 34 additions & 0 deletions packages/atom/test/Atom.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1388,6 +1388,40 @@ describe("Atom", () => {
const result = r.get(atom)
expect(Result.isInterrupted(result)).toBeTruthy()
})

it("writable derived clears waiting after refresh", async () => {
let count = 0
const base = Atom.make(Effect.sync(() => ++count).pipe(Effect.delay(100))).pipe(
Atom.withLabel("base")
)
const derived = Atom.writable(
(get) => get(base),
() => {},
(refresh) => refresh(base)
).pipe(
Atom.withLabel("derived")
)
const r = Registry.make()

const unmount1 = r.mount(derived)
await vitest.advanceTimersByTimeAsync(100)
let result = r.get(derived)
assert(Result.isSuccess(result))
expect(result.value).toEqual(1)
expect(result.waiting).toEqual(false)
unmount1()

r.refresh(derived)
const unmount2 = r.mount(derived)
await vitest.advanceTimersByTimeAsync(100)

result = r.get(derived)
expect(result.waiting).toEqual(false)
assert(Result.isSuccess(result))
expect(result.value).toEqual(2)

unmount2()
})
})

interface BuildCounter {
Expand Down