11import type { TypedEventTarget } from '@graphql-yoga/typed-event-target' ;
2- import { Repeater } from '@repeaterjs/repeater' ;
2+ import { Repeater , type RepeaterBuffer } from '@repeaterjs/repeater' ;
33import { CustomEvent } from '@whatwg-node/events' ;
44
55type PubSubPublishArgsByKey = {
@@ -45,9 +45,15 @@ export type PubSub<TPubSubPublishArgsByKey extends PubSubPublishArgsByKey> = {
4545 * Subscribe to a topic.
4646 */
4747 subscribe < TKey extends Extract < keyof TPubSubPublishArgsByKey , string > > (
48- ...[ routingKey , id ] : TPubSubPublishArgsByKey [ TKey ] [ 1 ] extends undefined
49- ? [ TKey ]
50- : [ TKey , TPubSubPublishArgsByKey [ TKey ] [ 0 ] ]
48+ args : {
49+ topic : string ;
50+ } & ( TPubSubPublishArgsByKey [ TKey ] [ 1 ] extends undefined
51+ ? {
52+ id ?: void ;
53+ }
54+ : {
55+ id : TPubSubPublishArgsByKey [ TKey ] [ 0 ] ;
56+ } ) ,
5157 ) : Repeater <
5258 TPubSubPublishArgsByKey [ TKey ] [ 1 ] extends undefined
5359 ? MapToNull < TPubSubPublishArgsByKey [ TKey ] [ 0 ] >
@@ -78,15 +84,23 @@ export const createPubSub = <TPubSubPublishArgsByKey extends PubSubPublishArgsBy
7884 target . dispatchEvent ( event ) ;
7985 } ,
8086 subscribe < TKey extends Extract < keyof TPubSubPublishArgsByKey , string > > (
81- ...[ routingKey , id ] : TPubSubPublishArgsByKey [ TKey ] [ 1 ] extends undefined
82- ? [ TKey ]
83- : [ TKey , TPubSubPublishArgsByKey [ TKey ] [ 0 ] ]
87+ args : {
88+ /** Topic to subscribe to */
89+ topic : string ;
90+ buffer ?: RepeaterBuffer | undefined ;
91+ } & ( TPubSubPublishArgsByKey [ TKey ] [ 1 ] extends undefined
92+ ? {
93+ id ?: void ;
94+ }
95+ : {
96+ id : TPubSubPublishArgsByKey [ TKey ] [ 0 ] ;
97+ } ) ,
8498 ) : Repeater <
8599 TPubSubPublishArgsByKey [ TKey ] [ 1 ] extends undefined
86100 ? TPubSubPublishArgsByKey [ TKey ] [ 0 ]
87101 : TPubSubPublishArgsByKey [ TKey ] [ 1 ]
88102 > {
89- const topic = id === undefined ? routingKey : `${ routingKey } :${ id as number } ` ;
103+ const topic = args . id === undefined ? args . topic : `${ args . topic } :${ args . id } ` ;
90104
91105 return new Repeater ( function subscriptionRepeater ( next , stop ) {
92106 stop . then ( function subscriptionRepeaterStopHandler ( ) {
@@ -98,7 +112,7 @@ export const createPubSub = <TPubSubPublishArgsByKey extends PubSubPublishArgsBy
98112 function pubsubEventListener ( event : PubSubEvent < TPubSubPublishArgsByKey , TKey > ) {
99113 next ( event . detail ) ;
100114 }
101- } ) ;
115+ } , args . buffer ) ;
102116 } ,
103117 } ;
104118} ;
0 commit comments