Effect 3.8 (Release)
Effect 3.8 has been released! This release includes a number of new features and improvements. Here’s a summary of what’s new:
With this api you can create a logger that uses the console.{log,info,warn,error,trace}
functions,
depending on the log level of each message.
This is useful in environments such as browsers, where the different log levels are styled differently.
For example:
1import { Effect, Logger } from "effect"2
3const loggerLayer = Logger.replace(4 Logger.defaultLogger,5 Logger.withLeveledConsole(Logger.stringLogger)6)7
8Effect.gen(function* () {9 yield* Effect.logError("an error")10 yield* Effect.logInfo("an info")11}).pipe(Effect.provide(loggerLayer))
Many of the data types in Effect can now be used directly as Effect
’s.
These include:
Ref<A>
-Effect<A>
, equivalent toRef.get
SynchronizedRef<A>
-Effect<A>
, equivalent toSynchronizedRef.get
SubscriptionRef<A>
-Effect<A>
, equivalent toSubscriptionRef.get
Deferred<A, E>
-Effect<A, E>
, equivalent toDeferred.await
Dequeue<A>
-Effect<A>
, equivalent toQueue.take
Fiber<A, E>
-Effect<A, E>
, equivalent toFiber.join
FiberRef<A>
-Effect<A>
, equivalent toFiberRef.get
Semaphore.withPermitsIfAvailable
will attempt to run an effect immediately if permits are available.
It will return an Option<A>
from the result of the effect, depending on whether the permits were available.
1import { Effect } from "effect"2
3Effect.gen(function*() {4 const semaphore = yield* Effect.makeSemaphore(1)5
6 // returns Option.some("foo")7 yield* semaphore.withPermitsIfAvailable(1)(Effect.succeed("foo"))8 // returns Option.none()9 yield* semaphore.withPermitsIfAvailable(2)(Effect.succeed("bar"))10})
You can create an Effect.Latch
with Effect.makeLatch
, which can be used to synchronize multiple effects.
The latch can be opened, closed and waited on.
1import { Effect } from "effect"2
3Effect.gen(function* () {4 // Create a latch, starting in the closed state5 const latch = yield* Effect.makeLatch(false)6
7 // Fork a fiber that logs "open sesame" when the latch is opened8 const fiber = yield* Effect.log("open sesame").pipe(9 latch.whenOpen,10 Effect.fork11 )12
13 // Open the latch14 yield* latch.open15
16 // Wait for the latch to be opened17 yield* fiber.await18
19 // Release all waiters, without opening the latch20 yield* latch.release21})
Stream.share
is a reference counted equivalent of the Stream.broadcastDynamic
api.
It is useful when you want to share a Stream
, and ensure any resources are finalized when no more consumers are subscribed.
1import { Effect, Stream } from "effect"2
3Effect.gen(function*() {4 const sharedStream = yield* Effect.acquireRelease(5 Effect.log("Stream acquired").pipe(6 Effect.as(Stream.never)7 ),8 () => Effect.log("Stream released")9 ).pipe(10 Stream.unwrapScoped,11 Stream.share({ capacity: 16 })12 )13
14 // Nothing is logged yet15
16 yield* Stream.runDrain(sharedStream)17 // The upstream will now start emitting values.18 // If the downstream is interrupted, the upstream will also be finalized.19})
The @effect/platform/HttpClient
module has been refactored to reduce and simplify the api surface.
The HttpClient.fetch
client implementation has been removed. Instead, you can
access a HttpClient
using the corresponding Context.Tag
.
1import { FetchHttpClient, HttpClient } from "@effect/platform"2import { Effect } from "effect"3
4Effect.gen(function* () {5 const client = yield* HttpClient.HttpClient6
7 // make a get request8 yield* client.get("https://jsonplaceholder.typicode.com/todos/1")9}).pipe(10 Effect.scoped,11 // the fetch client has been moved to the `FetchHttpClient` module12 Effect.provide(FetchHttpClient.layer)13)
Instead of being a function that returns the response, the HttpClient
interface now uses methods to make requests.
Some shorthand methods have been added to the HttpClient
interface to make
less complex requests easier to implement.
1import {2 FetchHttpClient,3 HttpClient,4 HttpClientRequest5} from "@effect/platform"6import { Effect } from "effect"7
8Effect.gen(function* () {9 const client = yield* HttpClient.HttpClient10
11 // make a get request12 yield* client.get("https://jsonplaceholder.typicode.com/todos/1")13 // make a post request14 yield* client.post("https://jsonplaceholder.typicode.com/todos")15
16 // execute a request instance17 yield* client.execute(18 HttpClientRequest.get("https://jsonplaceholder.typicode.com/todos/1")19 )20})
The HttpClientResponse
helpers that also supplied the Scope
have been removed.
Instead, you can use the HttpClientResponse
methods directly, and explicitly
add a Effect.scoped
to the pipeline.
1import { FetchHttpClient, HttpClient } from "@effect/platform"2import { Effect } from "effect"3
4Effect.gen(function* () {5 const client = yield* HttpClient.HttpClient6
7 yield* client.get("https://jsonplaceholder.typicode.com/todos/1").pipe(8 Effect.flatMap((response) => response.json),9 Effect.scoped // supply the `Scope`10 )11})
Including the HttpClientRequest
body apis, which is to make them more
discoverable.
A new experimental effect/Mailbox
module has been added. Mailbox
is an asynchronous queue
that can have a done / failure signal.
It is useful when you want to communicate between effects, and have a way to determine that the communication is complete.
1import { Chunk, Effect, Mailbox } from "effect"2import * as assert from "node:assert"3
4Effect.gen(function* () {5 const mailbox = yield* Mailbox.make<number, string>()6
7 // add messages to the mailbox8 yield* mailbox.offer(1)9 yield* mailbox.offer(2)10 yield* mailbox.offerAll([3, 4, 5])11
12 // take messages from the mailbox13 const [messages, done] = yield* mailbox.takeAll14 assert.deepStrictEqual(Chunk.toReadonlyArray(messages), [1, 2, 3, 4, 5])15 assert.strictEqual(done, false)16
17 // signal that the mailbox is done18 yield* mailbox.end19 const [messages2, done2] = yield* mailbox.takeAll20 assert.deepStrictEqual(messages2, Chunk.empty())21 assert.strictEqual(done2, true)22
23 // signal that the mailbox is failed24 yield* mailbox.fail("boom")25
26 // turn the mailbox into a stream27 const stream = Mailbox.toStream(mailbox)28})
Some common FiberRef
instances are now cached, which improves performance of the Effect runtime.
You can now access the available keys of a RcMap
or MutableMap
using the keys
api.
For example:
1const map = MutableHashMap.make([["a", "a"], ["b", "b"], ["c", "c"]])2const keys = MutableHashMap.keys(map) // ["a", "b", "c"]
And for RcMap
:
1Effect.gen(function* () {2 const map = yield* RcMap.make({3 lookup: (key) => Effect.succeed(key)4 })5
6 yield* RcMap.get(map, "a")7 yield* RcMap.get(map, "b")8 yield* RcMap.get(map, "c")9
10 const keys = yield* RcMap.keys(map) // ["a", "b", "c"]11})
Some new apis for converting between a Duration
and it’s corresponding parts have been added:
Duration.toMinutes
Duration.toHours
Duration.toDays
Duration.toWeeks
Duration.parts
There were several other smaller changes made. Take a look through the CHANGELOG to see them all: CHANGELOG.
Don’t forget to join our Discord Community to follow the last updates and discuss every tiny detail!