@graphql-yoga/subscription
Advanced tools
Comparing version 5.0.2-alpha-20240728232210-e814a390 to 5.1.0-alpha-20241003102906-3b853548
@@ -5,2 +5,3 @@ "use strict"; | ||
const repeater_1 = require("@repeaterjs/repeater"); | ||
const events_1 = require("@whatwg-node/events"); | ||
/** | ||
@@ -11,2 +12,22 @@ * Utility for publishing and subscribing to events. | ||
const target = config?.eventTarget ?? new EventTarget(); | ||
function subscribe(...args) { | ||
let topic; | ||
let buffer; | ||
if (typeof args[0] === 'string') { | ||
topic = args[1] === undefined ? args[0] : `${args[0]}:${args[1]}`; | ||
} | ||
else { | ||
topic = args[0].id === undefined ? args[0].topic : `${args[0].topic}:${args[0].id}`; | ||
buffer = args[0].buffer; | ||
} | ||
return new repeater_1.Repeater(function subscriptionRepeater(next, stop) { | ||
stop.then(function subscriptionRepeaterStopHandler() { | ||
target.removeEventListener(topic, pubsubEventListener); | ||
}); | ||
target.addEventListener(topic, pubsubEventListener); | ||
function pubsubEventListener(event) { | ||
next(event.detail); | ||
} | ||
}, buffer); | ||
} | ||
return { | ||
@@ -16,3 +37,3 @@ publish(routingKey, ...args) { | ||
const topic = args[1] === undefined ? routingKey : `${routingKey}:${args[0]}`; | ||
const event = new CustomEvent(topic, { | ||
const event = new events_1.CustomEvent(topic, { | ||
detail: payload, | ||
@@ -22,16 +43,5 @@ }); | ||
}, | ||
subscribe(...[routingKey, id]) { | ||
const topic = id === undefined ? routingKey : `${routingKey}:${id}`; | ||
return new repeater_1.Repeater(function subscriptionRepeater(next, stop) { | ||
stop.then(function subscriptionRepeaterStopHandler() { | ||
target.removeEventListener(topic, pubsubEventListener); | ||
}); | ||
target.addEventListener(topic, pubsubEventListener); | ||
function pubsubEventListener(event) { | ||
next(event.detail); | ||
} | ||
}); | ||
}, | ||
subscribe, | ||
}; | ||
}; | ||
exports.createPubSub = createPubSub; |
import { Repeater } from '@repeaterjs/repeater'; | ||
import { CustomEvent } from '@whatwg-node/events'; | ||
/** | ||
@@ -7,2 +8,22 @@ * Utility for publishing and subscribing to events. | ||
const target = config?.eventTarget ?? new EventTarget(); | ||
function subscribe(...args) { | ||
let topic; | ||
let buffer; | ||
if (typeof args[0] === 'string') { | ||
topic = args[1] === undefined ? args[0] : `${args[0]}:${args[1]}`; | ||
} | ||
else { | ||
topic = args[0].id === undefined ? args[0].topic : `${args[0].topic}:${args[0].id}`; | ||
buffer = args[0].buffer; | ||
} | ||
return new Repeater(function subscriptionRepeater(next, stop) { | ||
stop.then(function subscriptionRepeaterStopHandler() { | ||
target.removeEventListener(topic, pubsubEventListener); | ||
}); | ||
target.addEventListener(topic, pubsubEventListener); | ||
function pubsubEventListener(event) { | ||
next(event.detail); | ||
} | ||
}, buffer); | ||
} | ||
return { | ||
@@ -17,15 +38,4 @@ publish(routingKey, ...args) { | ||
}, | ||
subscribe(...[routingKey, id]) { | ||
const topic = id === undefined ? routingKey : `${routingKey}:${id}`; | ||
return new Repeater(function subscriptionRepeater(next, stop) { | ||
stop.then(function subscriptionRepeaterStopHandler() { | ||
target.removeEventListener(topic, pubsubEventListener); | ||
}); | ||
target.addEventListener(topic, pubsubEventListener); | ||
function pubsubEventListener(event) { | ||
next(event.detail); | ||
} | ||
}); | ||
}, | ||
subscribe, | ||
}; | ||
}; |
{ | ||
"name": "@graphql-yoga/subscription", | ||
"version": "5.0.2-alpha-20240728232210-e814a390", | ||
"version": "5.1.0-alpha-20241003102906-3b853548", | ||
"description": "", | ||
@@ -8,2 +8,3 @@ "sideEffects": true, | ||
"@repeaterjs/repeater": "^3.0.4", | ||
"@whatwg-node/events": "^0.1.0", | ||
"tslib": "^2.5.2", | ||
@@ -10,0 +11,0 @@ "@graphql-yoga/typed-event-target": "^3.0.0" |
import type { TypedEventTarget } from '@graphql-yoga/typed-event-target'; | ||
import { Repeater } from '@repeaterjs/repeater'; | ||
import { Repeater, type RepeaterBuffer } from '@repeaterjs/repeater'; | ||
type PubSubPublishArgsByKey = { | ||
@@ -26,3 +26,12 @@ [key: string]: [] | [any] | [number | string, any]; | ||
*/ | ||
subscribe<TKey extends Extract<keyof TPubSubPublishArgsByKey, string>>(...[routingKey, id]: TPubSubPublishArgsByKey[TKey][1] extends undefined ? [TKey] : [TKey, TPubSubPublishArgsByKey[TKey][0]]): Repeater<TPubSubPublishArgsByKey[TKey][1] extends undefined ? MapToNull<TPubSubPublishArgsByKey[TKey][0]> : MapToNull<TPubSubPublishArgsByKey[TKey][1]>>; | ||
subscribe<TKey extends Extract<keyof TPubSubPublishArgsByKey, string>>(...args: (TPubSubPublishArgsByKey[TKey][1] extends undefined ? [key: TKey] : [key: TKey, id: TPubSubPublishArgsByKey[TKey][0]]) | [ | ||
args: { | ||
topic: string; | ||
buffer?: RepeaterBuffer | undefined; | ||
} & (TPubSubPublishArgsByKey[TKey][1] extends undefined ? { | ||
id?: void; | ||
} : { | ||
id: TPubSubPublishArgsByKey[TKey][0]; | ||
}) | ||
]): Repeater<TPubSubPublishArgsByKey[TKey][1] extends undefined ? MapToNull<TPubSubPublishArgsByKey[TKey][0]> : MapToNull<TPubSubPublishArgsByKey[TKey][1]>>; | ||
}; | ||
@@ -29,0 +38,0 @@ /** |
Sorry, the diff of this file is not supported yet
27235
320
4
+ Added@whatwg-node/events@^0.1.0
+ Added@whatwg-node/events@0.1.2(transitive)