@@ -247,7 +247,7 @@ const ref = yield* SubscriptionRef.make<{
|
|||||||
|
|
||||||
const logUserCount = (users: View.View<readonly User[]>) =>
|
const logUserCount = (users: View.View<readonly User[]>) =>
|
||||||
Effect.gen(function*() {
|
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* Console.log(`There are ${yield* View.get(userCount)} users`)
|
||||||
yield* Stream.runForEach(
|
yield* Stream.runForEach(
|
||||||
View.changes(userCount),
|
View.changes(userCount),
|
||||||
|
|||||||
@@ -239,30 +239,31 @@ const nameLens = lens.pipe(
|
|||||||
|
|
||||||
### Subscribable
|
### 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
|
```typescript
|
||||||
const ref = yield* SubscriptionRef.make<{
|
const ref = yield* SubscriptionRef.make<{
|
||||||
readonly users: readonly User[]
|
readonly users: readonly User[]
|
||||||
}>({ users: [...] })
|
}>({ users: [] })
|
||||||
|
|
||||||
const someFunctionThatShouldOnlyHaveReadonlyAccessToTheState = (
|
const logUserCount = (users: Subscribable.Subscribable<readonly User[]>) =>
|
||||||
usersSub: Subscribable.Subscribable<readonly User[], never, never>
|
Effect.gen(function*() {
|
||||||
) => Effect.gen(function*() {
|
const userCount = Subscribable.focusArrayLength(users)
|
||||||
// Do whatever
|
yield* Console.log(`There are ${yield* userCount.get} users`)
|
||||||
const usersCountSub = Subscribable.map(usersSub, a => a.length)
|
yield* Stream.runForEach(
|
||||||
const users = yield* usersSub.get
|
userCount.changes,
|
||||||
yield* Effect.forkScoped(Stream.runForEach(usersSub.changes, ...))
|
count => Console.log(`There are now ${count} users`),
|
||||||
})
|
)
|
||||||
|
})
|
||||||
|
|
||||||
const lens = ref.pipe(
|
const usersLens = ref.pipe(
|
||||||
Lens.fromSubscriptionRef,
|
Lens.fromSubscriptionRef,
|
||||||
Lens.focusObjectOn("users"),
|
Lens.focusObjectOn("users"),
|
||||||
)
|
)
|
||||||
yield* someFunctionThatShouldOnlyHaveReadonlyAccessToTheState(lens)
|
yield* Effect.forkScoped(logUserCount(usersLens))
|
||||||
```
|
```
|
||||||
|
|
||||||
#### Focusing
|
#### 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
|
```typescript
|
||||||
import { Subscribable } from "effect-lens"
|
import { Subscribable } from "effect-lens"
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user