|
|
@@ -2,8 +2,8 @@ import z from "zod"
|
|
|
import type { ZodObject } from "zod"
|
|
|
import { EventEmitter } from "events"
|
|
|
import { Context, Effect, Layer } from "effect"
|
|
|
-import { makeRuntime } from "@/effect/run-service"
|
|
|
import { InstanceState } from "@/effect/instance-state"
|
|
|
+import { makeRuntime } from "@/effect/run-service"
|
|
|
import { Database, eq } from "@/storage/db"
|
|
|
import { Bus as ProjectBus } from "@/bus"
|
|
|
import { BusEvent } from "@/bus/bus-event"
|
|
|
@@ -42,6 +42,9 @@ export namespace SyncEvent {
|
|
|
export const registry = new Map<string, Definition>()
|
|
|
const versions = new Map<string, number>()
|
|
|
let frozen = false
|
|
|
+ let projectors: Map<Definition, ProjectorFunc> | undefined
|
|
|
+ let convert: Convert = (_, data) => data as Record<string, unknown>
|
|
|
+ const bus = new EventEmitter<{ event: [Payload] }>()
|
|
|
|
|
|
export interface Interface {
|
|
|
readonly reset: () => Effect.Effect<void>
|
|
|
@@ -62,10 +65,6 @@ export namespace SyncEvent {
|
|
|
export const layer = Layer.effect(
|
|
|
Service,
|
|
|
Effect.gen(function* () {
|
|
|
- let projectors: Map<Definition, ProjectorFunc> | undefined
|
|
|
- let convert: Convert = (_, data) => data as Record<string, unknown>
|
|
|
- const bus = new EventEmitter<{ event: [Payload] }>()
|
|
|
-
|
|
|
const reset = Effect.fn("SyncEvent.reset")(() =>
|
|
|
Effect.sync(() => {
|
|
|
frozen = false
|
|
|
@@ -94,13 +93,7 @@ export namespace SyncEvent {
|
|
|
}),
|
|
|
)
|
|
|
|
|
|
- const process = Effect.fn("SyncEvent.process")(function* <Def extends Definition>(
|
|
|
- def: Def,
|
|
|
- event: Event<Def>,
|
|
|
- options: { publish: boolean },
|
|
|
- ) {
|
|
|
- const ctx = yield* InstanceState.context
|
|
|
-
|
|
|
+ function process<Def extends Definition>(def: Def, event: Event<Def>, options: { publish: boolean }) {
|
|
|
if (projectors == null) {
|
|
|
throw new Error("No projectors available. Call `SyncEvent.init` to install projectors")
|
|
|
}
|
|
|
@@ -110,57 +103,55 @@ export namespace SyncEvent {
|
|
|
throw new Error(`Projector not found for event: ${def.type}`)
|
|
|
}
|
|
|
|
|
|
- yield* Effect.sync(() => {
|
|
|
- // idempotent: need to ignore any events already logged
|
|
|
- Database.transaction((tx) => {
|
|
|
- projector(tx, event.data)
|
|
|
-
|
|
|
- if (Flag.OPENCODE_EXPERIMENTAL_WORKSPACES) {
|
|
|
- tx.insert(EventSequenceTable)
|
|
|
- .values({
|
|
|
- aggregate_id: event.aggregateID,
|
|
|
- seq: event.seq,
|
|
|
- })
|
|
|
- .onConflictDoUpdate({
|
|
|
- target: EventSequenceTable.aggregate_id,
|
|
|
- set: { seq: event.seq },
|
|
|
- })
|
|
|
- .run()
|
|
|
- tx.insert(EventTable)
|
|
|
- .values({
|
|
|
- id: event.id,
|
|
|
- seq: event.seq,
|
|
|
- aggregate_id: event.aggregateID,
|
|
|
- type: versionedType(def.type, def.version),
|
|
|
- data: event.data as Record<string, unknown>,
|
|
|
- })
|
|
|
- .run()
|
|
|
- }
|
|
|
-
|
|
|
- Database.effect(() => {
|
|
|
- Instance.restore(ctx, () => {
|
|
|
- bus.emit("event", { def, event })
|
|
|
-
|
|
|
- if (!options.publish) return
|
|
|
-
|
|
|
- const result = convert(def.type, event.data)
|
|
|
- if (result instanceof Promise) {
|
|
|
- void result.then((data) => {
|
|
|
- Instance.restore(ctx, () => {
|
|
|
- void ProjectBus.publish({ type: def.type, properties: def.schema }, data)
|
|
|
- })
|
|
|
- })
|
|
|
- return
|
|
|
- }
|
|
|
+ // idempotent: need to ignore any events already logged
|
|
|
+ Database.transaction((tx) => {
|
|
|
+ projector(tx, event.data)
|
|
|
|
|
|
- void ProjectBus.publish({ type: def.type, properties: def.schema }, result)
|
|
|
+ if (Flag.OPENCODE_EXPERIMENTAL_WORKSPACES) {
|
|
|
+ tx.insert(EventSequenceTable)
|
|
|
+ .values({
|
|
|
+ aggregate_id: event.aggregateID,
|
|
|
+ seq: event.seq,
|
|
|
})
|
|
|
- })
|
|
|
- })
|
|
|
+ .onConflictDoUpdate({
|
|
|
+ target: EventSequenceTable.aggregate_id,
|
|
|
+ set: { seq: event.seq },
|
|
|
+ })
|
|
|
+ .run()
|
|
|
+ tx.insert(EventTable)
|
|
|
+ .values({
|
|
|
+ id: event.id,
|
|
|
+ seq: event.seq,
|
|
|
+ aggregate_id: event.aggregateID,
|
|
|
+ type: versionedType(def.type, def.version),
|
|
|
+ data: event.data as Record<string, unknown>,
|
|
|
+ })
|
|
|
+ .run()
|
|
|
+ }
|
|
|
+
|
|
|
+ Database.effect(
|
|
|
+ InstanceState.bind(() => {
|
|
|
+ bus.emit("event", { def, event })
|
|
|
+
|
|
|
+ if (!options.publish) return
|
|
|
+
|
|
|
+ const result = convert(def.type, event.data)
|
|
|
+ if (result instanceof Promise) {
|
|
|
+ void result.then(
|
|
|
+ InstanceState.bind((data) => {
|
|
|
+ void ProjectBus.publish({ type: def.type, properties: def.schema }, data)
|
|
|
+ }),
|
|
|
+ )
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ void ProjectBus.publish({ type: def.type, properties: def.schema }, result)
|
|
|
+ }),
|
|
|
+ )
|
|
|
})
|
|
|
- })
|
|
|
+ }
|
|
|
|
|
|
- const replay = Effect.fn("SyncEvent.replay")(function* (event: SerializedEvent, options?: { publish: boolean }) {
|
|
|
+ function replay(event: SerializedEvent, options?: { publish: boolean }) {
|
|
|
const def = registry.get(event.type)
|
|
|
if (!def) {
|
|
|
throw new Error(`Unknown event type: ${event.type}`)
|
|
|
@@ -184,14 +175,10 @@ export namespace SyncEvent {
|
|
|
)
|
|
|
}
|
|
|
|
|
|
- yield* process(def, event, { publish: !!options?.publish })
|
|
|
- })
|
|
|
+ process(def, event, { publish: !!options?.publish })
|
|
|
+ }
|
|
|
|
|
|
- const run = Effect.fn("SyncEvent.run")(function* <Def extends Definition>(
|
|
|
- def: Def,
|
|
|
- data: Event<Def>["data"],
|
|
|
- options?: { publish?: boolean },
|
|
|
- ) {
|
|
|
+ function run<Def extends Definition>(def: Def, data: Event<Def>["data"], options?: { publish?: boolean }) {
|
|
|
const agg = (data as Record<string, string>)[def.aggregate]
|
|
|
if (agg == null) {
|
|
|
throw new Error(`SyncEvent.run: "${def.aggregate}" required but not found: ${JSON.stringify(data)}`)
|
|
|
@@ -203,45 +190,39 @@ export namespace SyncEvent {
|
|
|
|
|
|
const publish = options?.publish ?? true
|
|
|
|
|
|
- yield* Effect.sync(() => {
|
|
|
- // Note that this is an "immediate" transaction which is critical.
|
|
|
- // We need to make sure we can safely read and write with nothing
|
|
|
- // else changing the data from under us
|
|
|
- Database.transaction(
|
|
|
- (tx) => {
|
|
|
- const id = EventID.ascending()
|
|
|
- const row = tx
|
|
|
- .select({ seq: EventSequenceTable.seq })
|
|
|
- .from(EventSequenceTable)
|
|
|
- .where(eq(EventSequenceTable.aggregate_id, agg))
|
|
|
- .get()
|
|
|
- const seq = row?.seq != null ? row.seq + 1 : 0
|
|
|
-
|
|
|
- const event = { id, seq, aggregateID: agg, data }
|
|
|
- Effect.runSync(process(def, event, { publish }))
|
|
|
- },
|
|
|
- {
|
|
|
- behavior: "immediate",
|
|
|
- },
|
|
|
- )
|
|
|
- })
|
|
|
- })
|
|
|
+ // Note that this is an "immediate" transaction which is critical.
|
|
|
+ // We need to make sure we can safely read and write with nothing
|
|
|
+ // else changing the data from under us
|
|
|
+ Database.transaction(
|
|
|
+ (tx) => {
|
|
|
+ const id = EventID.ascending()
|
|
|
+ const row = tx
|
|
|
+ .select({ seq: EventSequenceTable.seq })
|
|
|
+ .from(EventSequenceTable)
|
|
|
+ .where(eq(EventSequenceTable.aggregate_id, agg))
|
|
|
+ .get()
|
|
|
+ const seq = row?.seq != null ? row.seq + 1 : 0
|
|
|
+
|
|
|
+ const event = { id, seq, aggregateID: agg, data }
|
|
|
+ process(def, event, { publish })
|
|
|
+ },
|
|
|
+ {
|
|
|
+ behavior: "immediate",
|
|
|
+ },
|
|
|
+ )
|
|
|
+ }
|
|
|
|
|
|
- const remove = Effect.fn("SyncEvent.remove")((aggregateID: string) =>
|
|
|
- Effect.sync(() => {
|
|
|
- Database.transaction((tx) => {
|
|
|
- tx.delete(EventSequenceTable).where(eq(EventSequenceTable.aggregate_id, aggregateID)).run()
|
|
|
- tx.delete(EventTable).where(eq(EventTable.aggregate_id, aggregateID)).run()
|
|
|
- })
|
|
|
- }),
|
|
|
- )
|
|
|
+ function remove(aggregateID: string) {
|
|
|
+ Database.transaction((tx) => {
|
|
|
+ tx.delete(EventSequenceTable).where(eq(EventSequenceTable.aggregate_id, aggregateID)).run()
|
|
|
+ tx.delete(EventTable).where(eq(EventTable.aggregate_id, aggregateID)).run()
|
|
|
+ })
|
|
|
+ }
|
|
|
|
|
|
- const subscribeAll = Effect.fn("SyncEvent.subscribeAll")((handler: (event: Payload) => void) =>
|
|
|
- Effect.sync(() => {
|
|
|
- bus.on("event", handler)
|
|
|
- return () => bus.off("event", handler)
|
|
|
- }),
|
|
|
- )
|
|
|
+ function subscribeAll(handler: (event: Payload) => void) {
|
|
|
+ bus.on("event", handler)
|
|
|
+ return () => bus.off("event", handler)
|
|
|
+ }
|
|
|
|
|
|
function payloads() {
|
|
|
return z
|
|
|
@@ -266,7 +247,25 @@ export namespace SyncEvent {
|
|
|
})
|
|
|
}
|
|
|
|
|
|
- return Service.of({ reset, init, replay, run, remove, subscribeAll, payloads })
|
|
|
+ return Service.of({
|
|
|
+ reset,
|
|
|
+ init,
|
|
|
+ replay: (event, options) =>
|
|
|
+ Effect.gen(function* () {
|
|
|
+ const ctx = yield* InstanceState.context
|
|
|
+ return yield* Effect.sync(() => Instance.restore(ctx, () => replay(event, options)))
|
|
|
+ }),
|
|
|
+ run: (def, data, options) =>
|
|
|
+ options?.publish === false
|
|
|
+ ? Effect.sync(() => run(def, data, options))
|
|
|
+ : Effect.gen(function* () {
|
|
|
+ const ctx = yield* InstanceState.context
|
|
|
+ return yield* Effect.sync(() => Instance.restore(ctx, () => run(def, data, options)))
|
|
|
+ }),
|
|
|
+ remove: (aggregateID) => Effect.sync(() => remove(aggregateID)),
|
|
|
+ subscribeAll: (handler) => Effect.sync(() => subscribeAll(handler)),
|
|
|
+ payloads,
|
|
|
+ })
|
|
|
}),
|
|
|
)
|
|
|
|