@@ -159,6 +159,8 @@ export interface EventSinkV2Shape {
159159 readonly stream : ( input ?: {
160160 readonly threadId ?: ThreadId ;
161161 readonly afterSequence ?: number ;
162+ /** Filter before queuing live events so a busy worker retains only the events it handles. */
163+ readonly eventType ?: OrchestrationV2DomainEvent [ "type" ] ;
162164 /** Bound RPC subscribers; internal workers must not drop their subscription under load. */
163165 readonly bounded ?: boolean ;
164166 } ) => Stream . Stream < OrchestrationV2StoredEvent , EventSinkV2Error > ;
@@ -196,6 +198,20 @@ const baseLayer: Layer.Layer<
196198 const projectionStore = yield * ProjectionStoreV2 ;
197199 const turnItemPositions = yield * TurnItemPositionStoreV2 ;
198200 const liveEvents = yield * PubSub . unbounded < OrchestrationV2StoredEvent > ( ) ;
201+ const liveEventsByType = new Map <
202+ OrchestrationV2DomainEvent [ "type" ] ,
203+ PubSub . PubSub < OrchestrationV2StoredEvent >
204+ > ( ) ;
205+ const publishLiveEvents = ( events : ReadonlyArray < OrchestrationV2StoredEvent > ) =>
206+ Effect . gen ( function * ( ) {
207+ yield * PubSub . publishAll ( liveEvents , events ) ;
208+ for ( const [ type , pubsub ] of liveEventsByType ) {
209+ yield * PubSub . publishAll (
210+ pubsub ,
211+ events . filter ( ( stored ) => stored . event . type === type ) ,
212+ ) ;
213+ }
214+ } ) ;
199215
200216 // A user can answer after terminal normalization reads the pending request.
201217 // Recheck inside the write transaction so stale cleanup cannot erase answers.
@@ -324,7 +340,7 @@ const baseLayer: Layer.Layer<
324340 yield * effectOutbox . notifyAvailable ( input . effects . length ) ;
325341 }
326342 yield * eventStore . publishCommitted ( storedEvents ) ;
327- yield * PubSub . publishAll ( liveEvents , storedEvents ) ;
343+ yield * publishLiveEvents ( storedEvents ) ;
328344 return storedEvents ;
329345 } ) ;
330346
@@ -378,7 +394,7 @@ const baseLayer: Layer.Layer<
378394 ) ;
379395 if ( result . committed ) {
380396 yield * eventStore . publishCommitted ( result . storedEvents ) ;
381- yield * PubSub . publishAll ( liveEvents , result . storedEvents ) ;
397+ yield * publishLiveEvents ( result . storedEvents ) ;
382398 }
383399 return result ;
384400 } ,
@@ -439,7 +455,7 @@ const baseLayer: Layer.Layer<
439455 ) ;
440456 if ( result . committed ) {
441457 yield * eventStore . publishCommitted ( result . storedEvents ) ;
442- yield * PubSub . publishAll ( liveEvents , result . storedEvents ) ;
458+ yield * publishLiveEvents ( result . storedEvents ) ;
443459 }
444460 return result ;
445461 } ) ;
@@ -517,7 +533,7 @@ const baseLayer: Layer.Layer<
517533 }
518534 if ( result . committed ) {
519535 yield * eventStore . publishCommitted ( result . storedEvents ) ;
520- yield * PubSub . publishAll ( liveEvents , result . storedEvents ) ;
536+ yield * publishLiveEvents ( result . storedEvents ) ;
521537 }
522538 return {
523539 receipt : result . receipt ,
@@ -556,6 +572,7 @@ const baseLayer: Layer.Layer<
556572 readonly afterSequence : number ;
557573 readonly throughSequence : number ;
558574 readonly threadId ?: ThreadId ;
575+ readonly eventType ?: OrchestrationV2DomainEvent [ "type" ] ;
559576 } ) : Stream . Stream < OrchestrationV2StoredEvent , unknown > => {
560577 const pageSize = 256 ;
561578 const loop = ( afterSequence : number ) : Stream . Stream < OrchestrationV2StoredEvent , unknown > =>
@@ -565,6 +582,7 @@ const baseLayer: Layer.Layer<
565582 afterSequence,
566583 throughSequence : input . throughSequence ,
567584 ...( input . threadId === undefined ? { } : { threadId : input . threadId } ) ,
585+ ...( input . eventType === undefined ? { } : { eventType : input . eventType } ) ,
568586 limit : pageSize ,
569587 } )
570588 . pipe (
@@ -587,31 +605,44 @@ const baseLayer: Layer.Layer<
587605
588606 const stream = ( input ?: Parameters < EventSinkV2Shape [ "stream" ] > [ 0 ] ) => {
589607 const afterSequence = input ?. afterSequence ?? 0 ;
590- const matchesThread = ( stored : OrchestrationV2StoredEvent ) =>
591- input ?. threadId === undefined || stored . event . threadId === input . threadId ;
608+ const matches = ( stored : OrchestrationV2StoredEvent ) =>
609+ ( input ?. threadId === undefined || stored . event . threadId === input . threadId ) &&
610+ ( input ?. eventType === undefined || stored . event . type === input . eventType ) ;
592611 const replay = ( throughSequence : number ) =>
593612 catchUp ( {
594613 afterSequence,
595614 throughSequence,
596615 ...( input ?. threadId === undefined ? { } : { threadId : input . threadId } ) ,
597- } ) ;
598- if ( input ?. bounded === true ) {
599- return replayAndBufferProjectedLiveEvents ( {
600- subscribe : PubSub . subscribe ( liveEvents ) ,
601- latestSequence : eventStore . latestSequence ( ) ,
602- afterSequence,
603- filter : matchesThread ,
604- replay,
605- project : ( stored ) => ( { ...stored , event : projectDomainEventForWire ( stored . event ) } ) ,
606- } ) ;
607- }
616+ ...( input ?. eventType === undefined ? { } : { eventType : input . eventType } ) ,
617+ } ) . pipe ( Stream . filter ( matches ) ) ;
608618 return Stream . unwrap (
609619 Effect . gen ( function * ( ) {
610- const subscription = yield * PubSub . subscribe ( liveEvents ) ;
620+ let pubsub = liveEvents ;
621+ if ( input ?. eventType !== undefined ) {
622+ const existing = liveEventsByType . get ( input . eventType ) ;
623+ if ( existing !== undefined ) {
624+ pubsub = existing ;
625+ } else {
626+ const created = yield * PubSub . unbounded < OrchestrationV2StoredEvent > ( ) ;
627+ pubsub = liveEventsByType . get ( input . eventType ) ?? created ;
628+ liveEventsByType . set ( input . eventType , pubsub ) ;
629+ }
630+ }
631+ if ( input ?. bounded === true ) {
632+ return replayAndBufferProjectedLiveEvents ( {
633+ subscribe : PubSub . subscribe ( pubsub ) ,
634+ latestSequence : eventStore . latestSequence ( ) ,
635+ afterSequence,
636+ filter : matches ,
637+ replay,
638+ project : ( stored ) => ( { ...stored , event : projectDomainEventForWire ( stored . event ) } ) ,
639+ } ) ;
640+ }
641+ const subscription = yield * PubSub . subscribe ( pubsub ) ;
611642 const highWater = yield * eventStore . latestSequence ( ) ;
612643 const live = Stream . fromSubscription ( subscription ) . pipe (
613644 Stream . filter ( ( stored ) => stored . sequence > Math . max ( highWater , afterSequence ) ) ,
614- Stream . filter ( matchesThread ) ,
645+ Stream . filter ( matches ) ,
615646 ) ;
616647 return Stream . concat ( replay ( highWater ) , live ) ;
617648 } ) ,
0 commit comments