@motiadev/stream-client-browser 0.2.1-beta.75 → 0.2.1-beta.76
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.
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import { Stream } from '../src/stream';
|
|
2
|
+
class MockSocket {
|
|
3
|
+
constructor() {
|
|
4
|
+
this.listeners = {};
|
|
5
|
+
}
|
|
6
|
+
addEventListener(event, cb) {
|
|
7
|
+
if (!this.listeners[event])
|
|
8
|
+
this.listeners[event] = [];
|
|
9
|
+
this.listeners[event].push(cb);
|
|
10
|
+
}
|
|
11
|
+
send(data) {
|
|
12
|
+
this.listeners['message']?.forEach((cb) => cb({ data: JSON.stringify(data) }));
|
|
13
|
+
}
|
|
14
|
+
close() {
|
|
15
|
+
this.listeners = {};
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
function getSocket(stream) {
|
|
19
|
+
return stream.ws;
|
|
20
|
+
}
|
|
21
|
+
function makeGroupMessage(type, data, timestamp = Date.now()) {
|
|
22
|
+
return {
|
|
23
|
+
streamName: 'test-stream',
|
|
24
|
+
groupId: 'test-group',
|
|
25
|
+
timestamp,
|
|
26
|
+
event: { type, ...(type === 'event' ? { event: data } : { data }) },
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
function makeItemMessage(type, data, timestamp = Date.now()) {
|
|
30
|
+
return {
|
|
31
|
+
streamName: 'test-stream',
|
|
32
|
+
groupId: 'test-group',
|
|
33
|
+
id: '1',
|
|
34
|
+
timestamp,
|
|
35
|
+
event: { type, ...(type === 'event' ? { event: data } : { data }) },
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
describe('Stream', () => {
|
|
39
|
+
beforeEach(() => {
|
|
40
|
+
global.WebSocket = MockSocket;
|
|
41
|
+
});
|
|
42
|
+
afterEach(() => {
|
|
43
|
+
delete global.WebSocket;
|
|
44
|
+
});
|
|
45
|
+
it('should sync group events', () => {
|
|
46
|
+
const stream = new Stream('ws://localhost:3000');
|
|
47
|
+
const socket = getSocket(stream);
|
|
48
|
+
const sub = stream.subscribeGroup('test-stream', 'test-group');
|
|
49
|
+
const syncData = [
|
|
50
|
+
{ id: '1', name: 'A' },
|
|
51
|
+
{ id: '2', name: 'B' },
|
|
52
|
+
];
|
|
53
|
+
socket.send(makeGroupMessage('sync', syncData));
|
|
54
|
+
expect(sub.getState()).toEqual(syncData);
|
|
55
|
+
});
|
|
56
|
+
it('should not sync item events in group subscriptions', () => {
|
|
57
|
+
const stream = new Stream('ws://localhost:3000');
|
|
58
|
+
const socket = getSocket(stream);
|
|
59
|
+
const sub = stream.subscribeGroup('test-stream', 'test-group');
|
|
60
|
+
const syncData = [
|
|
61
|
+
{ id: '1', name: 'A' },
|
|
62
|
+
{ id: '2', name: 'B' },
|
|
63
|
+
];
|
|
64
|
+
socket.send(makeGroupMessage('sync', syncData));
|
|
65
|
+
socket.send(makeItemMessage('sync', syncData[0]));
|
|
66
|
+
expect(sub.getState()).toEqual(syncData);
|
|
67
|
+
});
|
|
68
|
+
});
|
package/dist/src/stream.js
CHANGED
|
@@ -61,7 +61,8 @@ export class Stream {
|
|
|
61
61
|
const message = JSON.parse(event.data);
|
|
62
62
|
const room = this.roomName(message);
|
|
63
63
|
this.listeners[room]?.forEach((listener) => listener.listener(message));
|
|
64
|
-
|
|
64
|
+
// we need to discard sync to group subs when it's an item event
|
|
65
|
+
if (message.id && message.event.type !== 'sync') {
|
|
65
66
|
const groupRoom = this.roomName({
|
|
66
67
|
streamName: message.streamName,
|
|
67
68
|
groupId: message.groupId,
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@motiadev/stream-client-browser",
|
|
3
3
|
"description": "Motia Stream Client Package – Responsible for managing streams of data.",
|
|
4
|
-
"version": "0.2.1-beta.
|
|
4
|
+
"version": "0.2.1-beta.76",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"main": "dist/index.js",
|
|
7
7
|
"types": "dist/index.d.ts",
|