From e70c28c0333f20e5b5e4e65ce9053993bc0ae6b1 Mon Sep 17 00:00:00 2001 From: Laurin Quast Date: Thu, 3 Oct 2024 12:20:34 +0200 Subject: [PATCH] feat: allow providing a buffer for pubsub subscribe call --- .changeset/angry-clocks-sniff.md | 20 +++++++++++++ packages/subscription/src/create-pub-sub.ts | 32 +++++++++++++++------ 2 files changed, 43 insertions(+), 9 deletions(-) create mode 100644 .changeset/angry-clocks-sniff.md diff --git a/.changeset/angry-clocks-sniff.md b/.changeset/angry-clocks-sniff.md new file mode 100644 index 0000000000..64b312f91a --- /dev/null +++ b/.changeset/angry-clocks-sniff.md @@ -0,0 +1,20 @@ +--- +'@graphql-yoga/subscription': minor +--- + +Support providing a `RepeaterBuffer` to the `PubSub.subscribe` method, by using the new object based call signature. + +```ts +import { createPubSub } from 'graphql-yoga' +import { SlidingBuffer } from '@repeaterjs/repeater' + +const pubSub = createPubSub() + +pubSub.subscribe({ + topic: "userChanged", + id: "1", + buffer: new SlidingBuffer(1_000) +}) +``` + +Learn more about buffers on the [Repeater.js website](https://repeater.js.org/docs/safety#3-buffering-and-dropping-values). diff --git a/packages/subscription/src/create-pub-sub.ts b/packages/subscription/src/create-pub-sub.ts index 447d093a5a..71f3a8f749 100644 --- a/packages/subscription/src/create-pub-sub.ts +++ b/packages/subscription/src/create-pub-sub.ts @@ -1,5 +1,5 @@ import type { TypedEventTarget } from '@graphql-yoga/typed-event-target'; -import { Repeater } from '@repeaterjs/repeater'; +import { Repeater, type RepeaterBuffer } from '@repeaterjs/repeater'; import { CustomEvent } from '@whatwg-node/events'; type PubSubPublishArgsByKey = { @@ -45,9 +45,15 @@ export type PubSub = { * Subscribe to a topic. */ subscribe>( - ...[routingKey, id]: TPubSubPublishArgsByKey[TKey][1] extends undefined - ? [TKey] - : [TKey, TPubSubPublishArgsByKey[TKey][0]] + args: { + topic: string; + } & (TPubSubPublishArgsByKey[TKey][1] extends undefined + ? { + id?: void; + } + : { + id: TPubSubPublishArgsByKey[TKey][0]; + }), ): Repeater< TPubSubPublishArgsByKey[TKey][1] extends undefined ? MapToNull @@ -78,15 +84,23 @@ export const createPubSub = >( - ...[routingKey, id]: TPubSubPublishArgsByKey[TKey][1] extends undefined - ? [TKey] - : [TKey, TPubSubPublishArgsByKey[TKey][0]] + args: { + /** Topic to subscribe to */ + topic: string; + buffer?: RepeaterBuffer | undefined; + } & (TPubSubPublishArgsByKey[TKey][1] extends undefined + ? { + id?: void; + } + : { + id: TPubSubPublishArgsByKey[TKey][0]; + }), ): Repeater< TPubSubPublishArgsByKey[TKey][1] extends undefined ? TPubSubPublishArgsByKey[TKey][0] : TPubSubPublishArgsByKey[TKey][1] > { - const topic = id === undefined ? routingKey : `${routingKey}:${id as number}`; + const topic = args.id === undefined ? args.topic : `${args.topic}:${args.id}`; return new Repeater(function subscriptionRepeater(next, stop) { stop.then(function subscriptionRepeaterStopHandler() { @@ -98,7 +112,7 @@ export const createPubSub = ) { next(event.detail); } - }); + }, args.buffer); }, }; };