Created
March 16, 2026 21:42
-
-
Save tim-smart/c0bfcfa60a856cb5a6b8540d132df738 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import { Effect, PubSub, Schema } from "effect" | |
| import { ClusterSchema, Entity, EntityResource, SingleRunner } from "effect/unstable/cluster" | |
| import { Rpc } from "effect/unstable/rpc" | |
| const Counter = Entity.make("Counter", [ | |
| Rpc.make("increment", { | |
| payload: { | |
| amount: Schema.Number | |
| } | |
| }).annotate(ClusterSchema.Persisted, true) | |
| ]) | |
| const CounterLayer = Counter.toLayer(Effect.gen(function*() { | |
| const resource = yield* EntityResource.make({ | |
| acquire: Effect.gen(function*() { | |
| const scope = yield* Effect.scope | |
| const closeScope = yield* EntityResource.CloseScope | |
| return yield* PubSub.unbounded<string>() | |
| }), | |
| idleTimeToLive: "5 minutes" | |
| }) | |
| let count = 0 | |
| return Counter.of({ | |
| increment: Effect.fn(function*({ payload }) { | |
| const pubsub = yield* resource.get | |
| count += payload.amount | |
| }) | |
| }) | |
| })) | |
| const ClusterLayer = SingleRunner.layer({ | |
| runnerStorage: "sql" | |
| }) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment