Skip to content

Commit 14b1b1d

Browse files
committed
add get.setResult, which can be used to call a Atom.fn inside an atom context
1 parent 73e0319 commit 14b1b1d

File tree

3 files changed

+18
-0
lines changed

3 files changed

+18
-0
lines changed

.changeset/cuddly-carrots-play.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+
add get.setResult, which can be used to call a Atom.fn inside an atom context

packages/atom/src/Atom.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ export interface Context {
140140
self<A>(this: Context): Option.Option<A>
141141
setSelf<A>(this: Context, a: A): void
142142
set<R, W>(this: Context, atom: Writable<R, W>, value: W): void
143+
setResult<A, E, W>(this: Context, atom: Writable<Result.Result<A, E>, W>, value: W): Effect.Effect<A, E>
143144
some<A>(this: Context, atom: Atom<Option.Option<A>>): Effect.Effect<A>
144145
someOnce<A>(this: Context, atom: Atom<Option.Option<A>>): Effect.Effect<A>
145146
stream<A>(this: Context, atom: Atom<A>, options?: {
@@ -983,6 +984,7 @@ export interface FnContext {
983984
self<A>(this: FnContext): Option.Option<A>
984985
setSelf<A>(this: FnContext, a: A): void
985986
set<R, W>(this: FnContext, atom: Writable<R, W>, value: W): void
987+
setResult<A, E, W>(this: Context, atom: Writable<Result.Result<A, E>, W>, value: W): Effect.Effect<A, E>
986988
some<A>(this: FnContext, atom: Atom<Option.Option<A>>): Effect.Effect<A>
987989
stream<A>(this: FnContext, atom: Atom<A>, options?: {
988990
readonly withoutInitialValue?: boolean

packages/atom/src/internal/registry.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -578,6 +578,14 @@ const LifetimeProto: Omit<Lifetime<any>, "node" | "finalizers" | "disposed" | "i
578578
})
579579
},
580580

581+
setResult<A, E, W>(this: Lifetime<any>, atom: Atom.Writable<Result.Result<A, E>, W>, value: W): Effect.Effect<A, E> {
582+
if (this.disposed) {
583+
throw disposedError(this.node.atom)
584+
}
585+
this.node.registry.set(atom, value)
586+
return this.resultOnce(atom, { suspendOnWaiting: true })
587+
},
588+
581589
some<A>(this: Lifetime<any>, atom: Atom.Atom<Option.Option<A>>): Effect.Effect<A> {
582590
if (this.disposed) {
583591
throw disposedError(this.node.atom)

0 commit comments

Comments
 (0)