Skip to content

Commit e70c28c

Browse files
committed
feat: allow providing a buffer for pubsub subscribe call
1 parent fbe0beb commit e70c28c

File tree

2 files changed

+43
-9
lines changed

2 files changed

+43
-9
lines changed

.changeset/angry-clocks-sniff.md

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
---
2+
'@graphql-yoga/subscription': minor
3+
---
4+
5+
Support providing a `RepeaterBuffer` to the `PubSub.subscribe` method, by using the new object based call signature.
6+
7+
```ts
8+
import { createPubSub } from 'graphql-yoga'
9+
import { SlidingBuffer } from '@repeaterjs/repeater'
10+
11+
const pubSub = createPubSub()
12+
13+
pubSub.subscribe({
14+
topic: "userChanged",
15+
id: "1",
16+
buffer: new SlidingBuffer(1_000)
17+
})
18+
```
19+
20+
Learn more about buffers on the [Repeater.js website](https://repeater.js.org/docs/safety#3-buffering-and-dropping-values).

packages/subscription/src/create-pub-sub.ts

+23-9
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { TypedEventTarget } from '@graphql-yoga/typed-event-target';
2-
import { Repeater } from '@repeaterjs/repeater';
2+
import { Repeater, type RepeaterBuffer } from '@repeaterjs/repeater';
33
import { CustomEvent } from '@whatwg-node/events';
44

55
type 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

Comments
 (0)