@motiadev/stream-client 0.13.0-beta.162-850424 → 0.13.0-beta.162-717198

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 (77) hide show
  1. package/LICENSE +93 -21
  2. package/dist/cjs/__tests__/stream-group.spec.d.ts +1 -0
  3. package/dist/cjs/__tests__/stream-group.spec.js +107 -0
  4. package/dist/cjs/__tests__/stream-item.spec.d.ts +1 -0
  5. package/dist/cjs/__tests__/stream-item.spec.js +48 -0
  6. package/dist/cjs/__tests__/stream.spec.d.ts +1 -0
  7. package/dist/cjs/__tests__/stream.spec.js +83 -0
  8. package/dist/cjs/index.d.ts +7 -0
  9. package/dist/cjs/index.js +26 -0
  10. package/dist/cjs/src/adapter-factory.d.ts +2 -0
  11. package/dist/cjs/src/adapter-factory.js +2 -0
  12. package/dist/cjs/src/socket-adapter.d.ts +9 -0
  13. package/dist/cjs/src/socket-adapter.js +2 -0
  14. package/dist/cjs/src/stream-group.d.ts +13 -0
  15. package/dist/cjs/src/stream-group.js +69 -0
  16. package/dist/cjs/src/stream-item.d.ts +9 -0
  17. package/dist/cjs/src/stream-item.js +26 -0
  18. package/dist/cjs/src/stream-subscription.d.ts +36 -0
  19. package/dist/cjs/src/stream-subscription.js +63 -0
  20. package/dist/cjs/src/stream.d.ts +38 -0
  21. package/dist/cjs/src/stream.js +102 -0
  22. package/dist/cjs/src/stream.types.d.ts +56 -0
  23. package/dist/cjs/src/stream.types.js +2 -0
  24. package/dist/esm/__tests__/stream-group.spec.d.ts +1 -0
  25. package/dist/esm/__tests__/stream-group.spec.js +105 -0
  26. package/dist/esm/__tests__/stream-item.spec.d.ts +1 -0
  27. package/dist/esm/__tests__/stream-item.spec.js +46 -0
  28. package/dist/esm/__tests__/stream.spec.d.ts +1 -0
  29. package/dist/esm/__tests__/stream.spec.js +81 -0
  30. package/dist/esm/index.d.ts +7 -0
  31. package/dist/esm/index.js +5 -0
  32. package/dist/esm/src/adapter-factory.d.ts +2 -0
  33. package/dist/esm/src/adapter-factory.js +1 -0
  34. package/dist/esm/src/socket-adapter.d.ts +9 -0
  35. package/dist/esm/src/socket-adapter.js +1 -0
  36. package/dist/esm/src/stream-group.d.ts +13 -0
  37. package/dist/esm/src/stream-group.js +65 -0
  38. package/dist/esm/src/stream-item.d.ts +9 -0
  39. package/dist/esm/src/stream-item.js +22 -0
  40. package/dist/esm/src/stream-subscription.d.ts +36 -0
  41. package/dist/esm/src/stream-subscription.js +59 -0
  42. package/dist/esm/src/stream.d.ts +38 -0
  43. package/dist/esm/src/stream.js +98 -0
  44. package/dist/esm/src/stream.types.d.ts +56 -0
  45. package/dist/esm/src/stream.types.js +1 -0
  46. package/dist/types/__tests__/stream-group.spec.d.ts +1 -0
  47. package/dist/types/__tests__/stream-item.spec.d.ts +1 -0
  48. package/dist/types/__tests__/stream.spec.d.ts +1 -0
  49. package/dist/types/index.d.ts +7 -0
  50. package/dist/types/src/adapter-factory.d.ts +2 -0
  51. package/dist/types/src/socket-adapter.d.ts +9 -0
  52. package/dist/types/src/stream-group.d.ts +13 -0
  53. package/dist/types/src/stream-item.d.ts +9 -0
  54. package/dist/types/src/stream-subscription.d.ts +36 -0
  55. package/dist/types/src/stream.d.ts +38 -0
  56. package/dist/types/src/stream.types.d.ts +56 -0
  57. package/package.json +23 -14
  58. package/scripts/build.sh +14 -0
  59. package/__mocks__/uuid.ts +0 -10
  60. package/dist/index.d.mts +0 -8
  61. package/dist/index.mjs +0 -6
  62. package/dist/src/adapter-factory.d.mts +0 -7
  63. package/dist/src/socket-adapter.d.mts +0 -13
  64. package/dist/src/stream-group.d.mts +0 -18
  65. package/dist/src/stream-group.mjs +0 -55
  66. package/dist/src/stream-group.mjs.map +0 -1
  67. package/dist/src/stream-item.d.mts +0 -14
  68. package/dist/src/stream-item.mjs +0 -20
  69. package/dist/src/stream-item.mjs.map +0 -1
  70. package/dist/src/stream-subscription.d.mts +0 -40
  71. package/dist/src/stream-subscription.mjs +0 -64
  72. package/dist/src/stream-subscription.mjs.map +0 -1
  73. package/dist/src/stream.d.mts +0 -43
  74. package/dist/src/stream.mjs +0 -104
  75. package/dist/src/stream.mjs.map +0 -1
  76. package/dist/src/stream.types.d.mts +0 -60
  77. package/tsdown.config.ts +0 -18
@@ -0,0 +1,59 @@
1
+ export class StreamSubscription {
2
+ constructor(sub, state) {
3
+ this.customEventListeners = new Map();
4
+ this.closeListeners = new Set();
5
+ this.onChangeListeners = new Set();
6
+ this.sub = sub;
7
+ this.state = state;
8
+ }
9
+ onEventReceived(event) {
10
+ const customEventListeners = this.customEventListeners.get(event.type);
11
+ if (customEventListeners) {
12
+ const eventData = event.data;
13
+ customEventListeners.forEach((listener) => listener(eventData));
14
+ }
15
+ }
16
+ /**
17
+ * Add a custom event listener. This listener will be called whenever the custom event is received.
18
+ */
19
+ onEvent(type, listener) {
20
+ const listeners = this.customEventListeners.get(type) || [];
21
+ this.customEventListeners.set(type, [...listeners, listener]);
22
+ }
23
+ /**
24
+ * Remove a custom event listener.
25
+ */
26
+ offEvent(type, listener) {
27
+ const listeners = this.customEventListeners.get(type) || [];
28
+ this.customEventListeners.set(type, listeners.filter((l) => l !== listener));
29
+ }
30
+ onClose(listener) {
31
+ this.closeListeners.add(listener);
32
+ }
33
+ close() {
34
+ this.closeListeners.forEach((listener) => listener());
35
+ this.closeListeners.clear();
36
+ }
37
+ /**
38
+ * Add a change listener. This listener will be called whenever the state of the group changes.
39
+ */
40
+ addChangeListener(listener) {
41
+ this.onChangeListeners.add(listener);
42
+ }
43
+ /**
44
+ * Remove a change listener.
45
+ */
46
+ removeChangeListener(listener) {
47
+ this.onChangeListeners.delete(listener);
48
+ }
49
+ /**
50
+ * Get the current state of the group.
51
+ */
52
+ getState() {
53
+ return this.state;
54
+ }
55
+ setState(state) {
56
+ this.state = state;
57
+ this.onChangeListeners.forEach((listener) => listener(state));
58
+ }
59
+ }
@@ -0,0 +1,38 @@
1
+ import type { SocketAdapterFactory } from './adapter-factory';
2
+ import type { SocketAdapter } from './socket-adapter';
3
+ import { StreamGroupSubscription } from './stream-group';
4
+ import { StreamItemSubscription } from './stream-item';
5
+ export declare class Stream {
6
+ private adapterFactory;
7
+ private ws;
8
+ private listeners;
9
+ constructor(adapterFactory: SocketAdapterFactory);
10
+ createSocket(): SocketAdapter;
11
+ /**
12
+ * Subscribe to an item in a stream.
13
+ *
14
+ * @argument streamName - The name of the stream to subscribe to.
15
+ * @argument groupId - The id of the group to subscribe to.
16
+ * @argument id - The id of the item to subscribe to.
17
+ */
18
+ subscribeItem<TData extends {
19
+ id: string;
20
+ }>(streamName: string, groupId: string, id: string): StreamItemSubscription<TData>;
21
+ /**
22
+ * Subscribe to a group in a stream.
23
+ *
24
+ * @argument streamName - The name of the stream to subscribe to.
25
+ * @argument groupId - The id of the group to subscribe to.
26
+ */
27
+ subscribeGroup<TData extends {
28
+ id: string;
29
+ }>(streamName: string, groupId: string, sortKey?: keyof TData): StreamGroupSubscription<TData>;
30
+ close(): void;
31
+ private onSocketClose;
32
+ private onSocketOpen;
33
+ messageListener(event: string): void;
34
+ private subscribe;
35
+ private join;
36
+ private leave;
37
+ private roomName;
38
+ }
@@ -0,0 +1,98 @@
1
+ import { v4 as uuidv4 } from 'uuid';
2
+ import { StreamGroupSubscription } from './stream-group';
3
+ import { StreamItemSubscription } from './stream-item';
4
+ export class Stream {
5
+ constructor(adapterFactory) {
6
+ this.adapterFactory = adapterFactory;
7
+ this.listeners = {};
8
+ this.ws = this.createSocket();
9
+ }
10
+ createSocket() {
11
+ this.ws = this.adapterFactory();
12
+ this.ws.onMessage((message) => this.messageListener(message));
13
+ this.ws.onOpen(() => this.onSocketOpen());
14
+ this.ws.onClose(() => this.onSocketClose());
15
+ return this.ws;
16
+ }
17
+ /**
18
+ * Subscribe to an item in a stream.
19
+ *
20
+ * @argument streamName - The name of the stream to subscribe to.
21
+ * @argument groupId - The id of the group to subscribe to.
22
+ * @argument id - The id of the item to subscribe to.
23
+ */
24
+ subscribeItem(streamName, groupId, id) {
25
+ const subscriptionId = uuidv4();
26
+ const sub = { streamName, groupId, id, subscriptionId };
27
+ const subscription = new StreamItemSubscription(sub);
28
+ this.subscribe(subscription);
29
+ return subscription;
30
+ }
31
+ /**
32
+ * Subscribe to a group in a stream.
33
+ *
34
+ * @argument streamName - The name of the stream to subscribe to.
35
+ * @argument groupId - The id of the group to subscribe to.
36
+ */
37
+ subscribeGroup(streamName, groupId, sortKey) {
38
+ const subscriptionId = uuidv4();
39
+ const sub = { streamName, groupId, subscriptionId };
40
+ const subscription = new StreamGroupSubscription(sub, sortKey);
41
+ this.subscribe(subscription);
42
+ return subscription;
43
+ }
44
+ close() {
45
+ this.listeners = {}; // clean up all listeners
46
+ this.ws.close();
47
+ }
48
+ onSocketClose() {
49
+ // retry to connect
50
+ setTimeout(() => this.createSocket(), 2000);
51
+ }
52
+ onSocketOpen() {
53
+ Object.values(this.listeners).forEach((listeners) => {
54
+ listeners.forEach((subscription) => this.join(subscription));
55
+ });
56
+ }
57
+ messageListener(event) {
58
+ const message = JSON.parse(event);
59
+ const room = this.roomName(message);
60
+ this.listeners[room]?.forEach((listener) => listener.listener(message));
61
+ // we need to discard sync to group subs when it's an item event
62
+ if (message.id && message.event.type !== 'sync') {
63
+ const groupRoom = this.roomName({
64
+ streamName: message.streamName,
65
+ groupId: message.groupId,
66
+ });
67
+ this.listeners[groupRoom]?.forEach((listener) => listener.listener(message));
68
+ }
69
+ }
70
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
71
+ subscribe(subscription) {
72
+ const room = this.roomName(subscription.sub);
73
+ if (!this.listeners[room]) {
74
+ this.listeners[room] = new Set();
75
+ }
76
+ this.listeners[room].add(subscription);
77
+ this.join(subscription);
78
+ subscription.onClose(() => {
79
+ this.listeners[room]?.delete(subscription);
80
+ this.leave(subscription);
81
+ });
82
+ }
83
+ join(subscription) {
84
+ if (this.ws.isOpen()) {
85
+ this.ws.send(JSON.stringify({ type: 'join', data: subscription.sub }));
86
+ }
87
+ }
88
+ leave(subscription) {
89
+ if (this.ws.isOpen()) {
90
+ this.ws.send(JSON.stringify({ type: 'leave', data: subscription.sub }));
91
+ }
92
+ }
93
+ roomName(message) {
94
+ return message.id
95
+ ? `${message.streamName}:group:${message.groupId}:item:${message.id}`
96
+ : `${message.streamName}:group:${message.groupId}`;
97
+ }
98
+ }
@@ -0,0 +1,56 @@
1
+ export type BaseMessage = {
2
+ streamName: string;
3
+ groupId: string;
4
+ id?: string;
5
+ timestamp: number;
6
+ };
7
+ export type JoinMessage = Omit<BaseMessage, 'timestamp'> & {
8
+ subscriptionId: string;
9
+ };
10
+ export type CustomEvent = {
11
+ type: string;
12
+ data: any;
13
+ };
14
+ export type StreamEvent<TData extends {
15
+ id: string;
16
+ }> = {
17
+ type: 'create';
18
+ data: TData;
19
+ } | {
20
+ type: 'update';
21
+ data: TData;
22
+ } | {
23
+ type: 'delete';
24
+ data: TData;
25
+ } | {
26
+ type: 'event';
27
+ event: CustomEvent;
28
+ };
29
+ export type ItemStreamEvent<TData extends {
30
+ id: string;
31
+ }> = StreamEvent<TData> | {
32
+ type: 'sync';
33
+ data: TData;
34
+ };
35
+ export type GroupStreamEvent<TData extends {
36
+ id: string;
37
+ }> = StreamEvent<TData> | {
38
+ type: 'sync';
39
+ data: TData[];
40
+ };
41
+ export type ItemEventMessage<TData extends {
42
+ id: string;
43
+ }> = BaseMessage & {
44
+ event: ItemStreamEvent<TData>;
45
+ };
46
+ export type GroupEventMessage<TData extends {
47
+ id: string;
48
+ }> = BaseMessage & {
49
+ event: GroupStreamEvent<TData>;
50
+ };
51
+ export type Message = {
52
+ type: 'join' | 'leave';
53
+ data: JoinMessage;
54
+ };
55
+ export type Listener<TData> = (state: TData | null) => void;
56
+ export type CustomEventListener<TData> = (event: TData) => void;
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,7 @@
1
+ export { SocketAdapterFactory } from './src/adapter-factory';
2
+ export { SocketAdapter } from './src/socket-adapter';
3
+ export { Stream } from './src/stream';
4
+ export * from './src/stream.types';
5
+ export { StreamGroupSubscription } from './src/stream-group';
6
+ export { StreamItemSubscription } from './src/stream-item';
7
+ export { StreamSubscription } from './src/stream-subscription';
@@ -0,0 +1,2 @@
1
+ import type { SocketAdapter } from './socket-adapter';
2
+ export type SocketAdapterFactory = () => SocketAdapter;
@@ -0,0 +1,9 @@
1
+ export interface SocketAdapter {
2
+ connect(): void;
3
+ close(): void;
4
+ send(message: string): void;
5
+ isOpen(): boolean;
6
+ onMessage(callback: (message: string) => void): void;
7
+ onOpen(callback: () => void): void;
8
+ onClose(callback: () => void): void;
9
+ }
@@ -0,0 +1,13 @@
1
+ import type { GroupEventMessage, JoinMessage } from './stream.types';
2
+ import { StreamSubscription } from './stream-subscription';
3
+ export declare class StreamGroupSubscription<TData extends {
4
+ id: string;
5
+ }> extends StreamSubscription<TData[], GroupEventMessage<TData>> {
6
+ private sortKey?;
7
+ private lastTimestamp;
8
+ private lastTimestampMap;
9
+ constructor(sub: JoinMessage, sortKey?: keyof TData | undefined);
10
+ private sort;
11
+ protected setState(state: TData[]): void;
12
+ listener(message: GroupEventMessage<TData>): void;
13
+ }
@@ -0,0 +1,9 @@
1
+ import type { ItemEventMessage, JoinMessage } from './stream.types';
2
+ import { StreamSubscription } from './stream-subscription';
3
+ export declare class StreamItemSubscription<TData extends {
4
+ id: string;
5
+ }> extends StreamSubscription<TData | null, ItemEventMessage<TData>> {
6
+ private lastEventTimestamp;
7
+ constructor(sub: JoinMessage);
8
+ listener(message: ItemEventMessage<TData>): void;
9
+ }
@@ -0,0 +1,36 @@
1
+ import type { CustomEvent, JoinMessage, Listener } from './stream.types';
2
+ type CustomEventListener = (event: any) => void;
3
+ export declare abstract class StreamSubscription<TData = unknown, TEventData = unknown> {
4
+ private customEventListeners;
5
+ private closeListeners;
6
+ private onChangeListeners;
7
+ private state;
8
+ readonly sub: JoinMessage;
9
+ constructor(sub: JoinMessage, state: TData);
10
+ abstract listener(message: TEventData): void;
11
+ protected onEventReceived(event: CustomEvent): void;
12
+ /**
13
+ * Add a custom event listener. This listener will be called whenever the custom event is received.
14
+ */
15
+ onEvent(type: string, listener: CustomEventListener): void;
16
+ /**
17
+ * Remove a custom event listener.
18
+ */
19
+ offEvent(type: string, listener: CustomEventListener): void;
20
+ onClose(listener: () => void): void;
21
+ close(): void;
22
+ /**
23
+ * Add a change listener. This listener will be called whenever the state of the group changes.
24
+ */
25
+ addChangeListener(listener: Listener<TData>): void;
26
+ /**
27
+ * Remove a change listener.
28
+ */
29
+ removeChangeListener(listener: Listener<TData>): void;
30
+ /**
31
+ * Get the current state of the group.
32
+ */
33
+ getState(): TData;
34
+ protected setState(state: TData): void;
35
+ }
36
+ export {};
@@ -0,0 +1,38 @@
1
+ import type { SocketAdapterFactory } from './adapter-factory';
2
+ import type { SocketAdapter } from './socket-adapter';
3
+ import { StreamGroupSubscription } from './stream-group';
4
+ import { StreamItemSubscription } from './stream-item';
5
+ export declare class Stream {
6
+ private adapterFactory;
7
+ private ws;
8
+ private listeners;
9
+ constructor(adapterFactory: SocketAdapterFactory);
10
+ createSocket(): SocketAdapter;
11
+ /**
12
+ * Subscribe to an item in a stream.
13
+ *
14
+ * @argument streamName - The name of the stream to subscribe to.
15
+ * @argument groupId - The id of the group to subscribe to.
16
+ * @argument id - The id of the item to subscribe to.
17
+ */
18
+ subscribeItem<TData extends {
19
+ id: string;
20
+ }>(streamName: string, groupId: string, id: string): StreamItemSubscription<TData>;
21
+ /**
22
+ * Subscribe to a group in a stream.
23
+ *
24
+ * @argument streamName - The name of the stream to subscribe to.
25
+ * @argument groupId - The id of the group to subscribe to.
26
+ */
27
+ subscribeGroup<TData extends {
28
+ id: string;
29
+ }>(streamName: string, groupId: string, sortKey?: keyof TData): StreamGroupSubscription<TData>;
30
+ close(): void;
31
+ private onSocketClose;
32
+ private onSocketOpen;
33
+ messageListener(event: string): void;
34
+ private subscribe;
35
+ private join;
36
+ private leave;
37
+ private roomName;
38
+ }
@@ -0,0 +1,56 @@
1
+ export type BaseMessage = {
2
+ streamName: string;
3
+ groupId: string;
4
+ id?: string;
5
+ timestamp: number;
6
+ };
7
+ export type JoinMessage = Omit<BaseMessage, 'timestamp'> & {
8
+ subscriptionId: string;
9
+ };
10
+ export type CustomEvent = {
11
+ type: string;
12
+ data: any;
13
+ };
14
+ export type StreamEvent<TData extends {
15
+ id: string;
16
+ }> = {
17
+ type: 'create';
18
+ data: TData;
19
+ } | {
20
+ type: 'update';
21
+ data: TData;
22
+ } | {
23
+ type: 'delete';
24
+ data: TData;
25
+ } | {
26
+ type: 'event';
27
+ event: CustomEvent;
28
+ };
29
+ export type ItemStreamEvent<TData extends {
30
+ id: string;
31
+ }> = StreamEvent<TData> | {
32
+ type: 'sync';
33
+ data: TData;
34
+ };
35
+ export type GroupStreamEvent<TData extends {
36
+ id: string;
37
+ }> = StreamEvent<TData> | {
38
+ type: 'sync';
39
+ data: TData[];
40
+ };
41
+ export type ItemEventMessage<TData extends {
42
+ id: string;
43
+ }> = BaseMessage & {
44
+ event: ItemStreamEvent<TData>;
45
+ };
46
+ export type GroupEventMessage<TData extends {
47
+ id: string;
48
+ }> = BaseMessage & {
49
+ event: GroupStreamEvent<TData>;
50
+ };
51
+ export type Message = {
52
+ type: 'join' | 'leave';
53
+ data: JoinMessage;
54
+ };
55
+ export type Listener<TData> = (state: TData | null) => void;
56
+ export type CustomEventListener<TData> = (event: TData) => void;
package/package.json CHANGED
@@ -1,26 +1,35 @@
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.162-850424",
5
- "license": "MIT",
6
- "type": "module",
7
- "main": "dist/index.mjs",
8
- "module": "dist/index.mjs",
9
- "types": "dist/index.d.mts",
4
+ "version": "0.13.0-beta.162-717198",
5
+ "license": "Elastic-2.0",
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
+ },
10
21
  "dependencies": {
11
- "uuid": "^13.0.0"
22
+ "uuid": "^11.1.0"
12
23
  },
13
24
  "devDependencies": {
14
- "@types/jest": "^30.0.0",
25
+ "@types/jest": "^29.5.14",
15
26
  "@types/ws": "^8.18.1",
16
- "jest": "^30.2.0",
17
- "ts-jest": "^29.4.5",
18
- "tsdown": "^0.16.6",
19
- "typescript": "^5.9.3"
27
+ "jest": "^29.7.0",
28
+ "ts-jest": "^29.3.2",
29
+ "typescript": "^5.7.2"
20
30
  },
21
31
  "scripts": {
22
- "build": "tsdown",
23
- "dev": "tsdown --watch",
32
+ "build": "sh scripts/build.sh",
24
33
  "lint": "eslint --config ../../eslint.config.js",
25
34
  "test": "jest"
26
35
  }
@@ -0,0 +1,14 @@
1
+ #!/bin/bash
2
+
3
+ rm -rf dist
4
+
5
+ echo "Building CommonJS version..."
6
+ npx tsc --project tsconfig.json --outDir dist/cjs --module CommonJS
7
+
8
+ echo "Building ESM version..."
9
+ npx tsc --project tsconfig.json --outDir dist/esm --module ES2020
10
+
11
+ echo "Building type declarations..."
12
+ npx tsc --emitDeclarationOnly --declaration --outDir dist/types
13
+
14
+ echo "Build completed successfully!"
package/__mocks__/uuid.ts DELETED
@@ -1,10 +0,0 @@
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
- }
package/dist/index.d.mts DELETED
@@ -1,8 +0,0 @@
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 DELETED
@@ -1,6 +0,0 @@
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 };
@@ -1,7 +0,0 @@
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
@@ -1,13 +0,0 @@
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
@@ -1,18 +0,0 @@
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
@@ -1,55 +0,0 @@
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
@@ -1 +0,0 @@
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"}
@@ -1,14 +0,0 @@
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