@graphql-hive/pubsub 0.0.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/dist/index.cjs ADDED
@@ -0,0 +1,81 @@
1
+ 'use strict';
2
+
3
+ var repeater = require('@repeaterjs/repeater');
4
+ var disposablestack = require('@whatwg-node/disposablestack');
5
+
6
+ class PubSub {
7
+ #topicListeners = /* @__PURE__ */ new Map();
8
+ #subIdTopic = /* @__PURE__ */ new Map();
9
+ #subIdListeners = /* @__PURE__ */ new Map();
10
+ /** @deprecated Please use {@link subscribedTopics} instead. */
11
+ getEventNames() {
12
+ return this.#topicListeners.keys();
13
+ }
14
+ subscribedTopics() {
15
+ return this.#topicListeners.keys();
16
+ }
17
+ publish(topic, data) {
18
+ const listeners = this.#topicListeners.get(topic);
19
+ if (listeners) {
20
+ for (const l of listeners) {
21
+ l(data);
22
+ }
23
+ }
24
+ }
25
+ subscribe(topic, listener) {
26
+ let listeners = this.#topicListeners.get(topic);
27
+ if (!listeners) {
28
+ listeners = /* @__PURE__ */ new Set();
29
+ this.#topicListeners.set(topic, listeners);
30
+ }
31
+ listeners.add(listener);
32
+ const subId = Math.floor(Math.random() * 1e8);
33
+ this.#subIdTopic.set(subId, topic);
34
+ this.#subIdListeners.set(subId, listener);
35
+ return subId;
36
+ }
37
+ unsubscribe(subId) {
38
+ const listener = this.#subIdListeners.get(subId);
39
+ if (!listener) {
40
+ return;
41
+ }
42
+ this.#subIdListeners.delete(subId);
43
+ const topic = this.#subIdTopic.get(subId);
44
+ if (!topic) {
45
+ return;
46
+ }
47
+ this.#subIdTopic.delete(subId);
48
+ const listeners = this.#topicListeners.get(topic);
49
+ if (!listeners) {
50
+ return;
51
+ }
52
+ listeners.delete(listener);
53
+ if (listeners.size === 0) {
54
+ this.#topicListeners.delete(topic);
55
+ }
56
+ }
57
+ #asyncIteratorStops = /* @__PURE__ */ new Set();
58
+ asyncIterator(topic) {
59
+ return new repeater.Repeater(async (push, stop) => {
60
+ const subId = this.subscribe(topic, push);
61
+ this.#asyncIteratorStops.add(stop);
62
+ await stop;
63
+ this.#asyncIteratorStops.delete(stop);
64
+ this.unsubscribe(subId);
65
+ });
66
+ }
67
+ dispose() {
68
+ this.#topicListeners.clear();
69
+ this.#subIdListeners.clear();
70
+ this.#subIdTopic.clear();
71
+ for (const stop of this.#asyncIteratorStops) {
72
+ stop();
73
+ }
74
+ this.#asyncIteratorStops.clear();
75
+ }
76
+ [disposablestack.DisposableSymbols.dispose]() {
77
+ this.dispose();
78
+ }
79
+ }
80
+
81
+ exports.PubSub = PubSub;
@@ -0,0 +1,32 @@
1
+ import { DisposableSymbols } from '@whatwg-node/disposablestack';
2
+
3
+ type TopicDataMap = Record<string, any>;
4
+ interface HivePubSub<Data extends TopicDataMap = TopicDataMap> {
5
+ /** @deprecated Please use {@link subscribedTopics} if implemented instead. This method will be removed in next major release. */
6
+ getEventNames(): Iterable<keyof Data>;
7
+ /** @important This method will be required starting next major release. */
8
+ subscribedTopics?(): Iterable<keyof Data>;
9
+ publish<Topic extends keyof Data>(topic: Topic, data: Data[Topic]): void;
10
+ subscribe<Topic extends keyof Data>(topic: Topic, listener: PubSubListener<Data, Topic>): number;
11
+ unsubscribe(subId: number): void;
12
+ asyncIterator<Topic extends keyof Data>(topic: Topic): AsyncIterable<Data[Topic]>;
13
+ /** @important This method will be required starting next major release. */
14
+ dispose?(): void;
15
+ /** @important This method will be required starting next major release. */
16
+ [DisposableSymbols.dispose]?(): void;
17
+ }
18
+ type PubSubListener<Data extends TopicDataMap, Topic extends keyof Data> = (data: Data[Topic]) => void;
19
+ declare class PubSub<Data extends TopicDataMap = TopicDataMap> implements HivePubSub<Data> {
20
+ #private;
21
+ /** @deprecated Please use {@link subscribedTopics} instead. */
22
+ getEventNames(): MapIterator<keyof Data>;
23
+ subscribedTopics(): MapIterator<keyof Data>;
24
+ publish<Topic extends keyof Data>(topic: Topic, data: Data[Topic]): void;
25
+ subscribe<Topic extends keyof Data>(topic: Topic, listener: PubSubListener<Data, Topic>): number;
26
+ unsubscribe(subId: number): void;
27
+ asyncIterator<Topic extends keyof Data>(topic: Topic): AsyncIterable<Data[Topic]>;
28
+ dispose(): void;
29
+ [DisposableSymbols.dispose](): void;
30
+ }
31
+
32
+ export { type HivePubSub, PubSub, type PubSubListener };
@@ -0,0 +1,32 @@
1
+ import { DisposableSymbols } from '@whatwg-node/disposablestack';
2
+
3
+ type TopicDataMap = Record<string, any>;
4
+ interface HivePubSub<Data extends TopicDataMap = TopicDataMap> {
5
+ /** @deprecated Please use {@link subscribedTopics} if implemented instead. This method will be removed in next major release. */
6
+ getEventNames(): Iterable<keyof Data>;
7
+ /** @important This method will be required starting next major release. */
8
+ subscribedTopics?(): Iterable<keyof Data>;
9
+ publish<Topic extends keyof Data>(topic: Topic, data: Data[Topic]): void;
10
+ subscribe<Topic extends keyof Data>(topic: Topic, listener: PubSubListener<Data, Topic>): number;
11
+ unsubscribe(subId: number): void;
12
+ asyncIterator<Topic extends keyof Data>(topic: Topic): AsyncIterable<Data[Topic]>;
13
+ /** @important This method will be required starting next major release. */
14
+ dispose?(): void;
15
+ /** @important This method will be required starting next major release. */
16
+ [DisposableSymbols.dispose]?(): void;
17
+ }
18
+ type PubSubListener<Data extends TopicDataMap, Topic extends keyof Data> = (data: Data[Topic]) => void;
19
+ declare class PubSub<Data extends TopicDataMap = TopicDataMap> implements HivePubSub<Data> {
20
+ #private;
21
+ /** @deprecated Please use {@link subscribedTopics} instead. */
22
+ getEventNames(): MapIterator<keyof Data>;
23
+ subscribedTopics(): MapIterator<keyof Data>;
24
+ publish<Topic extends keyof Data>(topic: Topic, data: Data[Topic]): void;
25
+ subscribe<Topic extends keyof Data>(topic: Topic, listener: PubSubListener<Data, Topic>): number;
26
+ unsubscribe(subId: number): void;
27
+ asyncIterator<Topic extends keyof Data>(topic: Topic): AsyncIterable<Data[Topic]>;
28
+ dispose(): void;
29
+ [DisposableSymbols.dispose](): void;
30
+ }
31
+
32
+ export { type HivePubSub, PubSub, type PubSubListener };
package/dist/index.js ADDED
@@ -0,0 +1,79 @@
1
+ import { Repeater } from '@repeaterjs/repeater';
2
+ import { DisposableSymbols } from '@whatwg-node/disposablestack';
3
+
4
+ class PubSub {
5
+ #topicListeners = /* @__PURE__ */ new Map();
6
+ #subIdTopic = /* @__PURE__ */ new Map();
7
+ #subIdListeners = /* @__PURE__ */ new Map();
8
+ /** @deprecated Please use {@link subscribedTopics} instead. */
9
+ getEventNames() {
10
+ return this.#topicListeners.keys();
11
+ }
12
+ subscribedTopics() {
13
+ return this.#topicListeners.keys();
14
+ }
15
+ publish(topic, data) {
16
+ const listeners = this.#topicListeners.get(topic);
17
+ if (listeners) {
18
+ for (const l of listeners) {
19
+ l(data);
20
+ }
21
+ }
22
+ }
23
+ subscribe(topic, listener) {
24
+ let listeners = this.#topicListeners.get(topic);
25
+ if (!listeners) {
26
+ listeners = /* @__PURE__ */ new Set();
27
+ this.#topicListeners.set(topic, listeners);
28
+ }
29
+ listeners.add(listener);
30
+ const subId = Math.floor(Math.random() * 1e8);
31
+ this.#subIdTopic.set(subId, topic);
32
+ this.#subIdListeners.set(subId, listener);
33
+ return subId;
34
+ }
35
+ unsubscribe(subId) {
36
+ const listener = this.#subIdListeners.get(subId);
37
+ if (!listener) {
38
+ return;
39
+ }
40
+ this.#subIdListeners.delete(subId);
41
+ const topic = this.#subIdTopic.get(subId);
42
+ if (!topic) {
43
+ return;
44
+ }
45
+ this.#subIdTopic.delete(subId);
46
+ const listeners = this.#topicListeners.get(topic);
47
+ if (!listeners) {
48
+ return;
49
+ }
50
+ listeners.delete(listener);
51
+ if (listeners.size === 0) {
52
+ this.#topicListeners.delete(topic);
53
+ }
54
+ }
55
+ #asyncIteratorStops = /* @__PURE__ */ new Set();
56
+ asyncIterator(topic) {
57
+ return new Repeater(async (push, stop) => {
58
+ const subId = this.subscribe(topic, push);
59
+ this.#asyncIteratorStops.add(stop);
60
+ await stop;
61
+ this.#asyncIteratorStops.delete(stop);
62
+ this.unsubscribe(subId);
63
+ });
64
+ }
65
+ dispose() {
66
+ this.#topicListeners.clear();
67
+ this.#subIdListeners.clear();
68
+ this.#subIdTopic.clear();
69
+ for (const stop of this.#asyncIteratorStops) {
70
+ stop();
71
+ }
72
+ this.#asyncIteratorStops.clear();
73
+ }
74
+ [DisposableSymbols.dispose]() {
75
+ this.dispose();
76
+ }
77
+ }
78
+
79
+ export { PubSub };
package/package.json ADDED
@@ -0,0 +1,49 @@
1
+ {
2
+ "name": "@graphql-hive/pubsub",
3
+ "version": "0.0.0",
4
+ "type": "module",
5
+ "repository": {
6
+ "type": "git",
7
+ "url": "git+https://github.com/graphql-hive/gateway.git",
8
+ "directory": "packages/pubsub"
9
+ },
10
+ "author": {
11
+ "email": "contact@the-guild.dev",
12
+ "name": "The Guild",
13
+ "url": "https://the-guild.dev"
14
+ },
15
+ "license": "MIT",
16
+ "engines": {
17
+ "node": ">=18.0.0"
18
+ },
19
+ "main": "./dist/index.js",
20
+ "exports": {
21
+ ".": {
22
+ "require": {
23
+ "types": "./dist/index.d.cts",
24
+ "default": "./dist/index.cjs"
25
+ },
26
+ "import": {
27
+ "types": "./dist/index.d.ts",
28
+ "default": "./dist/index.js"
29
+ }
30
+ },
31
+ "./package.json": "./package.json"
32
+ },
33
+ "types": "./dist/index.d.ts",
34
+ "files": [
35
+ "dist"
36
+ ],
37
+ "scripts": {
38
+ "build": "pkgroll --clean-dist",
39
+ "prepack": "yarn build"
40
+ },
41
+ "dependencies": {
42
+ "@repeaterjs/repeater": "^3.0.6",
43
+ "@whatwg-node/disposablestack": "^0.0.6"
44
+ },
45
+ "devDependencies": {
46
+ "pkgroll": "2.11.2"
47
+ },
48
+ "sideEffects": false
49
+ }