Skip to content

Commit

Permalink
Gen context (#2857)
Browse files Browse the repository at this point in the history
Co-authored-by: maksim.khramtsov <maksim.khramtsov@btsdigital.kz>
  • Loading branch information
2 people authored and tim-smart committed May 28, 2024
1 parent 64e1ca4 commit e975f0e
Show file tree
Hide file tree
Showing 9 changed files with 51 additions and 11 deletions.
5 changes: 5 additions & 0 deletions .changeset/rare-spiders-occur.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"effect": minor
---

Support `this` argument for `{STM, Either, Option}.gen`
5 changes: 4 additions & 1 deletion packages/effect/src/Either.ts
Original file line number Diff line number Diff line change
Expand Up @@ -700,7 +700,10 @@ const adapter = Gen.adapter<EitherTypeLambda>()
* @category generators
* @since 2.0.0
*/
export const gen: Gen.Gen<EitherTypeLambda, Gen.Adapter<EitherTypeLambda>> = (f) => {
export const gen: Gen.Gen<EitherTypeLambda, Gen.Adapter<EitherTypeLambda>> = (...args) => {
const f = (args.length === 1)
? args[0]
: args[1].bind(args[0])
const iterator = f(adapter)
let state: IteratorYieldResult<any> | IteratorReturnResult<any> = iterator.next()
if (state.done) {
Expand Down
8 changes: 7 additions & 1 deletion packages/effect/src/Option.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1345,7 +1345,13 @@ const adapter = Gen.adapter<OptionTypeLambda>()
* @category generators
* @since 2.0.0
*/
export const gen: Gen.Gen<OptionTypeLambda, Gen.Adapter<OptionTypeLambda>> = (f) => {
export const gen: Gen.Gen<OptionTypeLambda, Gen.Adapter<OptionTypeLambda>> = (...args) => {
let f: any
if (args.length === 1) {
f = args[0]
} else {
f = args[1].bind(args[0])
}
const iterator = f(adapter)
let state: IteratorYieldResult<any> | IteratorReturnResult<any> = iterator.next()
if (state.done) {
Expand Down
9 changes: 7 additions & 2 deletions packages/effect/src/STM.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1065,8 +1065,13 @@ export interface Adapter {
* @since 2.0.0
* @category constructors
*/
export const gen: <Eff extends YieldWrap<STM<any, any, any>>, AEff>(
f: (resume: Adapter) => Generator<Eff, AEff, never>
export const gen: <Self, Eff extends YieldWrap<STM<any, any, any>>, AEff>(
...args:
| [
self: Self,
body: (this: Self, resume: Adapter) => Generator<Eff, AEff, never>
]
| [body: (resume: Adapter) => Generator<Eff, AEff, never>]
) => STM<
AEff,
[Eff] extends [never] ? never : [Eff] extends [YieldWrap<STM<infer _A, infer E, infer _R>>] ? E : never,
Expand Down
15 changes: 9 additions & 6 deletions packages/effect/src/Utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,17 +172,20 @@ export interface Variance<in out F extends TypeLambda, in R, out O, out E> {
readonly _E: Types.Covariant<E>
}

/**
* @category models
* @since 2.0.0
*/
/**
* @category models
* @since 2.0.0
*/
export interface Gen<F extends TypeLambda, Z> {
<K extends Variance<F, any, any, any> | YieldWrap<Kind<F, any, any, any, any>>, A>(
body: (resume: Z) => Generator<K, A, never>
<Self, K extends Variance<F, any, any, any> | YieldWrap<Kind<F, any, any, any, any>>, A>(
...args:
| [
self: Self,
body: (this: Self, resume: Z) => Generator<K, A, never>
]
| [
body: (resume: Z) => Generator<K, A, never>
]
): Kind<
F,
[K] extends [Variance<F, infer R, any, any>] ? R
Expand Down
5 changes: 4 additions & 1 deletion packages/effect/src/internal/stm/stm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -618,8 +618,11 @@ export const fromOption = <A>(option: Option.Option<A>): STM.STM<A, Option.Optio
* Inspired by https://github.com/tusharmath/qio/pull/22 (revised)
* @internal
*/
export const gen: typeof STM.gen = (f) =>
export const gen: typeof STM.gen = (...args) =>
suspend(() => {
const f = (args.length === 1)
? args[0]
: args[1].bind(args[0])
const iterator = f(pipe)
const state = iterator.next()
const run = (
Expand Down
4 changes: 4 additions & 0 deletions packages/effect/test/Either.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,16 @@ describe("Either", () => {
const f = Either.gen(function*($) {
yield* $(Either.left("err"))
})
const g = Either.gen({ context: "testContext" as const }, function*($) {
return yield* $(Either.right(this.context))
})
expect(a).toEqual(Either.right(3))
expect(b).toEqual(Either.right(10))
expect(c).toEqual(Either.right(undefined))
expect(d).toEqual(Either.right(2))
expect(e).toEqual(Either.left("err"))
expect(f).toEqual(Either.left("err"))
expect(g).toEqual(Either.right("testContext"))
})

it("exports", () => {
Expand Down
5 changes: 5 additions & 0 deletions packages/effect/test/Option.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,17 @@ describe("Option", () => {
const f = Option.gen(function*($) {
yield* $(Option.none())
})

const g = Option.gen({ ctx: "testContext" as const }, function*() {
return yield* Option.some(this.ctx)
})
expect(a).toEqual(Option.some(3))
expect(b).toEqual(Option.some(10))
expect(c).toEqual(Option.some(undefined))
expect(d).toEqual(Option.some(2))
expect(e).toEqual(Option.none())
expect(f).toEqual(Option.none())
expect(g).toEqual(Option.some("testContext"))
})

it("toString", () => {
Expand Down
6 changes: 6 additions & 0 deletions packages/effect/test/STM.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -957,6 +957,12 @@ describe("STM", () => {
assert.strictEqual(result, "test")
}))

it.effect("gen with context", () =>
STM.gen({ context: "Context" as const }, function*() {
const result = yield* STM.succeed(this.context)
assert.strictEqual(result, "Context")
}))

it.effect("summarized - returns summary and value", () =>
Effect.gen(function*($) {
const transaction = STM.gen(function*($) {
Expand Down

0 comments on commit e975f0e

Please sign in to comment.