1
1
import type { TypedEventTarget } from '@graphql-yoga/typed-event-target' ;
2
- import { Repeater } from '@repeaterjs/repeater' ;
2
+ import { Repeater , type RepeaterBuffer } from '@repeaterjs/repeater' ;
3
3
import { CustomEvent } from '@whatwg-node/events' ;
4
4
5
5
type PubSubPublishArgsByKey = {
@@ -45,9 +45,15 @@ export type PubSub<TPubSubPublishArgsByKey extends PubSubPublishArgsByKey> = {
45
45
* Subscribe to a topic.
46
46
*/
47
47
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
+ } ) ,
51
57
) : Repeater <
52
58
TPubSubPublishArgsByKey [ TKey ] [ 1 ] extends undefined
53
59
? MapToNull < TPubSubPublishArgsByKey [ TKey ] [ 0 ] >
@@ -78,15 +84,23 @@ export const createPubSub = <TPubSubPublishArgsByKey extends PubSubPublishArgsBy
78
84
target . dispatchEvent ( event ) ;
79
85
} ,
80
86
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
+ } ) ,
84
98
) : Repeater <
85
99
TPubSubPublishArgsByKey [ TKey ] [ 1 ] extends undefined
86
100
? TPubSubPublishArgsByKey [ TKey ] [ 0 ]
87
101
: TPubSubPublishArgsByKey [ TKey ] [ 1 ]
88
102
> {
89
- const topic = id === undefined ? routingKey : `${ routingKey } :${ id as number } ` ;
103
+ const topic = args . id === undefined ? args . topic : `${ args . topic } :${ args . id } ` ;
90
104
91
105
return new Repeater ( function subscriptionRepeater ( next , stop ) {
92
106
stop . then ( function subscriptionRepeaterStopHandler ( ) {
@@ -98,7 +112,7 @@ export const createPubSub = <TPubSubPublishArgsByKey extends PubSubPublishArgsBy
98
112
function pubsubEventListener ( event : PubSubEvent < TPubSubPublishArgsByKey , TKey > ) {
99
113
next ( event . detail ) ;
100
114
}
101
- } ) ;
115
+ } , args . buffer ) ;
102
116
} ,
103
117
} ;
104
118
} ;
0 commit comments