forked from effect-app/boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEvents.ts
More file actions
18 lines (17 loc) · 755 Bytes
/
Events.ts
File metadata and controls
18 lines (17 loc) · 755 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
import { storeId } from "@effect-app/infra/Store/Memory"
import { Effect, FiberRef, PubSub, Stream } from "effect-app"
import type { NonEmptyReadonlyArray } from "effect/Array"
import type { ClientEvents } from "./resources/Events.js"
export class Events extends Effect.Service<Events>()("Events", {
accessors: true,
effect: Effect.gen(function*() {
const q = yield* PubSub.unbounded<{ evt: ClientEvents; namespace: string }>()
const svc = {
publish: (...evts: NonEmptyReadonlyArray<ClientEvents>) =>
storeId.pipe(FiberRef.get, Effect.andThen((namespace) => q.offerAll(evts.map((evt) => ({ evt, namespace }))))),
subscribe: q.subscribe,
stream: Stream.fromPubSub(q, { scoped: true })
}
return svc
})
}) {}