@graphql-yoga/redis-event-target 0.0.1-canary-23a9ff8.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md ADDED
@@ -0,0 +1,5 @@
1
+ # @graphql-yoga/redis-event-target
2
+
3
+ Do distributed GraphQL subscriptions over Redis.
4
+
5
+ [Learn more about GraphQL Subscriptions.](https://www.graphql-yoga.com/docs/features/subscriptions)
package/cjs/index.js ADDED
@@ -0,0 +1,63 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.createRedisEventTarget = void 0;
4
+ const typed_event_target_1 = require("@graphql-yoga/typed-event-target");
5
+ function createRedisEventTarget(args) {
6
+ const { publishClient, subscribeClient } = args;
7
+ const eventAPI = (0, typed_event_target_1.resolveGlobalConfig)(args.event);
8
+ const callbacksForTopic = new Map();
9
+ function onMessage(channel, message) {
10
+ const callbacks = callbacksForTopic.get(channel);
11
+ if (callbacks === undefined) {
12
+ return;
13
+ }
14
+ const event = new eventAPI.Event(channel);
15
+ event.data = message === '' ? undefined : JSON.parse(message);
16
+ for (const callback of callbacks) {
17
+ callback(event);
18
+ }
19
+ }
20
+ subscribeClient.on('message', onMessage);
21
+ function addCallback(topic, callback) {
22
+ let callbacks = callbacksForTopic.get(topic);
23
+ if (callbacks === undefined) {
24
+ callbacks = new Set();
25
+ callbacksForTopic.set(topic, callbacks);
26
+ subscribeClient.subscribe(topic);
27
+ }
28
+ callbacks.add(callback);
29
+ }
30
+ function removeCallback(topic, callback) {
31
+ let callbacks = callbacksForTopic.get(topic);
32
+ if (callbacks === undefined) {
33
+ return;
34
+ }
35
+ callbacks.delete(callback);
36
+ if (callbacks.size > 0) {
37
+ return;
38
+ }
39
+ callbacksForTopic.delete(topic);
40
+ subscribeClient.unsubscribe(topic);
41
+ }
42
+ return {
43
+ addEventListener(topic, callbackOrOptions) {
44
+ const callback = 'handleEvent' in callbackOrOptions
45
+ ? callbackOrOptions.handleEvent
46
+ : callbackOrOptions;
47
+ addCallback(topic, callback);
48
+ },
49
+ dispatchEvent(event) {
50
+ publishClient.publish(event.type, event.data === undefined
51
+ ? ''
52
+ : JSON.stringify(event.data));
53
+ return true;
54
+ },
55
+ removeEventListener(topic, callbackOrOptions) {
56
+ const callback = 'handleEvent' in callbackOrOptions
57
+ ? callbackOrOptions.handleEvent
58
+ : callbackOrOptions;
59
+ removeCallback(topic, callback);
60
+ },
61
+ };
62
+ }
63
+ exports.createRedisEventTarget = createRedisEventTarget;
@@ -0,0 +1 @@
1
+ {"type":"commonjs"}
package/esm/index.js ADDED
@@ -0,0 +1,59 @@
1
+ import { resolveGlobalConfig, } from '@graphql-yoga/typed-event-target';
2
+ export function createRedisEventTarget(args) {
3
+ const { publishClient, subscribeClient } = args;
4
+ const eventAPI = resolveGlobalConfig(args.event);
5
+ const callbacksForTopic = new Map();
6
+ function onMessage(channel, message) {
7
+ const callbacks = callbacksForTopic.get(channel);
8
+ if (callbacks === undefined) {
9
+ return;
10
+ }
11
+ const event = new eventAPI.Event(channel);
12
+ event.data = message === '' ? undefined : JSON.parse(message);
13
+ for (const callback of callbacks) {
14
+ callback(event);
15
+ }
16
+ }
17
+ subscribeClient.on('message', onMessage);
18
+ function addCallback(topic, callback) {
19
+ let callbacks = callbacksForTopic.get(topic);
20
+ if (callbacks === undefined) {
21
+ callbacks = new Set();
22
+ callbacksForTopic.set(topic, callbacks);
23
+ subscribeClient.subscribe(topic);
24
+ }
25
+ callbacks.add(callback);
26
+ }
27
+ function removeCallback(topic, callback) {
28
+ let callbacks = callbacksForTopic.get(topic);
29
+ if (callbacks === undefined) {
30
+ return;
31
+ }
32
+ callbacks.delete(callback);
33
+ if (callbacks.size > 0) {
34
+ return;
35
+ }
36
+ callbacksForTopic.delete(topic);
37
+ subscribeClient.unsubscribe(topic);
38
+ }
39
+ return {
40
+ addEventListener(topic, callbackOrOptions) {
41
+ const callback = 'handleEvent' in callbackOrOptions
42
+ ? callbackOrOptions.handleEvent
43
+ : callbackOrOptions;
44
+ addCallback(topic, callback);
45
+ },
46
+ dispatchEvent(event) {
47
+ publishClient.publish(event.type, event.data === undefined
48
+ ? ''
49
+ : JSON.stringify(event.data));
50
+ return true;
51
+ },
52
+ removeEventListener(topic, callbackOrOptions) {
53
+ const callback = 'handleEvent' in callbackOrOptions
54
+ ? callbackOrOptions.handleEvent
55
+ : callbackOrOptions;
56
+ removeCallback(topic, callback);
57
+ },
58
+ };
59
+ }
package/package.json ADDED
@@ -0,0 +1,48 @@
1
+ {
2
+ "name": "@graphql-yoga/redis-event-target",
3
+ "version": "0.0.1-canary-23a9ff8.0",
4
+ "description": "",
5
+ "peerDependencies": {
6
+ "ioredis": "^5.0.6"
7
+ },
8
+ "dependencies": {
9
+ "@graphql-yoga/typed-event-target": "^0.0.0"
10
+ },
11
+ "repository": {
12
+ "type": "git",
13
+ "url": "https://github.com/dotansimha/graphql-yoga.git",
14
+ "directory": "packages/event-target/redis-event-target"
15
+ },
16
+ "keywords": [
17
+ "pubsub",
18
+ "graphql",
19
+ "event",
20
+ "subscription"
21
+ ],
22
+ "author": "Laurin Quast <laurinquast@googlemail.com>",
23
+ "license": "MIT",
24
+ "main": "cjs/index.js",
25
+ "module": "esm/index.js",
26
+ "typings": "typings/index.d.ts",
27
+ "typescript": {
28
+ "definition": "typings/index.d.ts"
29
+ },
30
+ "type": "module",
31
+ "exports": {
32
+ ".": {
33
+ "require": {
34
+ "types": "./typings/index.d.ts",
35
+ "default": "./cjs/index.js"
36
+ },
37
+ "import": {
38
+ "types": "./typings/index.d.ts",
39
+ "default": "./esm/index.js"
40
+ },
41
+ "default": {
42
+ "types": "./typings/index.d.ts",
43
+ "default": "./esm/index.js"
44
+ }
45
+ },
46
+ "./package.json": "./package.json"
47
+ }
48
+ }
@@ -0,0 +1,12 @@
1
+ import { type TypedEventTarget, type EventAPI } from '@graphql-yoga/typed-event-target';
2
+ import type { Redis, Cluster } from 'ioredis';
3
+ export declare type CreateRedisEventTargetArgs = {
4
+ publishClient: Redis | Cluster;
5
+ subscribeClient: Redis | Cluster;
6
+ /**
7
+ * Event and EventTarget implementation.
8
+ * Providing this is mandatory for a Node.js versions below 16.
9
+ */
10
+ event?: EventAPI;
11
+ };
12
+ export declare function createRedisEventTarget<TEvent extends Event>(args: CreateRedisEventTargetArgs): TypedEventTarget<TEvent>;