From 966e22592eca131d237c9b3e22426c2afb03cf0e Mon Sep 17 00:00:00 2001 From: gcanti Date: Wed, 17 Jan 2024 18:57:28 +0100 Subject: [PATCH] update effect --- package.json | 4 ++-- pages/docs/error-management/retrying.mdx | 4 ++-- pages/docs/error-management/timing-out.mdx | 12 +++++------ pnpm-lock.yaml | 20 +++++++++---------- src/guide/error-management/retrying/retryN.ts | 2 +- src/guide/error-management/timing-out/1.ts | 10 +++------- src/guide/error-management/timing-out/2.ts | 12 +++++++---- src/guide/error-management/timing-out/3.ts | 12 +++++++---- .../error-management/timing-out/disconnect.ts | 12 +++++++---- .../error-management/timing-out/program.ts | 2 +- .../timing-out/timeoutFail.ts | 12 +++++++++-- .../error-management/timing-out/timeoutTo.ts | 2 +- .../ref/example-2-concurrent-pipe.ts | 4 ++-- .../state-management/ref/example-2-pipe.ts | 4 ++-- src/testing/clock/timeout-pipe.ts | 6 +++++- src/testing/clock/timeout.ts | 6 +++++- 16 files changed, 74 insertions(+), 50 deletions(-) diff --git a/package.json b/package.json index 1d45fab6f..fa2192298 100644 --- a/package.json +++ b/package.json @@ -23,7 +23,7 @@ }, "devDependencies": { "@effect/language-service": "^0.0.21", - "@effect/opentelemetry": "^0.27.0", + "@effect/opentelemetry": "^0.30.7", "@mdx-js/mdx": "^2.3.0", "@opentelemetry/exporter-trace-otlp-http": "0.43.0", "@opentelemetry/sdk-trace-base": "^1.17.1", @@ -34,7 +34,7 @@ "@typescript-eslint/eslint-plugin": "^6.7.4", "@typescript-eslint/parser": "^6.7.4", "autoprefixer": "^10.4.14", - "effect": "2.0.0-next.60", + "effect": "2.1.1", "eslint": "^8.38.0", "eslint-config-next": "^13.3.0", "eslint-config-prettier": "^8.8.0", diff --git a/pages/docs/error-management/retrying.mdx b/pages/docs/error-management/retrying.mdx index f5cbe2f87..c607a4fba 100644 --- a/pages/docs/error-management/retrying.mdx +++ b/pages/docs/error-management/retrying.mdx @@ -28,9 +28,9 @@ Effect.retry(effect, policy) ``` -## retryN +## retry n times -There is a shortcut when the policy is trivial and the failed effect is immediately retried: `retryN(effect)`. +There is a shortcut when the policy is trivial and the failed effect is immediately retried: ```ts file=/src/guide/error-management/retrying/retryN.ts diff --git a/pages/docs/error-management/timing-out.mdx b/pages/docs/error-management/timing-out.mdx index be3d9377f..3b4722b08 100644 --- a/pages/docs/error-management/timing-out.mdx +++ b/pages/docs/error-management/timing-out.mdx @@ -4,10 +4,10 @@ In the world of programming, we often deal with tasks that take some time to com ## timeout -Effect lets us timeout any effect using the `Effect.timeout` function, which returns a new effect that succeeds with an [`Option`](../data-types/option): +Effect lets us timeout any effect using the `Effect.timeout` function. It returns a new effect with the following characteristics: -- A `None` indicates that the timeout elapsed before the effect could complete its execution. -- A `Some` indicates that the effect completed before the timeout elapsed, and the `Some` contains the result of the effect. +- If an error is returned, it signifies that the timeout expired before the completion of the effect's execution. +- If successful, it indicates that the effect finished within the specified timeout, and the result of the effect is included. If an effect times out, then instead of continuing to execute in the background, it will be interrupted so no resources will be wasted. @@ -19,19 +19,19 @@ Suppose we have the following effect: When we apply `Effect.timeout` to `program`, it behaves in one of the following ways: -1. If the original effect (`program` in this case) completes before the timeout elapses, it returns `Some` of the produced value by the original effect. Here's an example: +1. If the original effect (`program` in this case) completes before the timeout elapses, it returns the produced value by the original effect. Here's an example: ```ts file=/src/guide/error-management/timing-out/1.ts ``` -2. If the timeout elapses before the original effect completes, and the effect is interruptible, it will be immediately interrupted, and the timeout operation produces a `None` value. Here's an example: +2. If the timeout elapses before the original effect completes, and the effect is interruptible, it will be immediately interrupted, and the timeout operation produces a `NoSuchElementException` error. Here's an example: ```ts file=/src/guide/error-management/timing-out/2.ts ``` -3. If the effect is uninterruptible, it will be blocked until the original effect safely finishes its work, and then the timeout operator produces a `None` value. Here's an example: +3. If the effect is uninterruptible, it will be blocked until the original effect safely finishes its work, and then the timeout operator produces a `NoSuchElementException` error. Here's an example: ```ts file=/src/guide/error-management/timing-out/3.ts diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 8848411cb..eba4212bc 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -29,8 +29,8 @@ devDependencies: specifier: ^0.0.21 version: 0.0.21 '@effect/opentelemetry': - specifier: ^0.27.0 - version: 0.27.0(@opentelemetry/api@1.6.0)(@opentelemetry/resources@1.17.1)(@opentelemetry/sdk-trace-base@1.17.1)(@opentelemetry/semantic-conventions@1.17.1)(effect@2.0.0-next.60) + specifier: ^0.30.7 + version: 0.30.7(@opentelemetry/api@1.6.0)(@opentelemetry/resources@1.17.1)(@opentelemetry/sdk-trace-base@1.17.1)(@opentelemetry/semantic-conventions@1.17.1)(effect@2.1.1) '@mdx-js/mdx': specifier: ^2.3.0 version: 2.3.0 @@ -62,8 +62,8 @@ devDependencies: specifier: ^10.4.14 version: 10.4.14(postcss@8.4.23) effect: - specifier: 2.0.0-next.60 - version: 2.0.0-next.60 + specifier: 2.1.1 + version: 2.1.1 eslint: specifier: ^8.38.0 version: 8.39.0 @@ -162,8 +162,8 @@ packages: resolution: {integrity: sha512-e8vfKbjnbYiyneBincEFS0tzXluopGK77OkVFbPRtUbNDS5tJfb+jiwOQEiqASDsadcZmd+9J9+Q6v/z7GuN2g==} dev: true - /@effect/opentelemetry@0.27.0(@opentelemetry/api@1.6.0)(@opentelemetry/resources@1.17.1)(@opentelemetry/sdk-trace-base@1.17.1)(@opentelemetry/semantic-conventions@1.17.1)(effect@2.0.0-next.60): - resolution: {integrity: sha512-qJGRWKEjG7Nseeb+gKq3NfAXNgqeWi8Q3F2u24LtA/2unwzu7cgMiCQ7Xi5r513CVbO870mwEDHdCfAVRlte0g==} + /@effect/opentelemetry@0.30.7(@opentelemetry/api@1.6.0)(@opentelemetry/resources@1.17.1)(@opentelemetry/sdk-trace-base@1.17.1)(@opentelemetry/semantic-conventions@1.17.1)(effect@2.1.1): + resolution: {integrity: sha512-a9F4qzyJqIDJSQqq1tyup2q1sMyVUZ4544GHljvhRAwUR9gxr1Yu0vBBY15DyE8ey/jZP2MKVWlacQkUy3mxKw==} peerDependencies: '@opentelemetry/api': ^1.6 '@opentelemetry/resources': ^1.17 @@ -172,7 +172,7 @@ packages: '@opentelemetry/sdk-trace-node': ^1.17 '@opentelemetry/sdk-trace-web': ^1.17 '@opentelemetry/semantic-conventions': ^1.17 - effect: 2.0.0-next.60 + effect: ^2.1.1 peerDependenciesMeta: '@opentelemetry/sdk-metrics': optional: true @@ -187,7 +187,7 @@ packages: '@opentelemetry/resources': 1.17.1(@opentelemetry/api@1.6.0) '@opentelemetry/sdk-trace-base': 1.17.1(@opentelemetry/api@1.6.0) '@opentelemetry/semantic-conventions': 1.17.1 - effect: 2.0.0-next.60 + effect: 2.1.1 dev: true /@eslint-community/eslint-utils@4.4.0(eslint@8.39.0): @@ -2075,8 +2075,8 @@ packages: resolution: {integrity: sha512-WMwm9LhRUo+WUaRN+vRuETqG89IgZphVSNkdFgeb6sS/E4OrDIN7t48CAewSHXc6C8lefD8KKfr5vY61brQlow==} dev: true - /effect@2.0.0-next.60: - resolution: {integrity: sha512-23KhlVACgrg5UPFu9i4szybSU4cCU4T/7CX4pe0jV84QBZX0zm96WzwCtg6dqOnmUzBL7hm6S+iiPW2Rab13Uw==} + /effect@2.1.1: + resolution: {integrity: sha512-VA9t/HDs14TdJA7j+VroaPrKxmf0YLA4HMQ0eSxA/P2hv8Gre+vKGk3gxpR45OBR0KK7QOq3gMyCbVr/pW2F/g==} dev: true /electron-to-chromium@1.4.369: diff --git a/src/guide/error-management/retrying/retryN.ts b/src/guide/error-management/retrying/retryN.ts index 1d1bc0d0c..4720365f4 100644 --- a/src/guide/error-management/retrying/retryN.ts +++ b/src/guide/error-management/retrying/retryN.ts @@ -1,7 +1,7 @@ import { Effect } from "effect" import { effect } from "./fake" -Effect.runPromise(Effect.retryN(effect, 5)) +Effect.runPromise(Effect.retry(effect, { times: 5 })) /* Output: failure diff --git a/src/guide/error-management/timing-out/1.ts b/src/guide/error-management/timing-out/1.ts index b89540ec4..9d9e904a2 100644 --- a/src/guide/error-management/timing-out/1.ts +++ b/src/guide/error-management/timing-out/1.ts @@ -8,17 +8,13 @@ const program = Effect.gen(function* (_) { return "some result" }) -// $ExpectType Effect> +// $ExpectType Effect const main = program.pipe(Effect.timeout("3 seconds")) -Effect.runPromise(main).then(console.log) +Effect.runPromise(main).then(console.log, console.error) /* Output: start doing something... my job is finished! -{ - _id: "Option", - _tag: "Some", - value: "some result" -} +some result */ diff --git a/src/guide/error-management/timing-out/2.ts b/src/guide/error-management/timing-out/2.ts index a1c624fe3..ac048080d 100644 --- a/src/guide/error-management/timing-out/2.ts +++ b/src/guide/error-management/timing-out/2.ts @@ -8,15 +8,19 @@ const program = Effect.gen(function* (_) { return "some result" }) -// $ExpectType Effect> +// $ExpectType Effect const main = program.pipe(Effect.timeout("1 seconds")) -Effect.runPromise(main).then(console.log) +Effect.runPromise(main).then(console.log, console.error) /* Output: start doing something... { - _id: "Option", - _tag: "None" + _id: 'FiberFailure', + cause: { + _id: 'Cause', + _tag: 'Fail', + failure: { _tag: 'NoSuchElementException' } + } } */ diff --git a/src/guide/error-management/timing-out/3.ts b/src/guide/error-management/timing-out/3.ts index 916db26ec..961f7c902 100644 --- a/src/guide/error-management/timing-out/3.ts +++ b/src/guide/error-management/timing-out/3.ts @@ -8,16 +8,20 @@ const program = Effect.gen(function* (_) { return "some result" }) -// $ExpectType Effect> +// $ExpectType Effect const main = program.pipe(Effect.uninterruptible, Effect.timeout("1 seconds")) -Effect.runPromise(main).then(console.log) +Effect.runPromise(main).then(console.log, console.error) /* Output: start doing something... my job is finished! { - _id: "Option", - _tag: "None" + _id: 'FiberFailure', + cause: { + _id: 'Cause', + _tag: 'Fail', + failure: { _tag: 'NoSuchElementException' } + } } */ diff --git a/src/guide/error-management/timing-out/disconnect.ts b/src/guide/error-management/timing-out/disconnect.ts index 92fdca90e..13023c8ed 100644 --- a/src/guide/error-management/timing-out/disconnect.ts +++ b/src/guide/error-management/timing-out/disconnect.ts @@ -8,20 +8,24 @@ const program = Effect.gen(function* (_) { return "some result" }) -// $ExpectType Effect> +// $ExpectType Effect const main = program.pipe( Effect.uninterruptible, Effect.disconnect, Effect.timeout("1 seconds") ) -Effect.runPromise(main).then(console.log) +Effect.runPromise(main).then(console.log, console.error) /* Output: start doing something... { - _id: "Option", - _tag: "None" + _id: 'FiberFailure', + cause: { + _id: 'Cause', + _tag: 'Fail', + failure: { _tag: 'NoSuchElementException' } + } } my job is finished! */ diff --git a/src/guide/error-management/timing-out/program.ts b/src/guide/error-management/timing-out/program.ts index b9114129e..5ab0d542a 100644 --- a/src/guide/error-management/timing-out/program.ts +++ b/src/guide/error-management/timing-out/program.ts @@ -8,5 +8,5 @@ const program = Effect.gen(function* (_) { return "some result" }) -// $ExpectType Effect> +// $ExpectType Effect const main = program.pipe(Effect.timeout("3 seconds")) diff --git a/src/guide/error-management/timing-out/timeoutFail.ts b/src/guide/error-management/timing-out/timeoutFail.ts index 4baf6e85a..faeeb7fe1 100644 --- a/src/guide/error-management/timing-out/timeoutFail.ts +++ b/src/guide/error-management/timing-out/timeoutFail.ts @@ -16,9 +16,17 @@ const main = program.pipe( }) ) -Effect.runPromise(main).then(console.log) +Effect.runPromise(main).then(console.log, console.error) /* Output: start doing something... -Error: timeout +{ + _id: 'FiberFailure', + cause: { + _id: 'Cause', + _tag: 'Fail', + failure: Error: timeout + ... stack trace ... + } +} */ diff --git a/src/guide/error-management/timing-out/timeoutTo.ts b/src/guide/error-management/timing-out/timeoutTo.ts index 2b6b55aad..d444d46f3 100644 --- a/src/guide/error-management/timing-out/timeoutTo.ts +++ b/src/guide/error-management/timing-out/timeoutTo.ts @@ -12,7 +12,7 @@ const program = Effect.gen(function* (_) { const main = program.pipe( Effect.timeoutTo({ duration: "1 seconds", - // let's return an Either instead of an Option + // let's return an Either onSuccess: (result): Either.Either => Either.right(result), onTimeout: (): Either.Either => Either.left("timeout!") }) diff --git a/src/guide/state-management/ref/example-2-concurrent-pipe.ts b/src/guide/state-management/ref/example-2-concurrent-pipe.ts index 860edc156..e77daa168 100644 --- a/src/guide/state-management/ref/example-2-concurrent-pipe.ts +++ b/src/guide/state-management/ref/example-2-concurrent-pipe.ts @@ -7,7 +7,7 @@ const getNames = Ref.make(Chunk.empty()).pipe( const fiber1 = ReadLine.readLine( "Please enter a name or `q` to exit: " ).pipe( - Effect.repeatWhileEffect((name) => { + Effect.repeat({ while: (name) => { if (name === "q") { return Effect.succeed(false) } else { @@ -16,7 +16,7 @@ const getNames = Ref.make(Chunk.empty()).pipe( Effect.as(true) ) } - }), + } }), Effect.fork ) const fiber2 = Effect.fork( diff --git a/src/guide/state-management/ref/example-2-pipe.ts b/src/guide/state-management/ref/example-2-pipe.ts index aca39c83e..017e3dac5 100644 --- a/src/guide/state-management/ref/example-2-pipe.ts +++ b/src/guide/state-management/ref/example-2-pipe.ts @@ -5,7 +5,7 @@ import * as ReadLine from "./ReadLine" const getNames = Ref.make(Chunk.empty()).pipe( Effect.flatMap((ref) => ReadLine.readLine("Please enter a name or `q` to exit: ").pipe( - Effect.repeatWhileEffect((name) => { + Effect.repeat({ while: (name) => { if (name === "q") { return Effect.succeed(false) } else { @@ -14,7 +14,7 @@ const getNames = Ref.make(Chunk.empty()).pipe( Effect.as(true) ) } - }), + } }), Effect.flatMap(() => Ref.get(ref)) ) ) diff --git a/src/testing/clock/timeout-pipe.ts b/src/testing/clock/timeout-pipe.ts index 116330c21..fd7c8886b 100644 --- a/src/testing/clock/timeout-pipe.ts +++ b/src/testing/clock/timeout-pipe.ts @@ -3,7 +3,11 @@ import * as assert from "node:assert" const test = pipe( Effect.sleep("5 minutes"), - Effect.timeout("1 minutes"), + Effect.timeoutTo({ + duration: "1 minutes", + onSuccess: Option.some, + onTimeout: () => Option.none() + }), Effect.fork, Effect.tap(() => // Adjust the TestClock by 1 minute to simulate the passage of time diff --git a/src/testing/clock/timeout.ts b/src/testing/clock/timeout.ts index bb0733e51..9083eca1c 100644 --- a/src/testing/clock/timeout.ts +++ b/src/testing/clock/timeout.ts @@ -5,7 +5,11 @@ const test = Effect.gen(function* (_) { // Create a fiber that sleeps for 5 minutes and then times out after 1 minute const fiber = yield* _( Effect.sleep("5 minutes"), - Effect.timeout("1 minutes"), + Effect.timeoutTo({ + duration: "1 minutes", + onSuccess: Option.some, + onTimeout: () => Option.none() + }), Effect.fork )