@motiadev/stream-client 0.13.0-beta.161 → 0.13.0-beta.162-439402

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.
Files changed (76) hide show
  1. package/__mocks__/uuid.ts +10 -0
  2. package/dist/index.d.mts +8 -0
  3. package/dist/index.mjs +6 -0
  4. package/dist/src/adapter-factory.d.mts +7 -0
  5. package/dist/src/socket-adapter.d.mts +13 -0
  6. package/dist/src/stream-group.d.mts +18 -0
  7. package/dist/src/stream-group.mjs +55 -0
  8. package/dist/src/stream-group.mjs.map +1 -0
  9. package/dist/src/stream-item.d.mts +14 -0
  10. package/dist/src/stream-item.mjs +20 -0
  11. package/dist/src/stream-item.mjs.map +1 -0
  12. package/dist/src/stream-subscription.d.mts +40 -0
  13. package/dist/src/stream-subscription.mjs +64 -0
  14. package/dist/src/stream-subscription.mjs.map +1 -0
  15. package/dist/src/stream.d.mts +43 -0
  16. package/dist/src/stream.mjs +104 -0
  17. package/dist/src/stream.mjs.map +1 -0
  18. package/dist/src/stream.types.d.mts +60 -0
  19. package/package.json +13 -22
  20. package/tsdown.config.ts +18 -0
  21. package/dist/cjs/__tests__/stream-group.spec.d.ts +0 -1
  22. package/dist/cjs/__tests__/stream-group.spec.js +0 -107
  23. package/dist/cjs/__tests__/stream-item.spec.d.ts +0 -1
  24. package/dist/cjs/__tests__/stream-item.spec.js +0 -48
  25. package/dist/cjs/__tests__/stream.spec.d.ts +0 -1
  26. package/dist/cjs/__tests__/stream.spec.js +0 -83
  27. package/dist/cjs/index.d.ts +0 -7
  28. package/dist/cjs/index.js +0 -26
  29. package/dist/cjs/src/adapter-factory.d.ts +0 -2
  30. package/dist/cjs/src/adapter-factory.js +0 -2
  31. package/dist/cjs/src/socket-adapter.d.ts +0 -9
  32. package/dist/cjs/src/socket-adapter.js +0 -2
  33. package/dist/cjs/src/stream-group.d.ts +0 -13
  34. package/dist/cjs/src/stream-group.js +0 -69
  35. package/dist/cjs/src/stream-item.d.ts +0 -9
  36. package/dist/cjs/src/stream-item.js +0 -26
  37. package/dist/cjs/src/stream-subscription.d.ts +0 -36
  38. package/dist/cjs/src/stream-subscription.js +0 -63
  39. package/dist/cjs/src/stream.d.ts +0 -38
  40. package/dist/cjs/src/stream.js +0 -102
  41. package/dist/cjs/src/stream.types.d.ts +0 -56
  42. package/dist/cjs/src/stream.types.js +0 -2
  43. package/dist/esm/__tests__/stream-group.spec.d.ts +0 -1
  44. package/dist/esm/__tests__/stream-group.spec.js +0 -105
  45. package/dist/esm/__tests__/stream-item.spec.d.ts +0 -1
  46. package/dist/esm/__tests__/stream-item.spec.js +0 -46
  47. package/dist/esm/__tests__/stream.spec.d.ts +0 -1
  48. package/dist/esm/__tests__/stream.spec.js +0 -81
  49. package/dist/esm/index.d.ts +0 -7
  50. package/dist/esm/index.js +0 -5
  51. package/dist/esm/src/adapter-factory.d.ts +0 -2
  52. package/dist/esm/src/adapter-factory.js +0 -1
  53. package/dist/esm/src/socket-adapter.d.ts +0 -9
  54. package/dist/esm/src/socket-adapter.js +0 -1
  55. package/dist/esm/src/stream-group.d.ts +0 -13
  56. package/dist/esm/src/stream-group.js +0 -65
  57. package/dist/esm/src/stream-item.d.ts +0 -9
  58. package/dist/esm/src/stream-item.js +0 -22
  59. package/dist/esm/src/stream-subscription.d.ts +0 -36
  60. package/dist/esm/src/stream-subscription.js +0 -59
  61. package/dist/esm/src/stream.d.ts +0 -38
  62. package/dist/esm/src/stream.js +0 -98
  63. package/dist/esm/src/stream.types.d.ts +0 -56
  64. package/dist/esm/src/stream.types.js +0 -1
  65. package/dist/types/__tests__/stream-group.spec.d.ts +0 -1
  66. package/dist/types/__tests__/stream-item.spec.d.ts +0 -1
  67. package/dist/types/__tests__/stream.spec.d.ts +0 -1
  68. package/dist/types/index.d.ts +0 -7
  69. package/dist/types/src/adapter-factory.d.ts +0 -2
  70. package/dist/types/src/socket-adapter.d.ts +0 -9
  71. package/dist/types/src/stream-group.d.ts +0 -13
  72. package/dist/types/src/stream-item.d.ts +0 -9
  73. package/dist/types/src/stream-subscription.d.ts +0 -36
  74. package/dist/types/src/stream.d.ts +0 -38
  75. package/dist/types/src/stream.types.d.ts +0 -56
  76. package/scripts/build.sh +0 -14
@@ -0,0 +1,10 @@
1
+ let counter = 0
2
+
3
+ export const v4 = (): string => {
4
+ counter++
5
+ return `00000000-0000-0000-0000-${counter.toString().padStart(12, '0')}`
6
+ }
7
+
8
+ export default {
9
+ v4,
10
+ }
@@ -0,0 +1,8 @@
1
+ import { SocketAdapter } from "./src/socket-adapter.mjs";
2
+ import { SocketAdapterFactory } from "./src/adapter-factory.mjs";
3
+ import { BaseMessage, CustomEvent, CustomEventListener, GroupEventMessage, GroupStreamEvent, ItemEventMessage, ItemStreamEvent, JoinMessage, Listener, Message, StreamEvent } from "./src/stream.types.mjs";
4
+ import { StreamSubscription } from "./src/stream-subscription.mjs";
5
+ import { StreamGroupSubscription } from "./src/stream-group.mjs";
6
+ import { StreamItemSubscription } from "./src/stream-item.mjs";
7
+ import { Stream } from "./src/stream.mjs";
8
+ export { BaseMessage, CustomEvent, CustomEventListener, GroupEventMessage, GroupStreamEvent, ItemEventMessage, ItemStreamEvent, JoinMessage, Listener, Message, type SocketAdapter, type SocketAdapterFactory, Stream, StreamEvent, StreamGroupSubscription, StreamItemSubscription, StreamSubscription };
package/dist/index.mjs ADDED
@@ -0,0 +1,6 @@
1
+ import { StreamSubscription } from "./src/stream-subscription.mjs";
2
+ import { StreamGroupSubscription } from "./src/stream-group.mjs";
3
+ import { StreamItemSubscription } from "./src/stream-item.mjs";
4
+ import { Stream } from "./src/stream.mjs";
5
+
6
+ export { Stream, StreamGroupSubscription, StreamItemSubscription, StreamSubscription };
@@ -0,0 +1,7 @@
1
+ import { SocketAdapter } from "./socket-adapter.mjs";
2
+
3
+ //#region src/adapter-factory.d.ts
4
+ type SocketAdapterFactory = () => SocketAdapter;
5
+ //#endregion
6
+ export { SocketAdapterFactory };
7
+ //# sourceMappingURL=adapter-factory.d.mts.map
@@ -0,0 +1,13 @@
1
+ //#region src/socket-adapter.d.ts
2
+ interface SocketAdapter {
3
+ connect(): void;
4
+ close(): void;
5
+ send(message: string): void;
6
+ isOpen(): boolean;
7
+ onMessage(callback: (message: string) => void): void;
8
+ onOpen(callback: () => void): void;
9
+ onClose(callback: () => void): void;
10
+ }
11
+ //#endregion
12
+ export { SocketAdapter };
13
+ //# sourceMappingURL=socket-adapter.d.mts.map
@@ -0,0 +1,18 @@
1
+ import { GroupEventMessage, JoinMessage } from "./stream.types.mjs";
2
+ import { StreamSubscription } from "./stream-subscription.mjs";
3
+
4
+ //#region src/stream-group.d.ts
5
+ declare class StreamGroupSubscription<TData extends {
6
+ id: string;
7
+ }> extends StreamSubscription<TData[], GroupEventMessage<TData>> {
8
+ private sortKey?;
9
+ private lastTimestamp;
10
+ private lastTimestampMap;
11
+ constructor(sub: JoinMessage, sortKey?: keyof TData | undefined);
12
+ private sort;
13
+ protected setState(state: TData[]): void;
14
+ listener(message: GroupEventMessage<TData>): void;
15
+ }
16
+ //#endregion
17
+ export { StreamGroupSubscription };
18
+ //# sourceMappingURL=stream-group.d.mts.map
@@ -0,0 +1,55 @@
1
+ import { StreamSubscription } from "./stream-subscription.mjs";
2
+
3
+ //#region src/stream-group.ts
4
+ var StreamGroupSubscription = class extends StreamSubscription {
5
+ constructor(sub, sortKey) {
6
+ super(sub, []);
7
+ this.sortKey = sortKey;
8
+ this.lastTimestamp = 0;
9
+ this.lastTimestampMap = /* @__PURE__ */ new Map();
10
+ }
11
+ sort(state) {
12
+ const sortKey = this.sortKey;
13
+ if (sortKey) return state.sort((a, b) => {
14
+ const aValue = a[sortKey];
15
+ const bValue = b[sortKey];
16
+ if (aValue && bValue) return aValue.toString().localeCompare(bValue.toString());
17
+ return 0;
18
+ });
19
+ return state;
20
+ }
21
+ setState(state) {
22
+ super.setState(this.sort(state));
23
+ }
24
+ listener(message) {
25
+ if (message.event.type === "sync") {
26
+ if (message.timestamp < this.lastTimestamp) return;
27
+ this.lastTimestampMap = /* @__PURE__ */ new Map();
28
+ this.lastTimestamp = message.timestamp;
29
+ this.setState(message.event.data);
30
+ } else if (message.event.type === "create") {
31
+ const id = message.event.data.id;
32
+ const state = this.getState();
33
+ if (!state.find((item) => item.id === id)) this.setState([...state, message.event.data]);
34
+ } else if (message.event.type === "update") {
35
+ const messageData = message.event.data;
36
+ const messageDataId = messageData.id;
37
+ const state = this.getState();
38
+ const currentItemTimestamp = this.lastTimestampMap.get(messageDataId);
39
+ if (currentItemTimestamp && currentItemTimestamp >= message.timestamp) return;
40
+ this.lastTimestamp = message.timestamp;
41
+ this.lastTimestampMap.set(messageDataId, message.timestamp);
42
+ this.setState(state.map((item) => item.id === messageDataId ? messageData : item));
43
+ } else if (message.event.type === "delete") {
44
+ const messageDataId = message.event.data.id;
45
+ const state = this.getState();
46
+ this.lastTimestamp = message.timestamp;
47
+ this.lastTimestampMap.set(messageDataId, message.timestamp);
48
+ this.setState(state.filter((item) => item.id !== messageDataId));
49
+ } else if (message.event.type === "event") this.onEventReceived(message.event.event);
50
+ }
51
+ };
52
+
53
+ //#endregion
54
+ export { StreamGroupSubscription };
55
+ //# sourceMappingURL=stream-group.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"stream-group.mjs","names":["sortKey?: keyof TData"],"sources":["../../src/stream-group.ts"],"sourcesContent":["import type { GroupEventMessage, JoinMessage } from './stream.types'\nimport { StreamSubscription } from './stream-subscription'\n\nexport class StreamGroupSubscription<TData extends { id: string }> extends StreamSubscription<\n TData[],\n GroupEventMessage<TData>\n> {\n private lastTimestamp: number = 0\n private lastTimestampMap: Map<string, number> = new Map()\n\n constructor(\n sub: JoinMessage,\n private sortKey?: keyof TData,\n ) {\n super(sub, [])\n }\n\n private sort(state: TData[]): TData[] {\n const sortKey = this.sortKey\n\n if (sortKey) {\n return state.sort((a: TData, b: TData) => {\n const aValue = a[sortKey]\n const bValue = b[sortKey]\n\n if (aValue && bValue) {\n return aValue.toString().localeCompare(bValue.toString())\n }\n\n return 0\n })\n }\n\n return state\n }\n\n protected setState(state: TData[]): void {\n super.setState(this.sort(state))\n }\n\n listener(message: GroupEventMessage<TData>): void {\n if (message.event.type === 'sync') {\n if (message.timestamp < this.lastTimestamp) {\n return\n }\n\n this.lastTimestampMap = new Map()\n this.lastTimestamp = message.timestamp\n this.setState(message.event.data)\n } else if (message.event.type === 'create') {\n const id = message.event.data.id\n const state = this.getState()\n\n if (!state.find((item) => item.id === id)) {\n this.setState([...state, message.event.data])\n }\n } else if (message.event.type === 'update') {\n const messageData = message.event.data\n const messageDataId = messageData.id\n const state = this.getState()\n const currentItemTimestamp = this.lastTimestampMap.get(messageDataId)\n\n if (currentItemTimestamp && currentItemTimestamp >= message.timestamp) {\n return\n }\n\n this.lastTimestamp = message.timestamp\n this.lastTimestampMap.set(messageDataId, message.timestamp)\n this.setState(state.map((item) => (item.id === messageDataId ? messageData : item)))\n } else if (message.event.type === 'delete') {\n const messageDataId = message.event.data.id\n const state = this.getState()\n\n this.lastTimestamp = message.timestamp\n this.lastTimestampMap.set(messageDataId, message.timestamp)\n this.setState(state.filter((item) => item.id !== messageDataId))\n } else if (message.event.type === 'event') {\n this.onEventReceived(message.event.event)\n }\n }\n}\n"],"mappings":";;;AAGA,IAAa,0BAAb,cAA2E,mBAGzE;CAIA,YACE,KACA,AAAQA,SACR;AACA,QAAM,KAAK,EAAE,CAAC;EAFN;uBALsB;0CACgB,IAAI,KAAK;;CASzD,AAAQ,KAAK,OAAyB;EACpC,MAAM,UAAU,KAAK;AAErB,MAAI,QACF,QAAO,MAAM,MAAM,GAAU,MAAa;GACxC,MAAM,SAAS,EAAE;GACjB,MAAM,SAAS,EAAE;AAEjB,OAAI,UAAU,OACZ,QAAO,OAAO,UAAU,CAAC,cAAc,OAAO,UAAU,CAAC;AAG3D,UAAO;IACP;AAGJ,SAAO;;CAGT,AAAU,SAAS,OAAsB;AACvC,QAAM,SAAS,KAAK,KAAK,MAAM,CAAC;;CAGlC,SAAS,SAAyC;AAChD,MAAI,QAAQ,MAAM,SAAS,QAAQ;AACjC,OAAI,QAAQ,YAAY,KAAK,cAC3B;AAGF,QAAK,mCAAmB,IAAI,KAAK;AACjC,QAAK,gBAAgB,QAAQ;AAC7B,QAAK,SAAS,QAAQ,MAAM,KAAK;aACxB,QAAQ,MAAM,SAAS,UAAU;GAC1C,MAAM,KAAK,QAAQ,MAAM,KAAK;GAC9B,MAAM,QAAQ,KAAK,UAAU;AAE7B,OAAI,CAAC,MAAM,MAAM,SAAS,KAAK,OAAO,GAAG,CACvC,MAAK,SAAS,CAAC,GAAG,OAAO,QAAQ,MAAM,KAAK,CAAC;aAEtC,QAAQ,MAAM,SAAS,UAAU;GAC1C,MAAM,cAAc,QAAQ,MAAM;GAClC,MAAM,gBAAgB,YAAY;GAClC,MAAM,QAAQ,KAAK,UAAU;GAC7B,MAAM,uBAAuB,KAAK,iBAAiB,IAAI,cAAc;AAErE,OAAI,wBAAwB,wBAAwB,QAAQ,UAC1D;AAGF,QAAK,gBAAgB,QAAQ;AAC7B,QAAK,iBAAiB,IAAI,eAAe,QAAQ,UAAU;AAC3D,QAAK,SAAS,MAAM,KAAK,SAAU,KAAK,OAAO,gBAAgB,cAAc,KAAM,CAAC;aAC3E,QAAQ,MAAM,SAAS,UAAU;GAC1C,MAAM,gBAAgB,QAAQ,MAAM,KAAK;GACzC,MAAM,QAAQ,KAAK,UAAU;AAE7B,QAAK,gBAAgB,QAAQ;AAC7B,QAAK,iBAAiB,IAAI,eAAe,QAAQ,UAAU;AAC3D,QAAK,SAAS,MAAM,QAAQ,SAAS,KAAK,OAAO,cAAc,CAAC;aACvD,QAAQ,MAAM,SAAS,QAChC,MAAK,gBAAgB,QAAQ,MAAM,MAAM"}
@@ -0,0 +1,14 @@
1
+ import { ItemEventMessage, JoinMessage } from "./stream.types.mjs";
2
+ import { StreamSubscription } from "./stream-subscription.mjs";
3
+
4
+ //#region src/stream-item.d.ts
5
+ declare class StreamItemSubscription<TData extends {
6
+ id: string;
7
+ }> extends StreamSubscription<TData | null, ItemEventMessage<TData>> {
8
+ private lastEventTimestamp;
9
+ constructor(sub: JoinMessage);
10
+ listener(message: ItemEventMessage<TData>): void;
11
+ }
12
+ //#endregion
13
+ export { StreamItemSubscription };
14
+ //# sourceMappingURL=stream-item.d.mts.map
@@ -0,0 +1,20 @@
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
@@ -0,0 +1 @@
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"}
@@ -0,0 +1,40 @@
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
@@ -0,0 +1,64 @@
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
@@ -0,0 +1 @@
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"}
@@ -0,0 +1,43 @@
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
@@ -0,0 +1,104 @@
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
@@ -0,0 +1 @@
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"}
@@ -0,0 +1,60 @@
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/package.json CHANGED
@@ -1,35 +1,26 @@
1
1
  {
2
2
  "name": "@motiadev/stream-client",
3
3
  "description": "Motia Stream Client Package – Responsible for managing streams of data.",
4
- "version": "0.13.0-beta.161",
4
+ "version": "0.13.0-beta.162-439402",
5
5
  "license": "MIT",
6
- "main": "dist/cjs/index.js",
7
- "module": "dist/esm/index.js",
8
- "types": "dist/types/index.d.ts",
9
- "exports": {
10
- ".": {
11
- "require": "./dist/cjs/index.js",
12
- "import": "./dist/esm/index.js",
13
- "types": "./dist/types/index.d.ts"
14
- },
15
- "./workbench": {
16
- "require": "./dist/cjs/workbench.js",
17
- "import": "./dist/esm/workbench.js",
18
- "types": "./dist/types/workbench.d.ts"
19
- }
20
- },
6
+ "type": "module",
7
+ "main": "dist/index.mjs",
8
+ "module": "dist/index.mjs",
9
+ "types": "dist/index.d.mts",
21
10
  "dependencies": {
22
- "uuid": "^11.1.0"
11
+ "uuid": "^13.0.0"
23
12
  },
24
13
  "devDependencies": {
25
- "@types/jest": "^29.5.14",
14
+ "@types/jest": "^30.0.0",
26
15
  "@types/ws": "^8.18.1",
27
- "jest": "^29.7.0",
28
- "ts-jest": "^29.3.2",
29
- "typescript": "^5.7.2"
16
+ "jest": "^30.2.0",
17
+ "ts-jest": "^29.4.5",
18
+ "tsdown": "^0.16.6",
19
+ "typescript": "^5.9.3"
30
20
  },
31
21
  "scripts": {
32
- "build": "sh scripts/build.sh",
22
+ "build": "tsdown",
23
+ "dev": "tsdown --watch",
33
24
  "lint": "eslint --config ../../eslint.config.js",
34
25
  "test": "jest"
35
26
  }
@@ -0,0 +1,18 @@
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
+ })
@@ -1 +0,0 @@
1
- export {};