Skip to content

Commit c31b2fe

Browse files
committed
support effects that are also functions in Atom.make
1 parent f57a5c9 commit c31b2fe

File tree

3 files changed

+23
-7
lines changed

3 files changed

+23
-7
lines changed

.changeset/odd-falcons-smile.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+
support effects that are also functions in Atom.make

packages/atom/src/Atom.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import * as Layer from "effect/Layer"
2424
import * as MutableHashMap from "effect/MutableHashMap"
2525
import * as Option from "effect/Option"
2626
import { type Pipeable, pipeArguments } from "effect/Pipeable"
27-
import { hasProperty } from "effect/Predicate"
27+
import { hasProperty, isObject } from "effect/Predicate"
2828
import type { ReadonlyRecord } from "effect/Record"
2929
import * as Runtime from "effect/Runtime"
3030
import * as Schema from "effect/Schema"
@@ -421,31 +421,31 @@ const makeRead: {
421421
| A,
422422
options?: { readonly initialValue?: unknown }
423423
) => {
424-
if (typeof arg === "function") {
424+
if (typeof arg === "function" && !Effect.isEffect(arg) && !(Stream.StreamTypeId in arg)) {
425425
const create = arg as (get: Context) => any
426426
return function(get: Context, providedRuntime?: Runtime.Runtime<any>) {
427427
const value = create(get)
428-
if (typeof value === "object" && value !== null) {
428+
if (isObject(value)) {
429429
if (isDataType(value)) {
430430
return value
431431
} else if (Effect.EffectTypeId in value) {
432432
return effect(get, value as any, options, providedRuntime)
433433
} else if (Stream.StreamTypeId in value) {
434-
return stream(get, value, options, providedRuntime)
434+
return stream(get, value as any, options, providedRuntime)
435435
}
436436
}
437437
return value
438438
}
439-
} else if (typeof arg === "object" && arg !== null) {
439+
} else if (isObject(arg)) {
440440
if (isDataType(arg)) {
441441
return state(arg)
442442
} else if (Effect.EffectTypeId in arg) {
443443
return function(get: Context, providedRuntime?: Runtime.Runtime<any>) {
444-
return effect(get, arg, options, providedRuntime)
444+
return effect(get, arg as any, options, providedRuntime)
445445
}
446446
} else if (Stream.StreamTypeId in arg) {
447447
return function(get: Context, providedRuntime?: Runtime.Runtime<any>) {
448-
return stream(get, arg, options, providedRuntime)
448+
return stream(get, arg as any, options, providedRuntime)
449449
}
450450
}
451451
}

packages/atom/test/Atom.test.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,14 @@ describe("Atom", () => {
141141
expect(result.value).toEqual(true)
142142
})
143143

144+
it("runtime direct tag", async () => {
145+
const counter = counterRuntime.atom(Counter)
146+
const r = Registry.make()
147+
const result = r.get(counter)
148+
assert(Result.isSuccess(result))
149+
assert(Effect.isEffect(result.value.get))
150+
})
151+
144152
it("effect initial", async () => {
145153
const count = Atom.make(
146154
Effect.succeed(1).pipe(Effect.delay(100)),

0 commit comments

Comments
 (0)