Skip to content

Instantly share code, notes, and snippets.

@tim-smart
Created March 16, 2026 21:42
Show Gist options
  • Select an option

  • Save tim-smart/c0bfcfa60a856cb5a6b8540d132df738 to your computer and use it in GitHub Desktop.

Select an option

Save tim-smart/c0bfcfa60a856cb5a6b8540d132df738 to your computer and use it in GitHub Desktop.
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