@@ -247,7 +247,7 @@ const ref = yield* SubscriptionRef.make<{
|
||||
|
||||
const logUserCount = (users: View.View<readonly User[]>) =>
|
||||
Effect.gen(function*() {
|
||||
const userCount = View.map(users, users => users.length)
|
||||
const userCount = View.focusArrayLength(users)
|
||||
yield* Console.log(`There are ${yield* View.get(userCount)} users`)
|
||||
yield* Stream.runForEach(
|
||||
View.changes(userCount),
|
||||
|
||||
@@ -239,30 +239,31 @@ const nameLens = lens.pipe(
|
||||
|
||||
### Subscribable
|
||||
|
||||
Lens implements both Effect's `Subscribable` and `Readable`, 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:
|
||||
Effect's `Subscribable` is a read-only, reactive view of a value: it lets you read the current value and observe subsequent changes. Every `Lens` implements both `Subscribable` and `Readable`, which you can use as constraints to allow some parts of your app to only read and subscribe to the Lenses you provide them:
|
||||
```typescript
|
||||
const ref = yield* SubscriptionRef.make<{
|
||||
readonly users: readonly User[]
|
||||
}>({ users: [...] })
|
||||
}>({ users: [] })
|
||||
|
||||
const someFunctionThatShouldOnlyHaveReadonlyAccessToTheState = (
|
||||
usersSub: Subscribable.Subscribable<readonly User[], never, never>
|
||||
) => Effect.gen(function*() {
|
||||
// Do whatever
|
||||
const usersCountSub = Subscribable.map(usersSub, a => a.length)
|
||||
const users = yield* usersSub.get
|
||||
yield* Effect.forkScoped(Stream.runForEach(usersSub.changes, ...))
|
||||
const logUserCount = (users: Subscribable.Subscribable<readonly User[]>) =>
|
||||
Effect.gen(function*() {
|
||||
const userCount = Subscribable.focusArrayLength(users)
|
||||
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))
|
||||
```
|
||||
|
||||
#### Focusing
|
||||
This library re-exports Effect's `Subscribable` module and adds error transforms and recovery (`catchAll`, `catchAllCause`, `orElse`, `orElseSucceed`, and `retry`), plus transforms to narrow the focus of `Subscribable`'s, same as Lenses:
|
||||
Subscribables can be focused in the same way as Lenses. This library re-exports Effect's `Subscribable` module and adds value and error transforms, plus recovery (`catchAll`, `catchAllCause`, `orElse`, `orElseSucceed`, and `retry`):
|
||||
```typescript
|
||||
import { Subscribable } from "effect-lens"
|
||||
|
||||
|
||||
Reference in New Issue
Block a user