@motiadev/stream-client 0.13.0-beta.162-945354 → 0.13.0-beta.162-080298
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/LICENSE +93 -21
- package/dist/cjs/__tests__/stream-group.spec.d.ts +1 -0
- package/dist/cjs/__tests__/stream-group.spec.js +107 -0
- package/dist/cjs/__tests__/stream-item.spec.d.ts +1 -0
- package/dist/cjs/__tests__/stream-item.spec.js +48 -0
- package/dist/cjs/__tests__/stream.spec.d.ts +1 -0
- package/dist/cjs/__tests__/stream.spec.js +83 -0
- package/dist/cjs/index.d.ts +7 -0
- package/dist/cjs/index.js +26 -0
- package/dist/cjs/src/adapter-factory.d.ts +2 -0
- package/dist/cjs/src/adapter-factory.js +2 -0
- package/dist/cjs/src/socket-adapter.d.ts +9 -0
- package/dist/cjs/src/socket-adapter.js +2 -0
- package/dist/cjs/src/stream-group.d.ts +13 -0
- package/dist/cjs/src/stream-group.js +69 -0
- package/dist/cjs/src/stream-item.d.ts +9 -0
- package/dist/cjs/src/stream-item.js +26 -0
- package/dist/cjs/src/stream-subscription.d.ts +36 -0
- package/dist/cjs/src/stream-subscription.js +63 -0
- package/dist/cjs/src/stream.d.ts +38 -0
- package/dist/cjs/src/stream.js +102 -0
- package/dist/cjs/src/stream.types.d.ts +56 -0
- package/dist/cjs/src/stream.types.js +2 -0
- package/dist/esm/__tests__/stream-group.spec.d.ts +1 -0
- package/dist/esm/__tests__/stream-group.spec.js +105 -0
- package/dist/esm/__tests__/stream-item.spec.d.ts +1 -0
- package/dist/esm/__tests__/stream-item.spec.js +46 -0
- package/dist/esm/__tests__/stream.spec.d.ts +1 -0
- package/dist/esm/__tests__/stream.spec.js +81 -0
- package/dist/esm/index.d.ts +7 -0
- package/dist/esm/index.js +5 -0
- package/dist/esm/src/adapter-factory.d.ts +2 -0
- package/dist/esm/src/adapter-factory.js +1 -0
- package/dist/esm/src/socket-adapter.d.ts +9 -0
- package/dist/esm/src/socket-adapter.js +1 -0
- package/dist/esm/src/stream-group.d.ts +13 -0
- package/dist/esm/src/stream-group.js +65 -0
- package/dist/esm/src/stream-item.d.ts +9 -0
- package/dist/esm/src/stream-item.js +22 -0
- package/dist/esm/src/stream-subscription.d.ts +36 -0
- package/dist/esm/src/stream-subscription.js +59 -0
- package/dist/esm/src/stream.d.ts +38 -0
- package/dist/esm/src/stream.js +98 -0
- package/dist/esm/src/stream.types.d.ts +56 -0
- package/dist/esm/src/stream.types.js +1 -0
- package/dist/types/__tests__/stream-group.spec.d.ts +1 -0
- package/dist/types/__tests__/stream-item.spec.d.ts +1 -0
- package/dist/types/__tests__/stream.spec.d.ts +1 -0
- package/dist/types/index.d.ts +7 -0
- package/dist/types/src/adapter-factory.d.ts +2 -0
- package/dist/types/src/socket-adapter.d.ts +9 -0
- package/dist/types/src/stream-group.d.ts +13 -0
- package/dist/types/src/stream-item.d.ts +9 -0
- package/dist/types/src/stream-subscription.d.ts +36 -0
- package/dist/types/src/stream.d.ts +38 -0
- package/dist/types/src/stream.types.d.ts +56 -0
- package/package.json +23 -14
- package/scripts/build.sh +14 -0
- package/__mocks__/uuid.ts +0 -10
- package/dist/index.d.mts +0 -8
- package/dist/index.mjs +0 -6
- package/dist/src/adapter-factory.d.mts +0 -7
- package/dist/src/socket-adapter.d.mts +0 -13
- package/dist/src/stream-group.d.mts +0 -18
- package/dist/src/stream-group.mjs +0 -55
- package/dist/src/stream-group.mjs.map +0 -1
- package/dist/src/stream-item.d.mts +0 -14
- package/dist/src/stream-item.mjs +0 -20
- package/dist/src/stream-item.mjs.map +0 -1
- package/dist/src/stream-subscription.d.mts +0 -40
- package/dist/src/stream-subscription.mjs +0 -64
- package/dist/src/stream-subscription.mjs.map +0 -1
- package/dist/src/stream.d.mts +0 -43
- package/dist/src/stream.mjs +0 -104
- package/dist/src/stream.mjs.map +0 -1
- package/dist/src/stream.types.d.mts +0 -60
- package/tsdown.config.ts +0 -18
package/dist/src/stream-item.mjs
DELETED
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
import { StreamSubscription } from "./stream-subscription.mjs";
|
|
2
|
-
|
|
3
|
-
//#region src/stream-item.ts
|
|
4
|
-
var StreamItemSubscription = class extends StreamSubscription {
|
|
5
|
-
constructor(sub) {
|
|
6
|
-
super(sub, null);
|
|
7
|
-
this.lastEventTimestamp = 0;
|
|
8
|
-
}
|
|
9
|
-
listener(message) {
|
|
10
|
-
if (message.timestamp <= this.lastEventTimestamp) return;
|
|
11
|
-
this.lastEventTimestamp = message.timestamp;
|
|
12
|
-
if (message.event.type === "sync" || message.event.type === "create" || message.event.type === "update") this.setState(message.event.data);
|
|
13
|
-
else if (message.event.type === "delete") this.setState(null);
|
|
14
|
-
else if (message.event.type === "event") this.onEventReceived(message.event.event);
|
|
15
|
-
}
|
|
16
|
-
};
|
|
17
|
-
|
|
18
|
-
//#endregion
|
|
19
|
-
export { StreamItemSubscription };
|
|
20
|
-
//# sourceMappingURL=stream-item.mjs.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"stream-item.mjs","names":[],"sources":["../../src/stream-item.ts"],"sourcesContent":["import type { ItemEventMessage, JoinMessage } from './stream.types'\nimport { StreamSubscription } from './stream-subscription'\n\nexport class StreamItemSubscription<TData extends { id: string }> extends StreamSubscription<\n TData | null,\n ItemEventMessage<TData>\n> {\n private lastEventTimestamp = 0\n\n constructor(sub: JoinMessage) {\n super(sub, null)\n }\n\n listener(message: ItemEventMessage<TData>): void {\n if (message.timestamp <= this.lastEventTimestamp) {\n return\n }\n\n this.lastEventTimestamp = message.timestamp\n\n if (message.event.type === 'sync' || message.event.type === 'create' || message.event.type === 'update') {\n this.setState(message.event.data)\n } else if (message.event.type === 'delete') {\n this.setState(null)\n } else if (message.event.type === 'event') {\n this.onEventReceived(message.event.event)\n }\n }\n}\n"],"mappings":";;;AAGA,IAAa,yBAAb,cAA0E,mBAGxE;CAGA,YAAY,KAAkB;AAC5B,QAAM,KAAK,KAAK;4BAHW;;CAM7B,SAAS,SAAwC;AAC/C,MAAI,QAAQ,aAAa,KAAK,mBAC5B;AAGF,OAAK,qBAAqB,QAAQ;AAElC,MAAI,QAAQ,MAAM,SAAS,UAAU,QAAQ,MAAM,SAAS,YAAY,QAAQ,MAAM,SAAS,SAC7F,MAAK,SAAS,QAAQ,MAAM,KAAK;WACxB,QAAQ,MAAM,SAAS,SAChC,MAAK,SAAS,KAAK;WACV,QAAQ,MAAM,SAAS,QAChC,MAAK,gBAAgB,QAAQ,MAAM,MAAM"}
|
|
@@ -1,40 +0,0 @@
|
|
|
1
|
-
import { CustomEvent, JoinMessage, Listener } from "./stream.types.mjs";
|
|
2
|
-
|
|
3
|
-
//#region src/stream-subscription.d.ts
|
|
4
|
-
type CustomEventListener = (event: any) => void;
|
|
5
|
-
declare abstract class StreamSubscription<TData = unknown, TEventData = unknown> {
|
|
6
|
-
private customEventListeners;
|
|
7
|
-
private closeListeners;
|
|
8
|
-
private onChangeListeners;
|
|
9
|
-
private state;
|
|
10
|
-
readonly sub: JoinMessage;
|
|
11
|
-
constructor(sub: JoinMessage, state: TData);
|
|
12
|
-
abstract listener(message: TEventData): void;
|
|
13
|
-
protected onEventReceived(event: CustomEvent): void;
|
|
14
|
-
/**
|
|
15
|
-
* Add a custom event listener. This listener will be called whenever the custom event is received.
|
|
16
|
-
*/
|
|
17
|
-
onEvent(type: string, listener: CustomEventListener): void;
|
|
18
|
-
/**
|
|
19
|
-
* Remove a custom event listener.
|
|
20
|
-
*/
|
|
21
|
-
offEvent(type: string, listener: CustomEventListener): void;
|
|
22
|
-
onClose(listener: () => void): void;
|
|
23
|
-
close(): void;
|
|
24
|
-
/**
|
|
25
|
-
* Add a change listener. This listener will be called whenever the state of the group changes.
|
|
26
|
-
*/
|
|
27
|
-
addChangeListener(listener: Listener<TData>): void;
|
|
28
|
-
/**
|
|
29
|
-
* Remove a change listener.
|
|
30
|
-
*/
|
|
31
|
-
removeChangeListener(listener: Listener<TData>): void;
|
|
32
|
-
/**
|
|
33
|
-
* Get the current state of the group.
|
|
34
|
-
*/
|
|
35
|
-
getState(): TData;
|
|
36
|
-
protected setState(state: TData): void;
|
|
37
|
-
}
|
|
38
|
-
//#endregion
|
|
39
|
-
export { StreamSubscription };
|
|
40
|
-
//# sourceMappingURL=stream-subscription.d.mts.map
|
|
@@ -1,64 +0,0 @@
|
|
|
1
|
-
//#region src/stream-subscription.ts
|
|
2
|
-
var StreamSubscription = class {
|
|
3
|
-
constructor(sub, state) {
|
|
4
|
-
this.customEventListeners = /* @__PURE__ */ new Map();
|
|
5
|
-
this.closeListeners = /* @__PURE__ */ new Set();
|
|
6
|
-
this.onChangeListeners = /* @__PURE__ */ new Set();
|
|
7
|
-
this.sub = sub;
|
|
8
|
-
this.state = state;
|
|
9
|
-
}
|
|
10
|
-
onEventReceived(event) {
|
|
11
|
-
const customEventListeners = this.customEventListeners.get(event.type);
|
|
12
|
-
if (customEventListeners) {
|
|
13
|
-
const eventData = event.data;
|
|
14
|
-
customEventListeners.forEach((listener) => listener(eventData));
|
|
15
|
-
}
|
|
16
|
-
}
|
|
17
|
-
/**
|
|
18
|
-
* Add a custom event listener. This listener will be called whenever the custom event is received.
|
|
19
|
-
*/
|
|
20
|
-
onEvent(type, listener) {
|
|
21
|
-
const listeners = this.customEventListeners.get(type) || [];
|
|
22
|
-
this.customEventListeners.set(type, [...listeners, listener]);
|
|
23
|
-
}
|
|
24
|
-
/**
|
|
25
|
-
* Remove a custom event listener.
|
|
26
|
-
*/
|
|
27
|
-
offEvent(type, listener) {
|
|
28
|
-
const listeners = this.customEventListeners.get(type) || [];
|
|
29
|
-
this.customEventListeners.set(type, listeners.filter((l) => l !== listener));
|
|
30
|
-
}
|
|
31
|
-
onClose(listener) {
|
|
32
|
-
this.closeListeners.add(listener);
|
|
33
|
-
}
|
|
34
|
-
close() {
|
|
35
|
-
this.closeListeners.forEach((listener) => listener());
|
|
36
|
-
this.closeListeners.clear();
|
|
37
|
-
}
|
|
38
|
-
/**
|
|
39
|
-
* Add a change listener. This listener will be called whenever the state of the group changes.
|
|
40
|
-
*/
|
|
41
|
-
addChangeListener(listener) {
|
|
42
|
-
this.onChangeListeners.add(listener);
|
|
43
|
-
}
|
|
44
|
-
/**
|
|
45
|
-
* Remove a change listener.
|
|
46
|
-
*/
|
|
47
|
-
removeChangeListener(listener) {
|
|
48
|
-
this.onChangeListeners.delete(listener);
|
|
49
|
-
}
|
|
50
|
-
/**
|
|
51
|
-
* Get the current state of the group.
|
|
52
|
-
*/
|
|
53
|
-
getState() {
|
|
54
|
-
return this.state;
|
|
55
|
-
}
|
|
56
|
-
setState(state) {
|
|
57
|
-
this.state = state;
|
|
58
|
-
this.onChangeListeners.forEach((listener) => listener(state));
|
|
59
|
-
}
|
|
60
|
-
};
|
|
61
|
-
|
|
62
|
-
//#endregion
|
|
63
|
-
export { StreamSubscription };
|
|
64
|
-
//# sourceMappingURL=stream-subscription.mjs.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"stream-subscription.mjs","names":[],"sources":["../../src/stream-subscription.ts"],"sourcesContent":["import type { CustomEvent, JoinMessage, Listener } from './stream.types'\n\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\ntype CustomEventListener = (event: any) => void\n\nexport abstract class StreamSubscription<TData = unknown, TEventData = unknown> {\n private customEventListeners: Map<string, CustomEventListener[]> = new Map()\n private closeListeners: Set<() => void> = new Set()\n\n private onChangeListeners: Set<Listener<TData>> = new Set()\n private state: TData\n\n readonly sub: JoinMessage\n\n constructor(sub: JoinMessage, state: TData) {\n this.sub = sub\n this.state = state\n }\n\n abstract listener(message: TEventData): void\n\n protected onEventReceived(event: CustomEvent) {\n const customEventListeners = this.customEventListeners.get(event.type)\n\n if (customEventListeners) {\n const eventData = event.data\n customEventListeners.forEach((listener) => listener(eventData))\n }\n }\n\n /**\n * Add a custom event listener. This listener will be called whenever the custom event is received.\n */\n onEvent(type: string, listener: CustomEventListener) {\n const listeners = this.customEventListeners.get(type) || []\n this.customEventListeners.set(type, [...listeners, listener])\n }\n\n /**\n * Remove a custom event listener.\n */\n offEvent(type: string, listener: CustomEventListener) {\n const listeners = this.customEventListeners.get(type) || []\n this.customEventListeners.set(\n type,\n listeners.filter((l) => l !== listener),\n )\n }\n\n onClose(listener: () => void) {\n this.closeListeners.add(listener)\n }\n\n close() {\n this.closeListeners.forEach((listener) => listener())\n this.closeListeners.clear()\n }\n\n /**\n * Add a change listener. This listener will be called whenever the state of the group changes.\n */\n addChangeListener(listener: Listener<TData>) {\n this.onChangeListeners.add(listener)\n }\n\n /**\n * Remove a change listener.\n */\n removeChangeListener(listener: Listener<TData>) {\n this.onChangeListeners.delete(listener)\n }\n\n /**\n * Get the current state of the group.\n */\n getState(): TData {\n return this.state\n }\n\n protected setState(state: TData) {\n this.state = state\n this.onChangeListeners.forEach((listener) => listener(state))\n }\n}\n"],"mappings":";AAKA,IAAsB,qBAAtB,MAAgF;CAS9E,YAAY,KAAkB,OAAc;8CARuB,IAAI,KAAK;wCAClC,IAAI,KAAK;2CAED,IAAI,KAAK;AAMzD,OAAK,MAAM;AACX,OAAK,QAAQ;;CAKf,AAAU,gBAAgB,OAAoB;EAC5C,MAAM,uBAAuB,KAAK,qBAAqB,IAAI,MAAM,KAAK;AAEtE,MAAI,sBAAsB;GACxB,MAAM,YAAY,MAAM;AACxB,wBAAqB,SAAS,aAAa,SAAS,UAAU,CAAC;;;;;;CAOnE,QAAQ,MAAc,UAA+B;EACnD,MAAM,YAAY,KAAK,qBAAqB,IAAI,KAAK,IAAI,EAAE;AAC3D,OAAK,qBAAqB,IAAI,MAAM,CAAC,GAAG,WAAW,SAAS,CAAC;;;;;CAM/D,SAAS,MAAc,UAA+B;EACpD,MAAM,YAAY,KAAK,qBAAqB,IAAI,KAAK,IAAI,EAAE;AAC3D,OAAK,qBAAqB,IACxB,MACA,UAAU,QAAQ,MAAM,MAAM,SAAS,CACxC;;CAGH,QAAQ,UAAsB;AAC5B,OAAK,eAAe,IAAI,SAAS;;CAGnC,QAAQ;AACN,OAAK,eAAe,SAAS,aAAa,UAAU,CAAC;AACrD,OAAK,eAAe,OAAO;;;;;CAM7B,kBAAkB,UAA2B;AAC3C,OAAK,kBAAkB,IAAI,SAAS;;;;;CAMtC,qBAAqB,UAA2B;AAC9C,OAAK,kBAAkB,OAAO,SAAS;;;;;CAMzC,WAAkB;AAChB,SAAO,KAAK;;CAGd,AAAU,SAAS,OAAc;AAC/B,OAAK,QAAQ;AACb,OAAK,kBAAkB,SAAS,aAAa,SAAS,MAAM,CAAC"}
|
package/dist/src/stream.d.mts
DELETED
|
@@ -1,43 +0,0 @@
|
|
|
1
|
-
import { SocketAdapter } from "./socket-adapter.mjs";
|
|
2
|
-
import { SocketAdapterFactory } from "./adapter-factory.mjs";
|
|
3
|
-
import { StreamGroupSubscription } from "./stream-group.mjs";
|
|
4
|
-
import { StreamItemSubscription } from "./stream-item.mjs";
|
|
5
|
-
|
|
6
|
-
//#region src/stream.d.ts
|
|
7
|
-
declare class Stream {
|
|
8
|
-
private adapterFactory;
|
|
9
|
-
private ws;
|
|
10
|
-
private listeners;
|
|
11
|
-
constructor(adapterFactory: SocketAdapterFactory);
|
|
12
|
-
createSocket(): SocketAdapter;
|
|
13
|
-
/**
|
|
14
|
-
* Subscribe to an item in a stream.
|
|
15
|
-
*
|
|
16
|
-
* @argument streamName - The name of the stream to subscribe to.
|
|
17
|
-
* @argument groupId - The id of the group to subscribe to.
|
|
18
|
-
* @argument id - The id of the item to subscribe to.
|
|
19
|
-
*/
|
|
20
|
-
subscribeItem<TData extends {
|
|
21
|
-
id: string;
|
|
22
|
-
}>(streamName: string, groupId: string, id: string): StreamItemSubscription<TData>;
|
|
23
|
-
/**
|
|
24
|
-
* Subscribe to a group in a stream.
|
|
25
|
-
*
|
|
26
|
-
* @argument streamName - The name of the stream to subscribe to.
|
|
27
|
-
* @argument groupId - The id of the group to subscribe to.
|
|
28
|
-
*/
|
|
29
|
-
subscribeGroup<TData extends {
|
|
30
|
-
id: string;
|
|
31
|
-
}>(streamName: string, groupId: string, sortKey?: keyof TData): StreamGroupSubscription<TData>;
|
|
32
|
-
close(): void;
|
|
33
|
-
private onSocketClose;
|
|
34
|
-
private onSocketOpen;
|
|
35
|
-
messageListener(event: string): void;
|
|
36
|
-
private subscribe;
|
|
37
|
-
private join;
|
|
38
|
-
private leave;
|
|
39
|
-
private roomName;
|
|
40
|
-
}
|
|
41
|
-
//#endregion
|
|
42
|
-
export { Stream };
|
|
43
|
-
//# sourceMappingURL=stream.d.mts.map
|
package/dist/src/stream.mjs
DELETED
|
@@ -1,104 +0,0 @@
|
|
|
1
|
-
import { StreamGroupSubscription } from "./stream-group.mjs";
|
|
2
|
-
import { StreamItemSubscription } from "./stream-item.mjs";
|
|
3
|
-
import { v4 } from "uuid";
|
|
4
|
-
|
|
5
|
-
//#region src/stream.ts
|
|
6
|
-
var Stream = class {
|
|
7
|
-
constructor(adapterFactory) {
|
|
8
|
-
this.adapterFactory = adapterFactory;
|
|
9
|
-
this.listeners = {};
|
|
10
|
-
this.ws = this.createSocket();
|
|
11
|
-
}
|
|
12
|
-
createSocket() {
|
|
13
|
-
this.ws = this.adapterFactory();
|
|
14
|
-
this.ws.onMessage((message) => this.messageListener(message));
|
|
15
|
-
this.ws.onOpen(() => this.onSocketOpen());
|
|
16
|
-
this.ws.onClose(() => this.onSocketClose());
|
|
17
|
-
return this.ws;
|
|
18
|
-
}
|
|
19
|
-
/**
|
|
20
|
-
* Subscribe to an item in a stream.
|
|
21
|
-
*
|
|
22
|
-
* @argument streamName - The name of the stream to subscribe to.
|
|
23
|
-
* @argument groupId - The id of the group to subscribe to.
|
|
24
|
-
* @argument id - The id of the item to subscribe to.
|
|
25
|
-
*/
|
|
26
|
-
subscribeItem(streamName, groupId, id) {
|
|
27
|
-
const subscription = new StreamItemSubscription({
|
|
28
|
-
streamName,
|
|
29
|
-
groupId,
|
|
30
|
-
id,
|
|
31
|
-
subscriptionId: v4()
|
|
32
|
-
});
|
|
33
|
-
this.subscribe(subscription);
|
|
34
|
-
return subscription;
|
|
35
|
-
}
|
|
36
|
-
/**
|
|
37
|
-
* Subscribe to a group in a stream.
|
|
38
|
-
*
|
|
39
|
-
* @argument streamName - The name of the stream to subscribe to.
|
|
40
|
-
* @argument groupId - The id of the group to subscribe to.
|
|
41
|
-
*/
|
|
42
|
-
subscribeGroup(streamName, groupId, sortKey) {
|
|
43
|
-
const subscription = new StreamGroupSubscription({
|
|
44
|
-
streamName,
|
|
45
|
-
groupId,
|
|
46
|
-
subscriptionId: v4()
|
|
47
|
-
}, sortKey);
|
|
48
|
-
this.subscribe(subscription);
|
|
49
|
-
return subscription;
|
|
50
|
-
}
|
|
51
|
-
close() {
|
|
52
|
-
this.listeners = {};
|
|
53
|
-
this.ws.close();
|
|
54
|
-
}
|
|
55
|
-
onSocketClose() {
|
|
56
|
-
setTimeout(() => this.createSocket(), 2e3);
|
|
57
|
-
}
|
|
58
|
-
onSocketOpen() {
|
|
59
|
-
Object.values(this.listeners).forEach((listeners) => {
|
|
60
|
-
listeners.forEach((subscription) => this.join(subscription));
|
|
61
|
-
});
|
|
62
|
-
}
|
|
63
|
-
messageListener(event) {
|
|
64
|
-
const message = JSON.parse(event);
|
|
65
|
-
const room = this.roomName(message);
|
|
66
|
-
this.listeners[room]?.forEach((listener) => listener.listener(message));
|
|
67
|
-
if (message.id && message.event.type !== "sync") {
|
|
68
|
-
const groupRoom = this.roomName({
|
|
69
|
-
streamName: message.streamName,
|
|
70
|
-
groupId: message.groupId
|
|
71
|
-
});
|
|
72
|
-
this.listeners[groupRoom]?.forEach((listener) => listener.listener(message));
|
|
73
|
-
}
|
|
74
|
-
}
|
|
75
|
-
subscribe(subscription) {
|
|
76
|
-
const room = this.roomName(subscription.sub);
|
|
77
|
-
if (!this.listeners[room]) this.listeners[room] = /* @__PURE__ */ new Set();
|
|
78
|
-
this.listeners[room].add(subscription);
|
|
79
|
-
this.join(subscription);
|
|
80
|
-
subscription.onClose(() => {
|
|
81
|
-
this.listeners[room]?.delete(subscription);
|
|
82
|
-
this.leave(subscription);
|
|
83
|
-
});
|
|
84
|
-
}
|
|
85
|
-
join(subscription) {
|
|
86
|
-
if (this.ws.isOpen()) this.ws.send(JSON.stringify({
|
|
87
|
-
type: "join",
|
|
88
|
-
data: subscription.sub
|
|
89
|
-
}));
|
|
90
|
-
}
|
|
91
|
-
leave(subscription) {
|
|
92
|
-
if (this.ws.isOpen()) this.ws.send(JSON.stringify({
|
|
93
|
-
type: "leave",
|
|
94
|
-
data: subscription.sub
|
|
95
|
-
}));
|
|
96
|
-
}
|
|
97
|
-
roomName(message) {
|
|
98
|
-
return message.id ? `${message.streamName}:group:${message.groupId}:item:${message.id}` : `${message.streamName}:group:${message.groupId}`;
|
|
99
|
-
}
|
|
100
|
-
};
|
|
101
|
-
|
|
102
|
-
//#endregion
|
|
103
|
-
export { Stream };
|
|
104
|
-
//# sourceMappingURL=stream.mjs.map
|
package/dist/src/stream.mjs.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"stream.mjs","names":["adapterFactory: SocketAdapterFactory","uuidv4","message: ItemEventMessage<never>"],"sources":["../../src/stream.ts"],"sourcesContent":["import { v4 as uuidv4 } from 'uuid'\nimport type { SocketAdapterFactory } from './adapter-factory'\nimport type { SocketAdapter } from './socket-adapter'\nimport type { BaseMessage, ItemEventMessage } from './stream.types'\nimport { StreamGroupSubscription } from './stream-group'\nimport { StreamItemSubscription } from './stream-item'\nimport type { StreamSubscription } from './stream-subscription'\n\nexport class Stream {\n private ws: SocketAdapter\n private listeners: { [channelId: string]: Set<StreamSubscription> } = {}\n\n constructor(private adapterFactory: SocketAdapterFactory) {\n this.ws = this.createSocket()\n }\n\n createSocket(): SocketAdapter {\n this.ws = this.adapterFactory()\n this.ws.onMessage((message) => this.messageListener(message))\n this.ws.onOpen(() => this.onSocketOpen())\n this.ws.onClose(() => this.onSocketClose())\n\n return this.ws\n }\n\n /**\n * Subscribe to an item in a stream.\n *\n * @argument streamName - The name of the stream to subscribe to.\n * @argument groupId - The id of the group to subscribe to.\n * @argument id - The id of the item to subscribe to.\n */\n subscribeItem<TData extends { id: string }>(\n streamName: string,\n groupId: string,\n id: string,\n ): StreamItemSubscription<TData> {\n const subscriptionId = uuidv4()\n const sub = { streamName, groupId, id, subscriptionId }\n const subscription = new StreamItemSubscription<TData>(sub)\n\n this.subscribe(subscription)\n\n return subscription\n }\n\n /**\n * Subscribe to a group in a stream.\n *\n * @argument streamName - The name of the stream to subscribe to.\n * @argument groupId - The id of the group to subscribe to.\n */\n subscribeGroup<TData extends { id: string }>(\n streamName: string,\n groupId: string,\n sortKey?: keyof TData,\n ): StreamGroupSubscription<TData> {\n const subscriptionId = uuidv4()\n const sub = { streamName, groupId, subscriptionId }\n const subscription = new StreamGroupSubscription<TData>(sub, sortKey)\n\n this.subscribe(subscription)\n\n return subscription\n }\n\n close() {\n this.listeners = {} // clean up all listeners\n this.ws.close()\n }\n\n private onSocketClose(): void {\n // retry to connect\n setTimeout(() => this.createSocket(), 2000)\n }\n\n private onSocketOpen(): void {\n Object.values(this.listeners).forEach((listeners) => {\n listeners.forEach((subscription) => this.join(subscription))\n })\n }\n\n messageListener(event: string): void {\n const message: ItemEventMessage<never> = JSON.parse(event)\n const room = this.roomName(message)\n\n this.listeners[room]?.forEach((listener) => listener.listener(message))\n\n // we need to discard sync to group subs when it's an item event\n if (message.id && message.event.type !== 'sync') {\n const groupRoom = this.roomName({\n streamName: message.streamName,\n groupId: message.groupId,\n })\n\n this.listeners[groupRoom]?.forEach((listener) => listener.listener(message))\n }\n }\n\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n private subscribe(subscription: StreamSubscription<any, any>): void {\n const room = this.roomName(subscription.sub)\n\n if (!this.listeners[room]) {\n this.listeners[room] = new Set()\n }\n\n this.listeners[room].add(subscription)\n this.join(subscription)\n\n subscription.onClose(() => {\n this.listeners[room]?.delete(subscription)\n this.leave(subscription)\n })\n }\n\n private join(subscription: StreamSubscription): void {\n if (this.ws.isOpen()) {\n this.ws.send(JSON.stringify({ type: 'join', data: subscription.sub }))\n }\n }\n\n private leave(subscription: StreamSubscription): void {\n if (this.ws.isOpen()) {\n this.ws.send(JSON.stringify({ type: 'leave', data: subscription.sub }))\n }\n }\n\n private roomName(message: Omit<BaseMessage, 'timestamp'>): string {\n return message.id\n ? `${message.streamName}:group:${message.groupId}:item:${message.id}`\n : `${message.streamName}:group:${message.groupId}`\n }\n}\n"],"mappings":";;;;;AAQA,IAAa,SAAb,MAAoB;CAIlB,YAAY,AAAQA,gBAAsC;EAAtC;mBAFkD,EAAE;AAGtE,OAAK,KAAK,KAAK,cAAc;;CAG/B,eAA8B;AAC5B,OAAK,KAAK,KAAK,gBAAgB;AAC/B,OAAK,GAAG,WAAW,YAAY,KAAK,gBAAgB,QAAQ,CAAC;AAC7D,OAAK,GAAG,aAAa,KAAK,cAAc,CAAC;AACzC,OAAK,GAAG,cAAc,KAAK,eAAe,CAAC;AAE3C,SAAO,KAAK;;;;;;;;;CAUd,cACE,YACA,SACA,IAC+B;EAG/B,MAAM,eAAe,IAAI,uBADb;GAAE;GAAY;GAAS;GAAI,gBADhBC,IAAQ;GACwB,CACI;AAE3D,OAAK,UAAU,aAAa;AAE5B,SAAO;;;;;;;;CAST,eACE,YACA,SACA,SACgC;EAGhC,MAAM,eAAe,IAAI,wBADb;GAAE;GAAY;GAAS,gBADZA,IAAQ;GACoB,EACU,QAAQ;AAErE,OAAK,UAAU,aAAa;AAE5B,SAAO;;CAGT,QAAQ;AACN,OAAK,YAAY,EAAE;AACnB,OAAK,GAAG,OAAO;;CAGjB,AAAQ,gBAAsB;AAE5B,mBAAiB,KAAK,cAAc,EAAE,IAAK;;CAG7C,AAAQ,eAAqB;AAC3B,SAAO,OAAO,KAAK,UAAU,CAAC,SAAS,cAAc;AACnD,aAAU,SAAS,iBAAiB,KAAK,KAAK,aAAa,CAAC;IAC5D;;CAGJ,gBAAgB,OAAqB;EACnC,MAAMC,UAAmC,KAAK,MAAM,MAAM;EAC1D,MAAM,OAAO,KAAK,SAAS,QAAQ;AAEnC,OAAK,UAAU,OAAO,SAAS,aAAa,SAAS,SAAS,QAAQ,CAAC;AAGvE,MAAI,QAAQ,MAAM,QAAQ,MAAM,SAAS,QAAQ;GAC/C,MAAM,YAAY,KAAK,SAAS;IAC9B,YAAY,QAAQ;IACpB,SAAS,QAAQ;IAClB,CAAC;AAEF,QAAK,UAAU,YAAY,SAAS,aAAa,SAAS,SAAS,QAAQ,CAAC;;;CAKhF,AAAQ,UAAU,cAAkD;EAClE,MAAM,OAAO,KAAK,SAAS,aAAa,IAAI;AAE5C,MAAI,CAAC,KAAK,UAAU,MAClB,MAAK,UAAU,wBAAQ,IAAI,KAAK;AAGlC,OAAK,UAAU,MAAM,IAAI,aAAa;AACtC,OAAK,KAAK,aAAa;AAEvB,eAAa,cAAc;AACzB,QAAK,UAAU,OAAO,OAAO,aAAa;AAC1C,QAAK,MAAM,aAAa;IACxB;;CAGJ,AAAQ,KAAK,cAAwC;AACnD,MAAI,KAAK,GAAG,QAAQ,CAClB,MAAK,GAAG,KAAK,KAAK,UAAU;GAAE,MAAM;GAAQ,MAAM,aAAa;GAAK,CAAC,CAAC;;CAI1E,AAAQ,MAAM,cAAwC;AACpD,MAAI,KAAK,GAAG,QAAQ,CAClB,MAAK,GAAG,KAAK,KAAK,UAAU;GAAE,MAAM;GAAS,MAAM,aAAa;GAAK,CAAC,CAAC;;CAI3E,AAAQ,SAAS,SAAiD;AAChE,SAAO,QAAQ,KACX,GAAG,QAAQ,WAAW,SAAS,QAAQ,QAAQ,QAAQ,QAAQ,OAC/D,GAAG,QAAQ,WAAW,SAAS,QAAQ"}
|
|
@@ -1,60 +0,0 @@
|
|
|
1
|
-
//#region src/stream.types.d.ts
|
|
2
|
-
type BaseMessage = {
|
|
3
|
-
streamName: string;
|
|
4
|
-
groupId: string;
|
|
5
|
-
id?: string;
|
|
6
|
-
timestamp: number;
|
|
7
|
-
};
|
|
8
|
-
type JoinMessage = Omit<BaseMessage, 'timestamp'> & {
|
|
9
|
-
subscriptionId: string;
|
|
10
|
-
};
|
|
11
|
-
type CustomEvent = {
|
|
12
|
-
type: string;
|
|
13
|
-
data: any;
|
|
14
|
-
};
|
|
15
|
-
type StreamEvent<TData extends {
|
|
16
|
-
id: string;
|
|
17
|
-
}> = {
|
|
18
|
-
type: 'create';
|
|
19
|
-
data: TData;
|
|
20
|
-
} | {
|
|
21
|
-
type: 'update';
|
|
22
|
-
data: TData;
|
|
23
|
-
} | {
|
|
24
|
-
type: 'delete';
|
|
25
|
-
data: TData;
|
|
26
|
-
} | {
|
|
27
|
-
type: 'event';
|
|
28
|
-
event: CustomEvent;
|
|
29
|
-
};
|
|
30
|
-
type ItemStreamEvent<TData extends {
|
|
31
|
-
id: string;
|
|
32
|
-
}> = StreamEvent<TData> | {
|
|
33
|
-
type: 'sync';
|
|
34
|
-
data: TData;
|
|
35
|
-
};
|
|
36
|
-
type GroupStreamEvent<TData extends {
|
|
37
|
-
id: string;
|
|
38
|
-
}> = StreamEvent<TData> | {
|
|
39
|
-
type: 'sync';
|
|
40
|
-
data: TData[];
|
|
41
|
-
};
|
|
42
|
-
type ItemEventMessage<TData extends {
|
|
43
|
-
id: string;
|
|
44
|
-
}> = BaseMessage & {
|
|
45
|
-
event: ItemStreamEvent<TData>;
|
|
46
|
-
};
|
|
47
|
-
type GroupEventMessage<TData extends {
|
|
48
|
-
id: string;
|
|
49
|
-
}> = BaseMessage & {
|
|
50
|
-
event: GroupStreamEvent<TData>;
|
|
51
|
-
};
|
|
52
|
-
type Message = {
|
|
53
|
-
type: 'join' | 'leave';
|
|
54
|
-
data: JoinMessage;
|
|
55
|
-
};
|
|
56
|
-
type Listener<TData> = (state: TData | null) => void;
|
|
57
|
-
type CustomEventListener<TData> = (event: TData) => void;
|
|
58
|
-
//#endregion
|
|
59
|
-
export { BaseMessage, CustomEvent, CustomEventListener, GroupEventMessage, GroupStreamEvent, ItemEventMessage, ItemStreamEvent, JoinMessage, Listener, Message, StreamEvent };
|
|
60
|
-
//# sourceMappingURL=stream.types.d.mts.map
|
package/tsdown.config.ts
DELETED
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
import { defineConfig } from 'tsdown'
|
|
2
|
-
|
|
3
|
-
export default defineConfig({
|
|
4
|
-
entry: {
|
|
5
|
-
index: './index.ts',
|
|
6
|
-
},
|
|
7
|
-
format: 'esm',
|
|
8
|
-
platform: 'neutral',
|
|
9
|
-
external: ['uuid'],
|
|
10
|
-
dts: {
|
|
11
|
-
build: true,
|
|
12
|
-
},
|
|
13
|
-
clean: true,
|
|
14
|
-
outDir: 'dist',
|
|
15
|
-
sourcemap: true,
|
|
16
|
-
unbundle: true,
|
|
17
|
-
fixedExtension: true,
|
|
18
|
-
})
|