Skip to content

Commit

Permalink
don't treat Option and Either as effects
Browse files Browse the repository at this point in the history
  • Loading branch information
tim-smart committed Jan 25, 2024
1 parent 290b566 commit c7612e2
Show file tree
Hide file tree
Showing 8 changed files with 130 additions and 60 deletions.
5 changes: 5 additions & 0 deletions .changeset/witty-mangos-shake.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@effect-rx/rx": patch
---

don't treat Option and Either as effects
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
"lint-fix": "pnpm lint --fix"
},
"devDependencies": {
"@babel/cli": "^7.23.4",
"@babel/core": "^7.23.7",
"@babel/cli": "^7.23.9",
"@babel/core": "^7.23.9",
"@babel/plugin-transform-export-namespace-from": "^7.23.4",
"@babel/plugin-transform-modules-commonjs": "^7.23.3",
"@changesets/changelog-github": "^0.5.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/rx-react/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"sideEffects": false,
"devDependencies": {
"@types/react": "^18.2.48",
"effect": "^2.2.0",
"effect": "^2.2.2",
"react": "^18.2.0"
},
"peerDependencies": {
Expand Down
2 changes: 1 addition & 1 deletion packages/rx-vue/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"license": "MIT",
"sideEffects": false,
"devDependencies": {
"effect": "^2.2.0",
"effect": "^2.2.2",
"vue": "^3.4.15"
},
"peerDependencies": {
Expand Down
2 changes: 1 addition & 1 deletion packages/rx/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"license": "MIT",
"sideEffects": false,
"devDependencies": {
"effect": "^2.2.0"
"effect": "^2.2.2"
},
"peerDependencies": {
"effect": "^2.2.0"
Expand Down
15 changes: 12 additions & 3 deletions packages/rx/src/Rx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { NoSuchElementException } from "effect/Cause"
import * as Chunk from "effect/Chunk"
import * as Duration from "effect/Duration"
import * as Effect from "effect/Effect"
import * as Either from "effect/Either"
import * as Exit from "effect/Exit"
import { dual, pipe } from "effect/Function"
import { globalValue } from "effect/GlobalValue"
Expand Down Expand Up @@ -365,6 +366,10 @@ export const make: {
// constructors - effect
// -----------------------------------------------------------------------------

const isDataType = (u: object): u is Option.Option<unknown> | Either.Either<unknown, unknown> =>
Option.TypeId in u ||
Either.TypeId in u

const makeRead: {
<E, A>(effect: Effect.Effect<Scope.Scope, E, A>, options?: {
readonly initialValue?: A
Expand Down Expand Up @@ -401,8 +406,10 @@ const makeRead: {
return function(get: Context, providedRuntime?: Runtime.Runtime<any>) {
const value = create(get)
if (typeof value === "object" && value !== null) {
if (Effect.EffectTypeId in value) {
return effect(get, value, options, providedRuntime)
if (isDataType(value)) {
return value
} else if (Effect.EffectTypeId in value) {
return effect(get, value as any, options, providedRuntime)
} else if (Stream.StreamTypeId in value) {
return stream(get, value, options, providedRuntime)
} else if (Layer.LayerTypeId in value) {
Expand All @@ -412,7 +419,9 @@ const makeRead: {
return value
}
} else if (typeof arg === "object" && arg !== null) {
if (Effect.EffectTypeId in arg) {
if (isDataType(arg)) {
return state(arg)
} else if (Effect.EffectTypeId in arg) {
return function(get: Context, providedRuntime?: Runtime.Runtime<any>) {
return effect(get, arg, options, providedRuntime)
}
Expand Down
14 changes: 13 additions & 1 deletion packages/rx/test/Rx.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as Registry from "@effect-rx/rx/Registry"
import * as Result from "@effect-rx/rx/Result"
import * as Rx from "@effect-rx/rx/Rx"
import { Cause, FiberRef } from "effect"
import { Cause, Either, FiberRef } from "effect"
import * as Context from "effect/Context"
import * as Effect from "effect/Effect"
import * as Hash from "effect/Hash"
Expand Down Expand Up @@ -752,6 +752,18 @@ describe("Rx", () => {

cancel()
})

it("Option is not an Effect", async () => {
const rx = Rx.make(Option.none<string>())
const r = Registry.make()
assert.deepStrictEqual(r.get(rx), Option.none())
})

it("Either is not an Effect", async () => {
const rx = Rx.make(Either.right(123))
const r = Registry.make()
assert.deepStrictEqual(r.get(rx), Either.right(123))
})
})

interface BuildCounter {
Expand Down
Loading

0 comments on commit c7612e2

Please sign in to comment.