Skip to content

Commit

Permalink
proposal for Effect.Tag conundrum (#2266)
Browse files Browse the repository at this point in the history
  • Loading branch information
patroza authored Mar 16, 2024
1 parent 6f7dfc9 commit 3851a02
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/tasty-seals-film.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"effect": patch
---

fix Effect.Tag generated proxy functions to work with andThen/tap, or others that do function/isEffect checks
14 changes: 12 additions & 2 deletions packages/effect/src/Effect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5336,8 +5336,18 @@ export const Tag: <const Id extends string>(id: Id) => <Self, Type>() =>
if (cache.has(prop)) {
return cache.get(prop)
}
// @ts-expect-error
const fn = (...args: Array<any>) => core.andThen(TagClass, (s: any) => s[prop](...args))
const fn = (...args: Array<any>) =>
// @ts-expect-error
core.andThen(TagClass, (s: any) => {
if (typeof s[prop] === "function") {
// @ts-expect-error
cache.set(prop, (...args: Array<any>) => core.andThen(TagClass, (s: any) => s[prop](...args)))
return s[prop](...args)
}
// @ts-expect-error
cache.set(prop, core.andThen(TagClass, (s) => s[prop]))
return s[prop]
})
// @ts-expect-error
const cn = core.andThen(TagClass, (s) => s[prop])
Object.assign(fn, cn)
Expand Down
18 changes: 18 additions & 0 deletions packages/effect/test/Effect/environment.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,24 @@ class NumberTag extends Effect.Tag("NumberTag")<NumberTag, number>() {
}

describe("Effect", () => {
describe("and Then", () => {
it.effect("effect tag", () =>
Effect.gen(function*($) {
const [n, s, z] = yield* $(Effect.all([
Effect.andThen(Effect.unit, DemoTag.getNumbers),
Effect.andThen(Effect.succeed("a"), DemoTag.strings),
Effect.andThen(Effect.succeed("a"), DemoTag.fn)
]))
expect(n).toEqual([0, 1])
expect(s).toEqual(["a", "b"])
expect(z).toEqual(["a"])
}).pipe(Effect.provideService(DemoTag, {
getNumbers: () => [0, 1],
strings: ["a", "b"],
fn: (...args) => Array.from(args),
fnGen: (s) => [s]
})))
})
it.effect("effect tag", () =>
Effect.gen(function*($) {
const [n, s, z] = yield* $(Effect.all([
Expand Down

0 comments on commit 3851a02

Please # to comment.