Effect 3.6 (Release)
Effect 3.6 has been released! This release includes a number of new features and improvements. Here’s a summary of what’s new:
The DateTime
module provides functionality for working with time, including
support for time zones and daylight saving time.
It has two main data types: DateTime.Utc
and DateTime.Zoned
.
A DateTime.Utc
represents a time in Coordinated Universal Time (UTC), and
a DateTime.Zoned
contains both a UTC timestamp and a time zone.
There is also a CurrentTimeZone
service, for setting a time zone contextually.
import { DateTime, Effect } from "effect"
Effect.gen(function* () { // Get the current time in the current time zone const now = yield* DateTime.nowInCurrentZone
// Math functions are included const tomorrow = DateTime.add(now, { days: 1 })
// Convert to a different time zone // The UTC portion of the `DateTime` is preserved and only the time zone is // changed const sydneyTime = tomorrow.pipe( DateTime.unsafeSetZoneNamed("Australia/Sydney"), )}).pipe(DateTime.withCurrentZoneNamed("America/New_York"))
Stream.asyncPush
can be used to create a Stream
from an external push-based resource.
You can customize the buffer size and strategy by passing an object as the
second argument with the bufferSize
and strategy
fields.
import { Effect, Stream } from "effect";
Stream.asyncPush<string>( (emit) => Effect.acquireRelease( Effect.gen(function* () { yield* Effect.log("subscribing"); return setInterval(() => emit.single("tick"), 1000); }), (handle) => Effect.gen(function* () { yield* Effect.log("unsubscribing"); clearInterval(handle); }), ), { bufferSize: 16, strategy: "dropping" },);
To access the fully typed keys of a struct, you can use the Struct.keys
function.
import { Struct } from "effect"
const symbol: unique symbol = Symbol()
const value = { a: 1, b: 2, [symbol]: 3}
const keys: Array<"a" | "b"> = Struct.keys(value)
The @effect/sql-kysely
package provides @effect/sql
integration with the kysely
query builder apis.
// create a Tag with your `Database` typeclass KyselyDB extends Context.Tag("KyselyDB")<KyselyDB, Kysely<Database>>() {}
Effect.gen(function*() { // access the service and execute queries const db = yield* KyselyDB
yield* db.schema .createTable("users") .addColumn("id", "integer", (c) => c.primaryKey().autoIncrement()) .addColumn("userName", "text", (c) => c.notNull())
const inserted = yield* db.insertInto("users").values({ userName: "Alice" }).returningAll() const selected = yield* db.selectFrom("users").selectAll() const updated = yield* db.updateTable("users").set({ userName: "Bob" }).returningAll() const deleted = yield* db.deleteFrom("users").returningAll()})
This api allows you to randomly select an item from an Iterable
.
Unless the Iterable
is “NonEmpty”, then the Effect can fail with a Cause.NoSuchElementException
.
import { Random } from "effect"
Effect.gen(function* () { const randomItem = yield* Random.choice([1, 2, 3]) console.log(randomItem)})
If the onlyEffect
option for Effect.tap
is set to true
, then it will ensure the side effect only uses Effect
’s.
This can be useful when you want to add strictness to your program.
Refinements can now be used with Predicate.tuple
and Predicate.struct
to narrow the resulting type.
import { Predicate } from "effect"
const isTrue = (u: unknown): u is true => u === true
// will narrow the type to { isTrue: true }Predicate.struct({ isTrue })
Some new lifetime hook apis have been added to the Stream
module:
Stream.onStart
- run an effect when the stream startsStream.onEnd
- run an effect when the stream ends without error
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!