@@ -58,27 +58,6 @@ describe("Lens", () => {
|
|||||||
expect(result[1]).toEqual(Either.left("mapped"))
|
expect(result[1]).toEqual(Either.left("mapped"))
|
||||||
})
|
})
|
||||||
|
|
||||||
test("catchAllRead recovers from read failures", async () => {
|
|
||||||
const result = await Effect.runPromise(
|
|
||||||
Effect.flatMap(
|
|
||||||
SubscriptionRef.make(42),
|
|
||||||
fallback => Lens.get(
|
|
||||||
Lens.catchAllRead(
|
|
||||||
Lens.make<number, "read", never, never, never>({
|
|
||||||
get: Effect.fail("read" as const),
|
|
||||||
changes: Stream.fail("read" as const),
|
|
||||||
commit: () => Effect.void,
|
|
||||||
lock: Effect.succeed(identity),
|
|
||||||
}),
|
|
||||||
() => Lens.fromSubscriptionRef(fallback),
|
|
||||||
),
|
|
||||||
),
|
|
||||||
),
|
|
||||||
)
|
|
||||||
|
|
||||||
expect(result).toBe(42)
|
|
||||||
})
|
|
||||||
|
|
||||||
test("tapErrorRead runs an effect on read failures", async () => {
|
test("tapErrorRead runs an effect on read failures", async () => {
|
||||||
const result = await Effect.runPromise(
|
const result = await Effect.runPromise(
|
||||||
Effect.flatMap(
|
Effect.flatMap(
|
||||||
|
|||||||
@@ -1,46 +0,0 @@
|
|||||||
import { Console, Effect, Stream, SubscriptionRef } from "effect"
|
|
||||||
import { Lens } from "./index.js"
|
|
||||||
|
|
||||||
|
|
||||||
class State extends Effect.Service<State>()("State", {
|
|
||||||
effect: Effect.gen(function*() {
|
|
||||||
const ref = yield* SubscriptionRef.make({
|
|
||||||
users: [
|
|
||||||
{ name: "Adolf" }
|
|
||||||
]
|
|
||||||
})
|
|
||||||
|
|
||||||
return {
|
|
||||||
lens: Lens.fromSubscriptionRef(ref)
|
|
||||||
} as const
|
|
||||||
})
|
|
||||||
}) {}
|
|
||||||
|
|
||||||
|
|
||||||
Effect.gen(function*() {
|
|
||||||
const lens = Lens.unwrap(Effect.andThen(
|
|
||||||
State,
|
|
||||||
state => state.lens,
|
|
||||||
)).pipe(
|
|
||||||
Lens.provideContext(yield* Effect.provide(Effect.context<State>(), State.Default))
|
|
||||||
)
|
|
||||||
|
|
||||||
const adolfNameLens = lens.pipe(
|
|
||||||
Lens.focusObjectOn("users"),
|
|
||||||
Lens.focusArrayAt(0),
|
|
||||||
Lens.focusObjectOn("name"),
|
|
||||||
Lens.catchAllRead(() => Lens.make({
|
|
||||||
get: Effect.succeed("User not found"),
|
|
||||||
changes: Stream.make("User not found"),
|
|
||||||
commit: () => Console.log("Test"),
|
|
||||||
lock: Effect.succeed(Effect.unsafeMakeSemaphore(1).withPermits(1)),
|
|
||||||
})),
|
|
||||||
)
|
|
||||||
|
|
||||||
console.log(yield* Lens.get(adolfNameLens))
|
|
||||||
yield* Lens.set(lens, { users: [] })
|
|
||||||
yield* Lens.set(adolfNameLens, "Himmler")
|
|
||||||
console.log(yield* Lens.get(adolfNameLens))
|
|
||||||
}).pipe(
|
|
||||||
Effect.runSync,
|
|
||||||
)
|
|
||||||
Reference in New Issue
Block a user