Update docs
Some checks failed
Lint / lint (push) Failing after 15s
Publish / publish (push) Failing after 14s

This commit is contained in:
Julien Valverdé
2026-03-25 02:08:09 +01:00
parent 122272b29e
commit dbb7ae3aef
2 changed files with 94 additions and 4 deletions

View File

@@ -1,5 +1,5 @@
import { KeyValueStore } from "@effect/platform"
import { Array, Console, Effect, Stream, SubscriptionRef } from "effect"
import { Array, Console, DateTime, Effect, Stream, SubscriptionRef } from "effect"
import { Lens } from "effect-lens"
Effect.gen(function*() {
@@ -17,7 +17,38 @@ Effect.gen(function*() {
})
Effect.gen(function*() {
const lens = Lens.make({
get: Effect.andThen(KeyValueStore.KeyValueStore, )
interface User {
readonly name: string
readonly age: DateTime.Utc
}
// The state of your app
const ref = yield* SubscriptionRef.make<{
readonly users: readonly User[]
}>({
users: [
{ name: "Jean Dupont", age: yield* DateTime.make("03/25/1969") },
{ name: "Juan Joya Borja", age: yield* DateTime.make("04/05/1956") },
{ name: "Benzemonstre", age: yield* DateTime.make("06/12/2000") },
]
})
})
// \/ Lens<User, NoSuchElementException, NoSuchElementException, never, never>
const jeanDupontLens = ref.pipe(
Lens.fromSubscriptionRef, // Creates a lens that proxies the ref
Lens.focusField("users"), // Creates a focused lens that points to the user field
Lens.focusArrayAt(0), // Creates a focused lens that points to the first entry of the user array
)
// Reading or writing from this lense can fail with NoSuchElementException
// This is because of Lens.focusArrayAt(0), as reading and writing to an array can be an unsafe operation
const jeanDupont = yield* Lens.get(jeanDupontLens)
yield* Lens.set(
// You can focus even further down
Lens.focusField(jeanDupontLens, "age"),
yield* DateTime.make("03/25/1970"),
)
// Mutations with the parent state are performed immutably by default
// unless you use a specific mutable transform such as 'focusMutableField'
})