@motiadev/stream-client 0.13.2-beta.163 → 0.13.2-beta.164-562802
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/__mocks__/uuid.ts +10 -0
- package/dist/index.d.ts +167 -0
- package/dist/index.js +230 -0
- package/package.json +17 -22
- package/tsdown.config.ts +18 -0
- package/dist/cjs/__tests__/stream-group.spec.d.ts +0 -1
- package/dist/cjs/__tests__/stream-group.spec.js +0 -107
- package/dist/cjs/__tests__/stream-item.spec.d.ts +0 -1
- package/dist/cjs/__tests__/stream-item.spec.js +0 -48
- package/dist/cjs/__tests__/stream.spec.d.ts +0 -1
- package/dist/cjs/__tests__/stream.spec.js +0 -83
- package/dist/cjs/index.d.ts +0 -7
- package/dist/cjs/index.js +0 -26
- package/dist/cjs/src/adapter-factory.d.ts +0 -2
- package/dist/cjs/src/adapter-factory.js +0 -2
- package/dist/cjs/src/socket-adapter.d.ts +0 -9
- package/dist/cjs/src/socket-adapter.js +0 -2
- package/dist/cjs/src/stream-group.d.ts +0 -13
- package/dist/cjs/src/stream-group.js +0 -69
- package/dist/cjs/src/stream-item.d.ts +0 -9
- package/dist/cjs/src/stream-item.js +0 -26
- package/dist/cjs/src/stream-subscription.d.ts +0 -36
- package/dist/cjs/src/stream-subscription.js +0 -63
- package/dist/cjs/src/stream.d.ts +0 -38
- package/dist/cjs/src/stream.js +0 -102
- package/dist/cjs/src/stream.types.d.ts +0 -56
- package/dist/cjs/src/stream.types.js +0 -2
- package/dist/esm/__tests__/stream-group.spec.d.ts +0 -1
- package/dist/esm/__tests__/stream-group.spec.js +0 -105
- package/dist/esm/__tests__/stream-item.spec.d.ts +0 -1
- package/dist/esm/__tests__/stream-item.spec.js +0 -46
- package/dist/esm/__tests__/stream.spec.d.ts +0 -1
- package/dist/esm/__tests__/stream.spec.js +0 -81
- package/dist/esm/index.d.ts +0 -7
- package/dist/esm/index.js +0 -5
- package/dist/esm/src/adapter-factory.d.ts +0 -2
- package/dist/esm/src/adapter-factory.js +0 -1
- package/dist/esm/src/socket-adapter.d.ts +0 -9
- package/dist/esm/src/socket-adapter.js +0 -1
- package/dist/esm/src/stream-group.d.ts +0 -13
- package/dist/esm/src/stream-group.js +0 -65
- package/dist/esm/src/stream-item.d.ts +0 -9
- package/dist/esm/src/stream-item.js +0 -22
- package/dist/esm/src/stream-subscription.d.ts +0 -36
- package/dist/esm/src/stream-subscription.js +0 -59
- package/dist/esm/src/stream.d.ts +0 -38
- package/dist/esm/src/stream.js +0 -98
- package/dist/esm/src/stream.types.d.ts +0 -56
- package/dist/esm/src/stream.types.js +0 -1
- package/dist/types/__tests__/stream-group.spec.d.ts +0 -1
- package/dist/types/__tests__/stream-item.spec.d.ts +0 -1
- package/dist/types/__tests__/stream.spec.d.ts +0 -1
- package/dist/types/index.d.ts +0 -7
- package/dist/types/src/adapter-factory.d.ts +0 -2
- package/dist/types/src/socket-adapter.d.ts +0 -9
- package/dist/types/src/stream-group.d.ts +0 -13
- package/dist/types/src/stream-item.d.ts +0 -9
- package/dist/types/src/stream-subscription.d.ts +0 -36
- package/dist/types/src/stream.d.ts +0 -38
- package/dist/types/src/stream.types.d.ts +0 -56
- package/scripts/build.sh +0 -14
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,167 @@
|
|
|
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
|
+
//#region src/adapter-factory.d.ts
|
|
13
|
+
type SocketAdapterFactory = () => SocketAdapter;
|
|
14
|
+
//#endregion
|
|
15
|
+
//#region src/stream.types.d.ts
|
|
16
|
+
type BaseMessage = {
|
|
17
|
+
streamName: string;
|
|
18
|
+
groupId: string;
|
|
19
|
+
id?: string;
|
|
20
|
+
timestamp: number;
|
|
21
|
+
};
|
|
22
|
+
type JoinMessage = Omit<BaseMessage, 'timestamp'> & {
|
|
23
|
+
subscriptionId: string;
|
|
24
|
+
};
|
|
25
|
+
type CustomEvent = {
|
|
26
|
+
type: string;
|
|
27
|
+
data: any;
|
|
28
|
+
};
|
|
29
|
+
type StreamEvent<TData extends {
|
|
30
|
+
id: string;
|
|
31
|
+
}> = {
|
|
32
|
+
type: 'create';
|
|
33
|
+
data: TData;
|
|
34
|
+
} | {
|
|
35
|
+
type: 'update';
|
|
36
|
+
data: TData;
|
|
37
|
+
} | {
|
|
38
|
+
type: 'delete';
|
|
39
|
+
data: TData;
|
|
40
|
+
} | {
|
|
41
|
+
type: 'event';
|
|
42
|
+
event: CustomEvent;
|
|
43
|
+
};
|
|
44
|
+
type ItemStreamEvent<TData extends {
|
|
45
|
+
id: string;
|
|
46
|
+
}> = StreamEvent<TData> | {
|
|
47
|
+
type: 'sync';
|
|
48
|
+
data: TData;
|
|
49
|
+
};
|
|
50
|
+
type GroupStreamEvent<TData extends {
|
|
51
|
+
id: string;
|
|
52
|
+
}> = StreamEvent<TData> | {
|
|
53
|
+
type: 'sync';
|
|
54
|
+
data: TData[];
|
|
55
|
+
};
|
|
56
|
+
type ItemEventMessage<TData extends {
|
|
57
|
+
id: string;
|
|
58
|
+
}> = BaseMessage & {
|
|
59
|
+
event: ItemStreamEvent<TData>;
|
|
60
|
+
};
|
|
61
|
+
type GroupEventMessage<TData extends {
|
|
62
|
+
id: string;
|
|
63
|
+
}> = BaseMessage & {
|
|
64
|
+
event: GroupStreamEvent<TData>;
|
|
65
|
+
};
|
|
66
|
+
type Message = {
|
|
67
|
+
type: 'join' | 'leave';
|
|
68
|
+
data: JoinMessage;
|
|
69
|
+
};
|
|
70
|
+
type Listener<TData> = (state: TData | null) => void;
|
|
71
|
+
type CustomEventListener<TData> = (event: TData) => void;
|
|
72
|
+
//#endregion
|
|
73
|
+
//#region src/stream-subscription.d.ts
|
|
74
|
+
type CustomEventListener$1 = (event: any) => void;
|
|
75
|
+
declare abstract class StreamSubscription<TData = unknown, TEventData = unknown> {
|
|
76
|
+
private customEventListeners;
|
|
77
|
+
private closeListeners;
|
|
78
|
+
private onChangeListeners;
|
|
79
|
+
private state;
|
|
80
|
+
readonly sub: JoinMessage;
|
|
81
|
+
constructor(sub: JoinMessage, state: TData);
|
|
82
|
+
abstract listener(message: TEventData): void;
|
|
83
|
+
protected onEventReceived(event: CustomEvent): void;
|
|
84
|
+
/**
|
|
85
|
+
* Add a custom event listener. This listener will be called whenever the custom event is received.
|
|
86
|
+
*/
|
|
87
|
+
onEvent(type: string, listener: CustomEventListener$1): void;
|
|
88
|
+
/**
|
|
89
|
+
* Remove a custom event listener.
|
|
90
|
+
*/
|
|
91
|
+
offEvent(type: string, listener: CustomEventListener$1): void;
|
|
92
|
+
onClose(listener: () => void): void;
|
|
93
|
+
close(): void;
|
|
94
|
+
/**
|
|
95
|
+
* Add a change listener. This listener will be called whenever the state of the group changes.
|
|
96
|
+
*/
|
|
97
|
+
addChangeListener(listener: Listener<TData>): void;
|
|
98
|
+
/**
|
|
99
|
+
* Remove a change listener.
|
|
100
|
+
*/
|
|
101
|
+
removeChangeListener(listener: Listener<TData>): void;
|
|
102
|
+
/**
|
|
103
|
+
* Get the current state of the group.
|
|
104
|
+
*/
|
|
105
|
+
getState(): TData;
|
|
106
|
+
protected setState(state: TData): void;
|
|
107
|
+
}
|
|
108
|
+
//#endregion
|
|
109
|
+
//#region src/stream-group.d.ts
|
|
110
|
+
declare class StreamGroupSubscription<TData extends {
|
|
111
|
+
id: string;
|
|
112
|
+
}> extends StreamSubscription<TData[], GroupEventMessage<TData>> {
|
|
113
|
+
private sortKey?;
|
|
114
|
+
private lastTimestamp;
|
|
115
|
+
private lastTimestampMap;
|
|
116
|
+
constructor(sub: JoinMessage, sortKey?: keyof TData | undefined);
|
|
117
|
+
private sort;
|
|
118
|
+
protected setState(state: TData[]): void;
|
|
119
|
+
listener(message: GroupEventMessage<TData>): void;
|
|
120
|
+
}
|
|
121
|
+
//#endregion
|
|
122
|
+
//#region src/stream-item.d.ts
|
|
123
|
+
declare class StreamItemSubscription<TData extends {
|
|
124
|
+
id: string;
|
|
125
|
+
}> extends StreamSubscription<TData | null, ItemEventMessage<TData>> {
|
|
126
|
+
private lastEventTimestamp;
|
|
127
|
+
constructor(sub: JoinMessage);
|
|
128
|
+
listener(message: ItemEventMessage<TData>): void;
|
|
129
|
+
}
|
|
130
|
+
//#endregion
|
|
131
|
+
//#region src/stream.d.ts
|
|
132
|
+
declare class Stream {
|
|
133
|
+
private adapterFactory;
|
|
134
|
+
private ws;
|
|
135
|
+
private listeners;
|
|
136
|
+
constructor(adapterFactory: SocketAdapterFactory);
|
|
137
|
+
createSocket(): SocketAdapter;
|
|
138
|
+
/**
|
|
139
|
+
* Subscribe to an item in a stream.
|
|
140
|
+
*
|
|
141
|
+
* @argument streamName - The name of the stream to subscribe to.
|
|
142
|
+
* @argument groupId - The id of the group to subscribe to.
|
|
143
|
+
* @argument id - The id of the item to subscribe to.
|
|
144
|
+
*/
|
|
145
|
+
subscribeItem<TData extends {
|
|
146
|
+
id: string;
|
|
147
|
+
}>(streamName: string, groupId: string, id: string): StreamItemSubscription<TData>;
|
|
148
|
+
/**
|
|
149
|
+
* Subscribe to a group in a stream.
|
|
150
|
+
*
|
|
151
|
+
* @argument streamName - The name of the stream to subscribe to.
|
|
152
|
+
* @argument groupId - The id of the group to subscribe to.
|
|
153
|
+
*/
|
|
154
|
+
subscribeGroup<TData extends {
|
|
155
|
+
id: string;
|
|
156
|
+
}>(streamName: string, groupId: string, sortKey?: keyof TData): StreamGroupSubscription<TData>;
|
|
157
|
+
close(): void;
|
|
158
|
+
private onSocketClose;
|
|
159
|
+
private onSocketOpen;
|
|
160
|
+
messageListener(event: string): void;
|
|
161
|
+
private subscribe;
|
|
162
|
+
private join;
|
|
163
|
+
private leave;
|
|
164
|
+
private roomName;
|
|
165
|
+
}
|
|
166
|
+
//#endregion
|
|
167
|
+
export { BaseMessage, CustomEvent, CustomEventListener, GroupEventMessage, GroupStreamEvent, ItemEventMessage, ItemStreamEvent, JoinMessage, Listener, Message, type SocketAdapter, type SocketAdapterFactory, Stream, StreamEvent, StreamGroupSubscription, StreamItemSubscription, StreamSubscription };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,230 @@
|
|
|
1
|
+
import { v4 } from "uuid";
|
|
2
|
+
|
|
3
|
+
//#region src/stream-subscription.ts
|
|
4
|
+
var StreamSubscription = class {
|
|
5
|
+
constructor(sub, state) {
|
|
6
|
+
this.customEventListeners = /* @__PURE__ */ new Map();
|
|
7
|
+
this.closeListeners = /* @__PURE__ */ new Set();
|
|
8
|
+
this.onChangeListeners = /* @__PURE__ */ new Set();
|
|
9
|
+
this.sub = sub;
|
|
10
|
+
this.state = state;
|
|
11
|
+
}
|
|
12
|
+
onEventReceived(event) {
|
|
13
|
+
const customEventListeners = this.customEventListeners.get(event.type);
|
|
14
|
+
if (customEventListeners) {
|
|
15
|
+
const eventData = event.data;
|
|
16
|
+
customEventListeners.forEach((listener) => listener(eventData));
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Add a custom event listener. This listener will be called whenever the custom event is received.
|
|
21
|
+
*/
|
|
22
|
+
onEvent(type, listener) {
|
|
23
|
+
const listeners = this.customEventListeners.get(type) || [];
|
|
24
|
+
this.customEventListeners.set(type, [...listeners, listener]);
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Remove a custom event listener.
|
|
28
|
+
*/
|
|
29
|
+
offEvent(type, listener) {
|
|
30
|
+
const listeners = this.customEventListeners.get(type) || [];
|
|
31
|
+
this.customEventListeners.set(type, listeners.filter((l) => l !== listener));
|
|
32
|
+
}
|
|
33
|
+
onClose(listener) {
|
|
34
|
+
this.closeListeners.add(listener);
|
|
35
|
+
}
|
|
36
|
+
close() {
|
|
37
|
+
this.closeListeners.forEach((listener) => listener());
|
|
38
|
+
this.closeListeners.clear();
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Add a change listener. This listener will be called whenever the state of the group changes.
|
|
42
|
+
*/
|
|
43
|
+
addChangeListener(listener) {
|
|
44
|
+
this.onChangeListeners.add(listener);
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Remove a change listener.
|
|
48
|
+
*/
|
|
49
|
+
removeChangeListener(listener) {
|
|
50
|
+
this.onChangeListeners.delete(listener);
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Get the current state of the group.
|
|
54
|
+
*/
|
|
55
|
+
getState() {
|
|
56
|
+
return this.state;
|
|
57
|
+
}
|
|
58
|
+
setState(state) {
|
|
59
|
+
this.state = state;
|
|
60
|
+
this.onChangeListeners.forEach((listener) => listener(state));
|
|
61
|
+
}
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
//#endregion
|
|
65
|
+
//#region src/stream-group.ts
|
|
66
|
+
var StreamGroupSubscription = class extends StreamSubscription {
|
|
67
|
+
constructor(sub, sortKey) {
|
|
68
|
+
super(sub, []);
|
|
69
|
+
this.sortKey = sortKey;
|
|
70
|
+
this.lastTimestamp = 0;
|
|
71
|
+
this.lastTimestampMap = /* @__PURE__ */ new Map();
|
|
72
|
+
}
|
|
73
|
+
sort(state) {
|
|
74
|
+
const sortKey = this.sortKey;
|
|
75
|
+
if (sortKey) return state.sort((a, b) => {
|
|
76
|
+
const aValue = a[sortKey];
|
|
77
|
+
const bValue = b[sortKey];
|
|
78
|
+
if (aValue && bValue) return aValue.toString().localeCompare(bValue.toString());
|
|
79
|
+
return 0;
|
|
80
|
+
});
|
|
81
|
+
return state;
|
|
82
|
+
}
|
|
83
|
+
setState(state) {
|
|
84
|
+
super.setState(this.sort(state));
|
|
85
|
+
}
|
|
86
|
+
listener(message) {
|
|
87
|
+
if (message.event.type === "sync") {
|
|
88
|
+
if (message.timestamp < this.lastTimestamp) return;
|
|
89
|
+
this.lastTimestampMap = /* @__PURE__ */ new Map();
|
|
90
|
+
this.lastTimestamp = message.timestamp;
|
|
91
|
+
this.setState(message.event.data);
|
|
92
|
+
} else if (message.event.type === "create") {
|
|
93
|
+
const id = message.event.data.id;
|
|
94
|
+
const state = this.getState();
|
|
95
|
+
if (!state.find((item) => item.id === id)) this.setState([...state, message.event.data]);
|
|
96
|
+
} else if (message.event.type === "update") {
|
|
97
|
+
const messageData = message.event.data;
|
|
98
|
+
const messageDataId = messageData.id;
|
|
99
|
+
const state = this.getState();
|
|
100
|
+
const currentItemTimestamp = this.lastTimestampMap.get(messageDataId);
|
|
101
|
+
if (currentItemTimestamp && currentItemTimestamp >= message.timestamp) return;
|
|
102
|
+
this.lastTimestamp = message.timestamp;
|
|
103
|
+
this.lastTimestampMap.set(messageDataId, message.timestamp);
|
|
104
|
+
this.setState(state.map((item) => item.id === messageDataId ? messageData : item));
|
|
105
|
+
} else if (message.event.type === "delete") {
|
|
106
|
+
const messageDataId = message.event.data.id;
|
|
107
|
+
const state = this.getState();
|
|
108
|
+
this.lastTimestamp = message.timestamp;
|
|
109
|
+
this.lastTimestampMap.set(messageDataId, message.timestamp);
|
|
110
|
+
this.setState(state.filter((item) => item.id !== messageDataId));
|
|
111
|
+
} else if (message.event.type === "event") this.onEventReceived(message.event.event);
|
|
112
|
+
}
|
|
113
|
+
};
|
|
114
|
+
|
|
115
|
+
//#endregion
|
|
116
|
+
//#region src/stream-item.ts
|
|
117
|
+
var StreamItemSubscription = class extends StreamSubscription {
|
|
118
|
+
constructor(sub) {
|
|
119
|
+
super(sub, null);
|
|
120
|
+
this.lastEventTimestamp = 0;
|
|
121
|
+
}
|
|
122
|
+
listener(message) {
|
|
123
|
+
if (message.timestamp <= this.lastEventTimestamp) return;
|
|
124
|
+
this.lastEventTimestamp = message.timestamp;
|
|
125
|
+
if (message.event.type === "sync" || message.event.type === "create" || message.event.type === "update") this.setState(message.event.data);
|
|
126
|
+
else if (message.event.type === "delete") this.setState(null);
|
|
127
|
+
else if (message.event.type === "event") this.onEventReceived(message.event.event);
|
|
128
|
+
}
|
|
129
|
+
};
|
|
130
|
+
|
|
131
|
+
//#endregion
|
|
132
|
+
//#region src/stream.ts
|
|
133
|
+
var Stream = class {
|
|
134
|
+
constructor(adapterFactory) {
|
|
135
|
+
this.adapterFactory = adapterFactory;
|
|
136
|
+
this.listeners = {};
|
|
137
|
+
this.ws = this.createSocket();
|
|
138
|
+
}
|
|
139
|
+
createSocket() {
|
|
140
|
+
this.ws = this.adapterFactory();
|
|
141
|
+
this.ws.onMessage((message) => this.messageListener(message));
|
|
142
|
+
this.ws.onOpen(() => this.onSocketOpen());
|
|
143
|
+
this.ws.onClose(() => this.onSocketClose());
|
|
144
|
+
return this.ws;
|
|
145
|
+
}
|
|
146
|
+
/**
|
|
147
|
+
* Subscribe to an item in a stream.
|
|
148
|
+
*
|
|
149
|
+
* @argument streamName - The name of the stream to subscribe to.
|
|
150
|
+
* @argument groupId - The id of the group to subscribe to.
|
|
151
|
+
* @argument id - The id of the item to subscribe to.
|
|
152
|
+
*/
|
|
153
|
+
subscribeItem(streamName, groupId, id) {
|
|
154
|
+
const subscription = new StreamItemSubscription({
|
|
155
|
+
streamName,
|
|
156
|
+
groupId,
|
|
157
|
+
id,
|
|
158
|
+
subscriptionId: v4()
|
|
159
|
+
});
|
|
160
|
+
this.subscribe(subscription);
|
|
161
|
+
return subscription;
|
|
162
|
+
}
|
|
163
|
+
/**
|
|
164
|
+
* Subscribe to a group in a stream.
|
|
165
|
+
*
|
|
166
|
+
* @argument streamName - The name of the stream to subscribe to.
|
|
167
|
+
* @argument groupId - The id of the group to subscribe to.
|
|
168
|
+
*/
|
|
169
|
+
subscribeGroup(streamName, groupId, sortKey) {
|
|
170
|
+
const subscription = new StreamGroupSubscription({
|
|
171
|
+
streamName,
|
|
172
|
+
groupId,
|
|
173
|
+
subscriptionId: v4()
|
|
174
|
+
}, sortKey);
|
|
175
|
+
this.subscribe(subscription);
|
|
176
|
+
return subscription;
|
|
177
|
+
}
|
|
178
|
+
close() {
|
|
179
|
+
this.listeners = {};
|
|
180
|
+
this.ws.close();
|
|
181
|
+
}
|
|
182
|
+
onSocketClose() {
|
|
183
|
+
setTimeout(() => this.createSocket(), 2e3);
|
|
184
|
+
}
|
|
185
|
+
onSocketOpen() {
|
|
186
|
+
Object.values(this.listeners).forEach((listeners) => {
|
|
187
|
+
listeners.forEach((subscription) => this.join(subscription));
|
|
188
|
+
});
|
|
189
|
+
}
|
|
190
|
+
messageListener(event) {
|
|
191
|
+
const message = JSON.parse(event);
|
|
192
|
+
const room = this.roomName(message);
|
|
193
|
+
this.listeners[room]?.forEach((listener) => listener.listener(message));
|
|
194
|
+
if (message.id && message.event.type !== "sync") {
|
|
195
|
+
const groupRoom = this.roomName({
|
|
196
|
+
streamName: message.streamName,
|
|
197
|
+
groupId: message.groupId
|
|
198
|
+
});
|
|
199
|
+
this.listeners[groupRoom]?.forEach((listener) => listener.listener(message));
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
subscribe(subscription) {
|
|
203
|
+
const room = this.roomName(subscription.sub);
|
|
204
|
+
if (!this.listeners[room]) this.listeners[room] = /* @__PURE__ */ new Set();
|
|
205
|
+
this.listeners[room].add(subscription);
|
|
206
|
+
this.join(subscription);
|
|
207
|
+
subscription.onClose(() => {
|
|
208
|
+
this.listeners[room]?.delete(subscription);
|
|
209
|
+
this.leave(subscription);
|
|
210
|
+
});
|
|
211
|
+
}
|
|
212
|
+
join(subscription) {
|
|
213
|
+
if (this.ws.isOpen()) this.ws.send(JSON.stringify({
|
|
214
|
+
type: "join",
|
|
215
|
+
data: subscription.sub
|
|
216
|
+
}));
|
|
217
|
+
}
|
|
218
|
+
leave(subscription) {
|
|
219
|
+
if (this.ws.isOpen()) this.ws.send(JSON.stringify({
|
|
220
|
+
type: "leave",
|
|
221
|
+
data: subscription.sub
|
|
222
|
+
}));
|
|
223
|
+
}
|
|
224
|
+
roomName(message) {
|
|
225
|
+
return message.id ? `${message.streamName}:group:${message.groupId}:item:${message.id}` : `${message.streamName}:group:${message.groupId}`;
|
|
226
|
+
}
|
|
227
|
+
};
|
|
228
|
+
|
|
229
|
+
//#endregion
|
|
230
|
+
export { Stream, StreamGroupSubscription, StreamItemSubscription, StreamSubscription };
|
package/package.json
CHANGED
|
@@ -1,35 +1,30 @@
|
|
|
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.2-beta.
|
|
4
|
+
"version": "0.13.2-beta.164-562802",
|
|
5
5
|
"license": "Elastic-2.0",
|
|
6
|
-
"
|
|
7
|
-
"
|
|
8
|
-
"
|
|
9
|
-
"
|
|
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.js",
|
|
8
|
+
"module": "./dist/index.js",
|
|
9
|
+
"types": "./dist/index.d.ts",
|
|
21
10
|
"dependencies": {
|
|
22
|
-
"uuid": "^
|
|
11
|
+
"uuid": "^13.0.0"
|
|
23
12
|
},
|
|
24
13
|
"devDependencies": {
|
|
25
|
-
"@types/jest": "^
|
|
14
|
+
"@types/jest": "^30.0.0",
|
|
26
15
|
"@types/ws": "^8.18.1",
|
|
27
|
-
"jest": "^
|
|
28
|
-
"ts-jest": "^29.
|
|
29
|
-
"
|
|
16
|
+
"jest": "^30.2.0",
|
|
17
|
+
"ts-jest": "^29.4.5",
|
|
18
|
+
"tsdown": "^0.16.6",
|
|
19
|
+
"typescript": "^5.9.3"
|
|
20
|
+
},
|
|
21
|
+
"exports": {
|
|
22
|
+
".": "./dist/index.js",
|
|
23
|
+
"./package.json": "./package.json"
|
|
30
24
|
},
|
|
31
25
|
"scripts": {
|
|
32
|
-
"build": "
|
|
26
|
+
"build": "tsdown",
|
|
27
|
+
"dev": "tsdown --watch",
|
|
33
28
|
"lint": "eslint --config ../../eslint.config.js",
|
|
34
29
|
"test": "jest"
|
|
35
30
|
}
|
package/tsdown.config.ts
ADDED
|
@@ -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
|
+
exports: {
|
|
16
|
+
devExports: 'development',
|
|
17
|
+
},
|
|
18
|
+
})
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
|
@@ -1,107 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
const stream_group_1 = require("../src/stream-group");
|
|
4
|
-
describe('StreamGroupSubscription', () => {
|
|
5
|
-
const joinMessage = {
|
|
6
|
-
streamName: 'test-stream',
|
|
7
|
-
groupId: 'test-group',
|
|
8
|
-
subscriptionId: 'sub-1',
|
|
9
|
-
};
|
|
10
|
-
function makeMessage(type, data, timestamp = Date.now()) {
|
|
11
|
-
return {
|
|
12
|
-
streamName: 'test-stream',
|
|
13
|
-
groupId: 'test-group',
|
|
14
|
-
timestamp,
|
|
15
|
-
event: { type, ...(type === 'event' ? { event: data } : { data }) },
|
|
16
|
-
};
|
|
17
|
-
}
|
|
18
|
-
it('notifies change listeners on state change', () => {
|
|
19
|
-
const sub = new stream_group_1.StreamGroupSubscription(joinMessage);
|
|
20
|
-
const listener = jest.fn();
|
|
21
|
-
sub.addChangeListener(listener);
|
|
22
|
-
const syncData = [
|
|
23
|
-
{ id: '1', name: 'A' },
|
|
24
|
-
{ id: '2', name: 'B' },
|
|
25
|
-
];
|
|
26
|
-
sub.listener(makeMessage('sync', syncData));
|
|
27
|
-
expect(listener).toHaveBeenCalledWith(syncData);
|
|
28
|
-
});
|
|
29
|
-
it('should be able to sort by a key', () => {
|
|
30
|
-
const sub = new stream_group_1.StreamGroupSubscription(joinMessage, 'name');
|
|
31
|
-
const listener = jest.fn();
|
|
32
|
-
sub.addChangeListener(listener);
|
|
33
|
-
const syncData = [
|
|
34
|
-
{ id: '1', name: 'B' },
|
|
35
|
-
{ id: '2', name: 'A' },
|
|
36
|
-
];
|
|
37
|
-
sub.listener(makeMessage('sync', syncData));
|
|
38
|
-
expect(listener).toHaveBeenCalledWith(syncData);
|
|
39
|
-
expect(sub.getState()).toEqual([
|
|
40
|
-
{ id: '2', name: 'A' },
|
|
41
|
-
{ id: '1', name: 'B' },
|
|
42
|
-
]);
|
|
43
|
-
});
|
|
44
|
-
it('should discard old events', () => {
|
|
45
|
-
const sub = new stream_group_1.StreamGroupSubscription(joinMessage);
|
|
46
|
-
const listener = jest.fn();
|
|
47
|
-
sub.addChangeListener(listener);
|
|
48
|
-
const syncData = [
|
|
49
|
-
{ id: '1', name: 'A' },
|
|
50
|
-
{ id: '2', name: 'B' },
|
|
51
|
-
];
|
|
52
|
-
sub.listener(makeMessage('sync', syncData));
|
|
53
|
-
const oldSyncData = [
|
|
54
|
-
{ id: '3', name: 'C' },
|
|
55
|
-
{ id: '4', name: 'D' },
|
|
56
|
-
];
|
|
57
|
-
sub.listener(makeMessage('sync', oldSyncData, Date.now() - 1000));
|
|
58
|
-
expect(sub.getState()).toEqual(syncData);
|
|
59
|
-
});
|
|
60
|
-
it('should add items', () => {
|
|
61
|
-
const sub = new stream_group_1.StreamGroupSubscription(joinMessage);
|
|
62
|
-
const syncData = [
|
|
63
|
-
{ id: '1', name: 'A' },
|
|
64
|
-
{ id: '2', name: 'B' },
|
|
65
|
-
];
|
|
66
|
-
sub.listener(makeMessage('sync', syncData));
|
|
67
|
-
sub.listener(makeMessage('create', { id: '3', name: 'C' }));
|
|
68
|
-
expect(sub.getState()).toEqual([...syncData, { id: '3', name: 'C' }]);
|
|
69
|
-
});
|
|
70
|
-
it('should update items', () => {
|
|
71
|
-
const sub = new stream_group_1.StreamGroupSubscription(joinMessage);
|
|
72
|
-
const syncData = [
|
|
73
|
-
{ id: '1', name: 'A' },
|
|
74
|
-
{ id: '2', name: 'B' },
|
|
75
|
-
];
|
|
76
|
-
sub.listener(makeMessage('sync', syncData));
|
|
77
|
-
sub.listener(makeMessage('update', { id: '1', name: 'A1' }));
|
|
78
|
-
expect(sub.getState()).toEqual([
|
|
79
|
-
{ id: '1', name: 'A1' },
|
|
80
|
-
{ id: '2', name: 'B' },
|
|
81
|
-
]);
|
|
82
|
-
});
|
|
83
|
-
it('should remove items', () => {
|
|
84
|
-
const sub = new stream_group_1.StreamGroupSubscription(joinMessage);
|
|
85
|
-
const syncData = [
|
|
86
|
-
{ id: '1', name: 'A' },
|
|
87
|
-
{ id: '2', name: 'B' },
|
|
88
|
-
];
|
|
89
|
-
sub.listener(makeMessage('sync', syncData));
|
|
90
|
-
sub.listener(makeMessage('delete', { id: '1' }));
|
|
91
|
-
expect(sub.getState()).toEqual([{ id: '2', name: 'B' }]);
|
|
92
|
-
});
|
|
93
|
-
it('should discard old on event on a particular item', () => {
|
|
94
|
-
const sub = new stream_group_1.StreamGroupSubscription(joinMessage);
|
|
95
|
-
const syncData = [
|
|
96
|
-
{ id: '1', name: 'A' },
|
|
97
|
-
{ id: '2', name: 'B' },
|
|
98
|
-
];
|
|
99
|
-
sub.listener(makeMessage('sync', syncData));
|
|
100
|
-
sub.listener(makeMessage('update', { id: '1', name: 'A1' }));
|
|
101
|
-
sub.listener(makeMessage('update', { id: '1', name: 'A2' }, Date.now() - 1000));
|
|
102
|
-
expect(sub.getState()).toEqual([
|
|
103
|
-
{ id: '1', name: 'A1' },
|
|
104
|
-
{ id: '2', name: 'B' },
|
|
105
|
-
]);
|
|
106
|
-
});
|
|
107
|
-
});
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
|
@@ -1,48 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
const stream_item_1 = require("../src/stream-item");
|
|
4
|
-
describe('StreamItemSubscription', () => {
|
|
5
|
-
const joinMessage = {
|
|
6
|
-
streamName: 'test-stream',
|
|
7
|
-
groupId: 'test-group',
|
|
8
|
-
subscriptionId: 'sub-1',
|
|
9
|
-
};
|
|
10
|
-
function makeMessage(type, data, timestamp = Date.now()) {
|
|
11
|
-
return {
|
|
12
|
-
streamName: 'test-stream',
|
|
13
|
-
groupId: 'test-group',
|
|
14
|
-
timestamp,
|
|
15
|
-
event: { type, ...(type === 'event' ? { event: data } : { data }) },
|
|
16
|
-
};
|
|
17
|
-
}
|
|
18
|
-
it('notifies change listeners on state change', () => {
|
|
19
|
-
const sub = new stream_item_1.StreamItemSubscription(joinMessage);
|
|
20
|
-
const listener = jest.fn();
|
|
21
|
-
sub.addChangeListener(listener);
|
|
22
|
-
const syncData = { id: '1', name: 'A', value: 1 };
|
|
23
|
-
sub.listener(makeMessage('sync', syncData));
|
|
24
|
-
expect(listener).toHaveBeenCalledWith(syncData);
|
|
25
|
-
});
|
|
26
|
-
it('should discard old events', () => {
|
|
27
|
-
const sub = new stream_item_1.StreamItemSubscription(joinMessage);
|
|
28
|
-
const syncData = { id: '1', name: 'A', value: 1 };
|
|
29
|
-
const oldSyncData = { id: '1', name: 'B', value: 3 };
|
|
30
|
-
sub.listener(makeMessage('sync', syncData));
|
|
31
|
-
sub.listener(makeMessage('sync', oldSyncData, Date.now() - 1000));
|
|
32
|
-
expect(sub.getState()).toEqual(syncData);
|
|
33
|
-
});
|
|
34
|
-
it('should update item', () => {
|
|
35
|
-
const sub = new stream_item_1.StreamItemSubscription(joinMessage);
|
|
36
|
-
const syncData = { id: '1', name: 'A', value: 1 };
|
|
37
|
-
sub.listener(makeMessage('sync', syncData));
|
|
38
|
-
sub.listener(makeMessage('update', { id: '1', name: 'A1', value: 2 }, Date.now() + 1000));
|
|
39
|
-
expect(sub.getState()).toEqual({ id: '1', name: 'A1', value: 2 });
|
|
40
|
-
});
|
|
41
|
-
it('should remove item', () => {
|
|
42
|
-
const sub = new stream_item_1.StreamItemSubscription(joinMessage);
|
|
43
|
-
const syncData = { id: '1', name: 'A', value: 1 };
|
|
44
|
-
sub.listener(makeMessage('sync', syncData));
|
|
45
|
-
sub.listener(makeMessage('delete', { id: '1' }, Date.now() + 1000));
|
|
46
|
-
expect(sub.getState()).toEqual(null);
|
|
47
|
-
});
|
|
48
|
-
});
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|