From cf6c08267212ba566b34eba13e66b2854f1bd3ae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Valverd=C3=A9?= Date: Thu, 16 Jul 2026 22:00:17 +0200 Subject: [PATCH] Update README --- packages/effect-lens-next/README.md | 42 ++++++++++++++--------------- packages/effect-lens/README.md | 5 +--- 2 files changed, 22 insertions(+), 25 deletions(-) diff --git a/packages/effect-lens-next/README.md b/packages/effect-lens-next/README.md index 67dc5ae..16fc0f2 100644 --- a/packages/effect-lens-next/README.md +++ b/packages/effect-lens-next/README.md @@ -4,13 +4,13 @@ A Lens type for [Effect](https://effect.website/) to easily manage nested state. ## Install ``` -npm install effect-lens@beta effect@4.0.0-beta.85 -yarn add effect-lens@beta effect@4.0.0-beta.85 -bun add effect-lens@beta effect@4.0.0-beta.85 +npm install effect-lens@beta effect@4.0.0-beta.98 +yarn add effect-lens@beta effect@4.0.0-beta.98 +bun add effect-lens@beta effect@4.0.0-beta.98 ``` ## Peer dependencies -- `effect` 4.0.0-beta.85 +- `effect` 4.0.0-beta.98 ## Quickstart @@ -64,7 +64,7 @@ You can also create Lenses manually using `make` by providing: - `get`: an effect that reads the current value, - `changes`: a stream of value changes, - `commit`: an effectful write primitive, -- `lock`: an effect that produces the lock used to serialize writes. +- `lock`: an effect that produces the lock used to serialize writes and preserve atomicity. You can get pretty creative! Here's an example of a Lens that points to a specific key of the browser `LocalStorage`: ```typescript @@ -101,9 +101,6 @@ const lens = Effect.all([ ) ``` -Note: while Lens supports asynchronous effects for the proxy logic, we would recommend keeping them synchronous to preserve atomicity. - - ### Focusing Lenses can focus on a nested part of the data type they point to. @@ -242,30 +239,33 @@ const nameLens = lens.pipe( ### View -Effect 4 no longer provides its former `Subscribable` and `Readable` abstractions. This library provides a `View` interface, which you can use as a constraint to allow some parts of your app to only read and subscribe to the Lenses you provide them: +A `View` is a read-only, reactive view of a value: it lets you read the current value and observe subsequent changes. Every `Lens` is also a `View`, which you can use as a constraint to allow some parts of your app to only read and subscribe to the Lenses you provide them. It replaces Effect v3's `Subscribable` module: ```typescript const ref = yield* SubscriptionRef.make<{ readonly users: readonly User[] -}>({ users: [...] }) +}>({ users: [] }) -const someFunctionThatShouldOnlyHaveReadonlyAccessToTheState = ( - usersView: View.View -) => Effect.gen(function*() { - // Do whatever - const usersCountView = View.map(usersView, a => a.length) - const users = yield* usersView.get - yield* Effect.forkScoped(Stream.runForEach(usersView.changes, ...)) -}) +const logUserCount = (users: View.View) => + Effect.gen(function*() { + const userCount = View.map(users, users => users.length) + yield* Console.log(`There are ${yield* userCount.get} users`) + yield* Stream.runForEach( + userCount.changes, + count => Console.log(`There are now ${count} users`), + ) + }) -const lens = ref.pipe( +const usersLens = ref.pipe( Lens.fromSubscriptionRef, Lens.focusObjectOn("users"), ) -yield* someFunctionThatShouldOnlyHaveReadonlyAccessToTheState(lens) +yield* Effect.forkScoped(logUserCount(usersLens)) ``` +Use `Lens.asView` when you want to make that read-only boundary explicit. + #### Focusing -This library provides a `View` module with value and error transforms, recovery (`catch`, `catchCause`, `orElse`, `orElseSucceed`, and `retry`), and transforms to narrow the focus of `View`'s, same as Lenses: +Views can be focused in the same way as Lenses. The `View` module also provides value and error transforms, plus recovery (`catch`, `catchCause`, `orElse`, `orElseSucceed`, and `retry`): ```typescript import { View } from "effect-lens" diff --git a/packages/effect-lens/README.md b/packages/effect-lens/README.md index a34dad7..7668bde 100644 --- a/packages/effect-lens/README.md +++ b/packages/effect-lens/README.md @@ -64,7 +64,7 @@ You can also create Lenses manually using `make` by providing: - `get`: an effect that reads the current value, - `changes`: a stream of value changes, - `commit`: an effectful write primitive, -- `lock`: an effect that produces the lock used to serialize writes. +- `lock`: an effect that produces the lock used to serialize writes and preserve atomicity. You can get pretty creative! Here's an example of a Lens that points to a specific key of the browser `LocalStorage`: ```typescript @@ -101,9 +101,6 @@ const lens = Effect.all([ ) ``` -Note: while Lens supports asynchronous effects for the proxy logic, we would recommend keeping them synchronous to preserve atomicity. - - ### Focusing Lenses can focus on a nested part of the data type they point to.