Cluster.Singleton reference
Singleton
Section titled “Singleton”Source:
packages/effect-cluster/src/Singleton.ts
A Singleton on an Effect Cluster: one fiber, running on exactly one of its pool. If that runner stops, another starts it.
Declare it by name, with its implementation: an Effect that resolves
bindings once per runner and returns the fiber, which only the runner
that owns the singleton runs. Providing a pool Layer to the
implementation is what deploys it. Or declare it bare and implement it
with .make(props, impl), as a Layer.
Singleton: Declaring a singleton
Section titled “Singleton: Declaring a singleton”A loop that runs once across the cluster
export default class Reaper extends Cluster.Singleton<Reaper>()( "Reaper", { main: import.meta.url }, Effect.gen(function* () { const counters = yield* Counter; return Effect.gen(function* () { const counter = yield* counters.getByName("reaped"); yield* counter.Increment({ amount: 1 }); }).pipe(Effect.repeat(Schedule.spaced("1 minute")), Effect.orDie); }).pipe(Effect.provide(Default)),) {}Declared bare, and implemented as a Layer
export class Reaper extends Cluster.Singleton<Reaper>()("Reaper") {}
// src/ReaperLive.tsexport default Reaper.make( { main: import.meta.url }, Effect.gen(function* () { const counters = yield* Counter; return Effect.gen(function* () { const counter = yield* counters.getByName("reaped"); yield* counter.Increment({ amount: 1 }); }).pipe(Effect.repeat(Schedule.spaced("1 minute")), Effect.orDie); }).pipe(Effect.provide(Default)),);