@@ -2,9 +2,12 @@ import z from "zod"
22import type { ZodObject } from "zod"
33import { EventEmitter } from "events"
44import { Database , eq } from "@/storage/db"
5+ import { GlobalBus } from "@/bus/global"
56import { Bus as ProjectBus } from "@/bus"
67import { BusEvent } from "@/bus/bus-event"
8+ import { Instance } from "@/project/instance"
79import { EventSequenceTable , EventTable } from "./event.sql"
10+ import { WorkspaceContext } from "@/control-plane/workspace-context"
811import { EventID } from "./schema"
912import { Flag } from "@/flag/flag"
1013
@@ -37,8 +40,6 @@ export namespace SyncEvent {
3740 let frozen = false
3841 let convertEvent : ( type : string , event : Event [ "data" ] ) => Promise < Record < string , unknown > > | Record < string , unknown >
3942
40- const Bus = new EventEmitter < { event : [ { def : Definition ; event : Event } ] } > ( )
41-
4243 export function reset ( ) {
4344 frozen = false
4445 projectors = undefined
@@ -140,11 +141,6 @@ export namespace SyncEvent {
140141 }
141142
142143 Database . effect ( ( ) => {
143- Bus . emit ( "event" , {
144- def,
145- event,
146- } )
147-
148144 if ( options ?. publish ) {
149145 const result = convertEvent ( def . type , event . data )
150146 if ( result instanceof Promise ) {
@@ -154,6 +150,17 @@ export namespace SyncEvent {
154150 } else {
155151 ProjectBus . publish ( { type : def . type , properties : def . schema } , result )
156152 }
153+
154+ GlobalBus . emit ( "event" , {
155+ directory : Instance . directory ,
156+ project : Instance . project . id ,
157+ workspace : WorkspaceContext . workspaceID ,
158+ payload : {
159+ type : "sync" ,
160+ name : versionedType ( def . type , def . version ) ,
161+ ...event ,
162+ } ,
163+ } )
157164 }
158165 } )
159166 } )
@@ -235,31 +242,23 @@ export namespace SyncEvent {
235242 } )
236243 }
237244
238- export function subscribeAll ( handler : ( event : { def : Definition ; event : Event } ) => void ) {
239- Bus . on ( "event" , handler )
240- return ( ) => Bus . off ( "event" , handler )
241- }
242-
243245 export function payloads ( ) {
244- return z
245- . union (
246- registry
247- . entries ( )
248- . map ( ( [ type , def ] ) => {
249- return z
250- . object ( {
251- type : z . literal ( type ) ,
252- aggregate : z . literal ( def . aggregate ) ,
253- data : def . schema ,
254- } )
255- . meta ( {
256- ref : "SyncEvent" + "." + def . type ,
257- } )
246+ return registry
247+ . entries ( )
248+ . map ( ( [ type , def ] ) => {
249+ return z
250+ . object ( {
251+ type : z . literal ( "sync" ) ,
252+ name : z . literal ( type ) ,
253+ id : z . string ( ) ,
254+ seq : z . number ( ) ,
255+ aggregateID : z . literal ( def . aggregate ) ,
256+ data : def . schema ,
257+ } )
258+ . meta ( {
259+ ref : "SyncEvent" + "." + def . type ,
258260 } )
259- . toArray ( ) as any ,
260- )
261- . meta ( {
262- ref : "SyncEvent" ,
263261 } )
262+ . toArray ( )
264263 }
265264}
0 commit comments