@motiadev/stream-client-browser 0.2.1-beta.73 → 0.2.1-beta.74
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/dist/__tests__/stream-group.spec.d.ts +1 -0
- package/dist/__tests__/stream-group.spec.js +105 -0
- package/dist/__tests__/stream-item.spec.d.ts +1 -0
- package/dist/__tests__/stream-item.spec.js +46 -0
- package/dist/src/stream-group.d.ts +6 -1
- package/dist/src/stream-group.js +34 -1
- package/dist/src/stream-item.d.ts +1 -0
- package/dist/src/stream-item.js +5 -0
- package/dist/src/stream.d.ts +1 -1
- package/dist/src/stream.js +2 -2
- package/dist/src/stream.types.d.ts +2 -1
- package/package.json +4 -3
- package/src/stream-group.ts +0 -37
- package/src/stream-item.ts +0 -21
- package/src/stream-subscription.ts +0 -84
- package/src/stream.ts +0 -130
- package/src/stream.types.ts +0 -18
- package/tsconfig.json +0 -23
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
import { StreamGroupSubscription } from '../src/stream-group';
|
|
2
|
+
describe('StreamGroupSubscription', () => {
|
|
3
|
+
const joinMessage = {
|
|
4
|
+
streamName: 'test-stream',
|
|
5
|
+
groupId: 'test-group',
|
|
6
|
+
subscriptionId: 'sub-1',
|
|
7
|
+
};
|
|
8
|
+
function makeMessage(type, data, timestamp = Date.now()) {
|
|
9
|
+
return {
|
|
10
|
+
streamName: 'test-stream',
|
|
11
|
+
groupId: 'test-group',
|
|
12
|
+
timestamp,
|
|
13
|
+
event: { type, ...(type === 'event' ? { event: data } : { data }) },
|
|
14
|
+
};
|
|
15
|
+
}
|
|
16
|
+
it('notifies change listeners on state change', () => {
|
|
17
|
+
const sub = new StreamGroupSubscription(joinMessage);
|
|
18
|
+
const listener = jest.fn();
|
|
19
|
+
sub.addChangeListener(listener);
|
|
20
|
+
const syncData = [
|
|
21
|
+
{ id: '1', name: 'A' },
|
|
22
|
+
{ id: '2', name: 'B' },
|
|
23
|
+
];
|
|
24
|
+
sub.listener(makeMessage('sync', syncData));
|
|
25
|
+
expect(listener).toHaveBeenCalledWith(syncData);
|
|
26
|
+
});
|
|
27
|
+
it('should be able to sort by a key', () => {
|
|
28
|
+
const sub = new StreamGroupSubscription(joinMessage, 'name');
|
|
29
|
+
const listener = jest.fn();
|
|
30
|
+
sub.addChangeListener(listener);
|
|
31
|
+
const syncData = [
|
|
32
|
+
{ id: '1', name: 'B' },
|
|
33
|
+
{ id: '2', name: 'A' },
|
|
34
|
+
];
|
|
35
|
+
sub.listener(makeMessage('sync', syncData));
|
|
36
|
+
expect(listener).toHaveBeenCalledWith(syncData);
|
|
37
|
+
expect(sub.getState()).toEqual([
|
|
38
|
+
{ id: '2', name: 'A' },
|
|
39
|
+
{ id: '1', name: 'B' },
|
|
40
|
+
]);
|
|
41
|
+
});
|
|
42
|
+
it('should discard old events', () => {
|
|
43
|
+
const sub = new StreamGroupSubscription(joinMessage);
|
|
44
|
+
const listener = jest.fn();
|
|
45
|
+
sub.addChangeListener(listener);
|
|
46
|
+
const syncData = [
|
|
47
|
+
{ id: '1', name: 'A' },
|
|
48
|
+
{ id: '2', name: 'B' },
|
|
49
|
+
];
|
|
50
|
+
sub.listener(makeMessage('sync', syncData));
|
|
51
|
+
const oldSyncData = [
|
|
52
|
+
{ id: '3', name: 'C' },
|
|
53
|
+
{ id: '4', name: 'D' },
|
|
54
|
+
];
|
|
55
|
+
sub.listener(makeMessage('sync', oldSyncData, Date.now() - 1000));
|
|
56
|
+
expect(sub.getState()).toEqual(syncData);
|
|
57
|
+
});
|
|
58
|
+
it('should add items', () => {
|
|
59
|
+
const sub = new StreamGroupSubscription(joinMessage);
|
|
60
|
+
const syncData = [
|
|
61
|
+
{ id: '1', name: 'A' },
|
|
62
|
+
{ id: '2', name: 'B' },
|
|
63
|
+
];
|
|
64
|
+
sub.listener(makeMessage('sync', syncData));
|
|
65
|
+
sub.listener(makeMessage('create', { id: '3', name: 'C' }));
|
|
66
|
+
expect(sub.getState()).toEqual([...syncData, { id: '3', name: 'C' }]);
|
|
67
|
+
});
|
|
68
|
+
it('should update items', () => {
|
|
69
|
+
const sub = new StreamGroupSubscription(joinMessage);
|
|
70
|
+
const syncData = [
|
|
71
|
+
{ id: '1', name: 'A' },
|
|
72
|
+
{ id: '2', name: 'B' },
|
|
73
|
+
];
|
|
74
|
+
sub.listener(makeMessage('sync', syncData));
|
|
75
|
+
sub.listener(makeMessage('update', { id: '1', name: 'A1' }));
|
|
76
|
+
expect(sub.getState()).toEqual([
|
|
77
|
+
{ id: '1', name: 'A1' },
|
|
78
|
+
{ id: '2', name: 'B' },
|
|
79
|
+
]);
|
|
80
|
+
});
|
|
81
|
+
it('should remove items', () => {
|
|
82
|
+
const sub = new StreamGroupSubscription(joinMessage);
|
|
83
|
+
const syncData = [
|
|
84
|
+
{ id: '1', name: 'A' },
|
|
85
|
+
{ id: '2', name: 'B' },
|
|
86
|
+
];
|
|
87
|
+
sub.listener(makeMessage('sync', syncData));
|
|
88
|
+
sub.listener(makeMessage('delete', { id: '1' }));
|
|
89
|
+
expect(sub.getState()).toEqual([{ id: '2', name: 'B' }]);
|
|
90
|
+
});
|
|
91
|
+
it('should discard old on event on a particular item', () => {
|
|
92
|
+
const sub = new StreamGroupSubscription(joinMessage);
|
|
93
|
+
const syncData = [
|
|
94
|
+
{ id: '1', name: 'A' },
|
|
95
|
+
{ id: '2', name: 'B' },
|
|
96
|
+
];
|
|
97
|
+
sub.listener(makeMessage('sync', syncData));
|
|
98
|
+
sub.listener(makeMessage('update', { id: '1', name: 'A1' }));
|
|
99
|
+
sub.listener(makeMessage('update', { id: '1', name: 'A2' }, Date.now() - 1000));
|
|
100
|
+
expect(sub.getState()).toEqual([
|
|
101
|
+
{ id: '1', name: 'A1' },
|
|
102
|
+
{ id: '2', name: 'B' },
|
|
103
|
+
]);
|
|
104
|
+
});
|
|
105
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { StreamItemSubscription } from '../src/stream-item';
|
|
2
|
+
describe('StreamItemSubscription', () => {
|
|
3
|
+
const joinMessage = {
|
|
4
|
+
streamName: 'test-stream',
|
|
5
|
+
groupId: 'test-group',
|
|
6
|
+
subscriptionId: 'sub-1',
|
|
7
|
+
};
|
|
8
|
+
function makeMessage(type, data, timestamp = Date.now()) {
|
|
9
|
+
return {
|
|
10
|
+
streamName: 'test-stream',
|
|
11
|
+
groupId: 'test-group',
|
|
12
|
+
timestamp,
|
|
13
|
+
event: { type, ...(type === 'event' ? { event: data } : { data }) },
|
|
14
|
+
};
|
|
15
|
+
}
|
|
16
|
+
it('notifies change listeners on state change', () => {
|
|
17
|
+
const sub = new StreamItemSubscription(joinMessage);
|
|
18
|
+
const listener = jest.fn();
|
|
19
|
+
sub.addChangeListener(listener);
|
|
20
|
+
const syncData = { id: '1', name: 'A', value: 1 };
|
|
21
|
+
sub.listener(makeMessage('sync', syncData));
|
|
22
|
+
expect(listener).toHaveBeenCalledWith(syncData);
|
|
23
|
+
});
|
|
24
|
+
it('should discard old events', () => {
|
|
25
|
+
const sub = new StreamItemSubscription(joinMessage);
|
|
26
|
+
const syncData = { id: '1', name: 'A', value: 1 };
|
|
27
|
+
const oldSyncData = { id: '1', name: 'B', value: 3 };
|
|
28
|
+
sub.listener(makeMessage('sync', syncData));
|
|
29
|
+
sub.listener(makeMessage('sync', oldSyncData, Date.now() - 1000));
|
|
30
|
+
expect(sub.getState()).toEqual(syncData);
|
|
31
|
+
});
|
|
32
|
+
it('should update item', () => {
|
|
33
|
+
const sub = new StreamItemSubscription(joinMessage);
|
|
34
|
+
const syncData = { id: '1', name: 'A', value: 1 };
|
|
35
|
+
sub.listener(makeMessage('sync', syncData));
|
|
36
|
+
sub.listener(makeMessage('update', { id: '1', name: 'A1', value: 2 }, Date.now() + 1000));
|
|
37
|
+
expect(sub.getState()).toEqual({ id: '1', name: 'A1', value: 2 });
|
|
38
|
+
});
|
|
39
|
+
it('should remove item', () => {
|
|
40
|
+
const sub = new StreamItemSubscription(joinMessage);
|
|
41
|
+
const syncData = { id: '1', name: 'A', value: 1 };
|
|
42
|
+
sub.listener(makeMessage('sync', syncData));
|
|
43
|
+
sub.listener(makeMessage('delete', { id: '1' }, Date.now() + 1000));
|
|
44
|
+
expect(sub.getState()).toEqual(null);
|
|
45
|
+
});
|
|
46
|
+
});
|
|
@@ -3,6 +3,11 @@ import { GroupEventMessage, JoinMessage } from './stream.types';
|
|
|
3
3
|
export declare class StreamGroupSubscription<TData extends {
|
|
4
4
|
id: string;
|
|
5
5
|
}> extends StreamSubscription<TData[], GroupEventMessage<TData>> {
|
|
6
|
-
|
|
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;
|
|
7
12
|
listener(message: GroupEventMessage<TData>): void;
|
|
8
13
|
}
|
package/dist/src/stream-group.js
CHANGED
|
@@ -1,10 +1,35 @@
|
|
|
1
1
|
import { StreamSubscription } from './stream-subscription';
|
|
2
2
|
export class StreamGroupSubscription extends StreamSubscription {
|
|
3
|
-
constructor(sub) {
|
|
3
|
+
constructor(sub, sortKey) {
|
|
4
4
|
super(sub, []);
|
|
5
|
+
this.sortKey = sortKey;
|
|
6
|
+
this.lastTimestamp = 0;
|
|
7
|
+
this.lastTimestampMap = new Map();
|
|
8
|
+
}
|
|
9
|
+
sort(state) {
|
|
10
|
+
const sortKey = this.sortKey;
|
|
11
|
+
if (sortKey) {
|
|
12
|
+
return state.sort((a, b) => {
|
|
13
|
+
const aValue = a[sortKey];
|
|
14
|
+
const bValue = b[sortKey];
|
|
15
|
+
if (aValue && bValue) {
|
|
16
|
+
return aValue.toString().localeCompare(bValue.toString());
|
|
17
|
+
}
|
|
18
|
+
return 0;
|
|
19
|
+
});
|
|
20
|
+
}
|
|
21
|
+
return state;
|
|
22
|
+
}
|
|
23
|
+
setState(state) {
|
|
24
|
+
super.setState(this.sort(state));
|
|
5
25
|
}
|
|
6
26
|
listener(message) {
|
|
7
27
|
if (message.event.type === 'sync') {
|
|
28
|
+
if (message.timestamp < this.lastTimestamp) {
|
|
29
|
+
return;
|
|
30
|
+
}
|
|
31
|
+
this.lastTimestampMap = new Map();
|
|
32
|
+
this.lastTimestamp = message.timestamp;
|
|
8
33
|
this.setState(message.event.data);
|
|
9
34
|
}
|
|
10
35
|
else if (message.event.type === 'create') {
|
|
@@ -18,11 +43,19 @@ export class StreamGroupSubscription extends StreamSubscription {
|
|
|
18
43
|
const messageData = message.event.data;
|
|
19
44
|
const messageDataId = messageData.id;
|
|
20
45
|
const state = this.getState();
|
|
46
|
+
const currentItemTimestamp = this.lastTimestampMap.get(messageDataId);
|
|
47
|
+
if (currentItemTimestamp && currentItemTimestamp >= message.timestamp) {
|
|
48
|
+
return;
|
|
49
|
+
}
|
|
50
|
+
this.lastTimestamp = message.timestamp;
|
|
51
|
+
this.lastTimestampMap.set(messageDataId, message.timestamp);
|
|
21
52
|
this.setState(state.map((item) => (item.id === messageDataId ? messageData : item)));
|
|
22
53
|
}
|
|
23
54
|
else if (message.event.type === 'delete') {
|
|
24
55
|
const messageDataId = message.event.data.id;
|
|
25
56
|
const state = this.getState();
|
|
57
|
+
this.lastTimestamp = message.timestamp;
|
|
58
|
+
this.lastTimestampMap.set(messageDataId, message.timestamp);
|
|
26
59
|
this.setState(state.filter((item) => item.id !== messageDataId));
|
|
27
60
|
}
|
|
28
61
|
else if (message.event.type === 'event') {
|
|
@@ -3,6 +3,7 @@ import { ItemEventMessage, JoinMessage } from './stream.types';
|
|
|
3
3
|
export declare class StreamItemSubscription<TData extends {
|
|
4
4
|
id: string;
|
|
5
5
|
}> extends StreamSubscription<TData | null, ItemEventMessage<TData>> {
|
|
6
|
+
private lastEventTimestamp;
|
|
6
7
|
constructor(sub: JoinMessage);
|
|
7
8
|
listener(message: ItemEventMessage<TData>): void;
|
|
8
9
|
}
|
package/dist/src/stream-item.js
CHANGED
|
@@ -2,8 +2,13 @@ import { StreamSubscription } from './stream-subscription';
|
|
|
2
2
|
export class StreamItemSubscription extends StreamSubscription {
|
|
3
3
|
constructor(sub) {
|
|
4
4
|
super(sub, null);
|
|
5
|
+
this.lastEventTimestamp = 0;
|
|
5
6
|
}
|
|
6
7
|
listener(message) {
|
|
8
|
+
if (message.timestamp <= this.lastEventTimestamp) {
|
|
9
|
+
return;
|
|
10
|
+
}
|
|
11
|
+
this.lastEventTimestamp = message.timestamp;
|
|
7
12
|
if (message.event.type === 'sync' || message.event.type === 'create' || message.event.type === 'update') {
|
|
8
13
|
this.setState(message.event.data);
|
|
9
14
|
}
|
package/dist/src/stream.d.ts
CHANGED
|
@@ -24,7 +24,7 @@ export declare class Stream {
|
|
|
24
24
|
*/
|
|
25
25
|
subscribeGroup<TData extends {
|
|
26
26
|
id: string;
|
|
27
|
-
}>(streamName: string, groupId: string): StreamGroupSubscription<TData>;
|
|
27
|
+
}>(streamName: string, groupId: string, sortKey?: keyof TData): StreamGroupSubscription<TData>;
|
|
28
28
|
close(): void;
|
|
29
29
|
private onSocketClose;
|
|
30
30
|
private onSocketOpen;
|
package/dist/src/stream.js
CHANGED
|
@@ -34,10 +34,10 @@ export class Stream {
|
|
|
34
34
|
* @argument streamName - The name of the stream to subscribe to.
|
|
35
35
|
* @argument groupId - The id of the group to subscribe to.
|
|
36
36
|
*/
|
|
37
|
-
subscribeGroup(streamName, groupId) {
|
|
37
|
+
subscribeGroup(streamName, groupId, sortKey) {
|
|
38
38
|
const subscriptionId = uuidv4();
|
|
39
39
|
const sub = { streamName, groupId, subscriptionId };
|
|
40
|
-
const subscription = new StreamGroupSubscription(sub);
|
|
40
|
+
const subscription = new StreamGroupSubscription(sub, sortKey);
|
|
41
41
|
this.subscribe(subscription);
|
|
42
42
|
return subscription;
|
|
43
43
|
}
|
|
@@ -2,8 +2,9 @@ export type BaseMessage = {
|
|
|
2
2
|
streamName: string;
|
|
3
3
|
groupId: string;
|
|
4
4
|
id?: string;
|
|
5
|
+
timestamp: number;
|
|
5
6
|
};
|
|
6
|
-
export type JoinMessage = BaseMessage & {
|
|
7
|
+
export type JoinMessage = Omit<BaseMessage, 'timestamp'> & {
|
|
7
8
|
subscriptionId: string;
|
|
8
9
|
};
|
|
9
10
|
export type CustomEvent = {
|
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.74",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"main": "dist/index.js",
|
|
7
7
|
"types": "dist/index.d.ts",
|
|
@@ -12,11 +12,12 @@
|
|
|
12
12
|
"@types/jest": "^29.5.14",
|
|
13
13
|
"@types/ws": "^8.18.1",
|
|
14
14
|
"jest": "^29.7.0",
|
|
15
|
-
"ts-jest": "^29.2
|
|
15
|
+
"ts-jest": "^29.3.2",
|
|
16
16
|
"typescript": "^5.7.2"
|
|
17
17
|
},
|
|
18
18
|
"scripts": {
|
|
19
19
|
"build": "rm -rf dist && tsc",
|
|
20
|
-
"lint": "eslint --config ../../eslint.config.js"
|
|
20
|
+
"lint": "eslint --config ../../eslint.config.js",
|
|
21
|
+
"test": "jest"
|
|
21
22
|
}
|
|
22
23
|
}
|
package/src/stream-group.ts
DELETED
|
@@ -1,37 +0,0 @@
|
|
|
1
|
-
import { StreamSubscription } from './stream-subscription'
|
|
2
|
-
import { GroupEventMessage, JoinMessage } from './stream.types'
|
|
3
|
-
|
|
4
|
-
export class StreamGroupSubscription<TData extends { id: string }> extends StreamSubscription<
|
|
5
|
-
TData[],
|
|
6
|
-
GroupEventMessage<TData>
|
|
7
|
-
> {
|
|
8
|
-
constructor(sub: JoinMessage) {
|
|
9
|
-
super(sub, [])
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
listener(message: GroupEventMessage<TData>): void {
|
|
13
|
-
if (message.event.type === 'sync') {
|
|
14
|
-
this.setState(message.event.data)
|
|
15
|
-
} else if (message.event.type === 'create') {
|
|
16
|
-
const id = message.event.data.id
|
|
17
|
-
const state = this.getState()
|
|
18
|
-
|
|
19
|
-
if (!state.find((item) => item.id === id)) {
|
|
20
|
-
this.setState([...state, message.event.data])
|
|
21
|
-
}
|
|
22
|
-
} else if (message.event.type === 'update') {
|
|
23
|
-
const messageData = message.event.data
|
|
24
|
-
const messageDataId = messageData.id
|
|
25
|
-
const state = this.getState()
|
|
26
|
-
|
|
27
|
-
this.setState(state.map((item) => (item.id === messageDataId ? messageData : item)))
|
|
28
|
-
} else if (message.event.type === 'delete') {
|
|
29
|
-
const messageDataId = message.event.data.id
|
|
30
|
-
const state = this.getState()
|
|
31
|
-
|
|
32
|
-
this.setState(state.filter((item) => item.id !== messageDataId))
|
|
33
|
-
} else if (message.event.type === 'event') {
|
|
34
|
-
this.onEventReceived(message.event.event)
|
|
35
|
-
}
|
|
36
|
-
}
|
|
37
|
-
}
|
package/src/stream-item.ts
DELETED
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
import { StreamSubscription } from './stream-subscription'
|
|
2
|
-
import { ItemEventMessage, JoinMessage } from './stream.types'
|
|
3
|
-
|
|
4
|
-
export class StreamItemSubscription<TData extends { id: string }> extends StreamSubscription<
|
|
5
|
-
TData | null,
|
|
6
|
-
ItemEventMessage<TData>
|
|
7
|
-
> {
|
|
8
|
-
constructor(sub: JoinMessage) {
|
|
9
|
-
super(sub, null)
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
listener(message: ItemEventMessage<TData>): void {
|
|
13
|
-
if (message.event.type === 'sync' || message.event.type === 'create' || message.event.type === 'update') {
|
|
14
|
-
this.setState(message.event.data)
|
|
15
|
-
} else if (message.event.type === 'delete') {
|
|
16
|
-
this.setState(null)
|
|
17
|
-
} else if (message.event.type === 'event') {
|
|
18
|
-
this.onEventReceived(message.event.event)
|
|
19
|
-
}
|
|
20
|
-
}
|
|
21
|
-
}
|
|
@@ -1,84 +0,0 @@
|
|
|
1
|
-
import { CustomEvent, JoinMessage, Listener } from './stream.types'
|
|
2
|
-
|
|
3
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
4
|
-
type CustomEventListener = (event: any) => void
|
|
5
|
-
|
|
6
|
-
export abstract class StreamSubscription<TData = unknown, TEventData = unknown> {
|
|
7
|
-
private customEventListeners: Map<string, CustomEventListener[]> = new Map()
|
|
8
|
-
private closeListeners: Set<() => void> = new Set()
|
|
9
|
-
|
|
10
|
-
private onChangeListeners: Set<Listener<TData>> = new Set()
|
|
11
|
-
private state: TData
|
|
12
|
-
|
|
13
|
-
readonly sub: JoinMessage
|
|
14
|
-
|
|
15
|
-
constructor(sub: JoinMessage, state: TData) {
|
|
16
|
-
this.sub = sub
|
|
17
|
-
this.state = state
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
abstract listener(message: TEventData): void
|
|
21
|
-
|
|
22
|
-
protected onEventReceived(event: CustomEvent) {
|
|
23
|
-
const customEventListeners = this.customEventListeners.get(event.type)
|
|
24
|
-
|
|
25
|
-
if (customEventListeners) {
|
|
26
|
-
const eventData = event.data
|
|
27
|
-
customEventListeners.forEach((listener) => listener(eventData))
|
|
28
|
-
}
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
/**
|
|
32
|
-
* Add a custom event listener. This listener will be called whenever the custom event is received.
|
|
33
|
-
*/
|
|
34
|
-
onEvent(type: string, listener: CustomEventListener) {
|
|
35
|
-
const listeners = this.customEventListeners.get(type) || []
|
|
36
|
-
this.customEventListeners.set(type, [...listeners, listener])
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
/**
|
|
40
|
-
* Remove a custom event listener.
|
|
41
|
-
*/
|
|
42
|
-
offEvent(type: string, listener: CustomEventListener) {
|
|
43
|
-
const listeners = this.customEventListeners.get(type) || []
|
|
44
|
-
this.customEventListeners.set(
|
|
45
|
-
type,
|
|
46
|
-
listeners.filter((l) => l !== listener),
|
|
47
|
-
)
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
onClose(listener: () => void) {
|
|
51
|
-
this.closeListeners.add(listener)
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
close() {
|
|
55
|
-
this.closeListeners.forEach((listener) => listener())
|
|
56
|
-
this.closeListeners.clear()
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
/**
|
|
60
|
-
* Add a change listener. This listener will be called whenever the state of the group changes.
|
|
61
|
-
*/
|
|
62
|
-
addChangeListener(listener: Listener<TData>) {
|
|
63
|
-
this.onChangeListeners.add(listener)
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
/**
|
|
67
|
-
* Remove a change listener.
|
|
68
|
-
*/
|
|
69
|
-
removeChangeListener(listener: Listener<TData>) {
|
|
70
|
-
this.onChangeListeners.delete(listener)
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
/**
|
|
74
|
-
* Get the current state of the group.
|
|
75
|
-
*/
|
|
76
|
-
getState(): TData {
|
|
77
|
-
return this.state
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
protected setState(state: TData) {
|
|
81
|
-
this.state = state
|
|
82
|
-
this.onChangeListeners.forEach((listener) => listener(state))
|
|
83
|
-
}
|
|
84
|
-
}
|
package/src/stream.ts
DELETED
|
@@ -1,130 +0,0 @@
|
|
|
1
|
-
import { v4 as uuidv4 } from 'uuid'
|
|
2
|
-
import { StreamGroupSubscription } from './stream-group'
|
|
3
|
-
import { StreamItemSubscription } from './stream-item'
|
|
4
|
-
import { StreamSubscription } from './stream-subscription'
|
|
5
|
-
import { BaseMessage } from './stream.types'
|
|
6
|
-
|
|
7
|
-
export class Stream {
|
|
8
|
-
private ws: WebSocket
|
|
9
|
-
private listeners: { [channelId: string]: Set<StreamSubscription> } = {}
|
|
10
|
-
|
|
11
|
-
constructor(private address: string) {
|
|
12
|
-
this.ws = this.createSocket()
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
createSocket(): WebSocket {
|
|
16
|
-
this.ws = new WebSocket(this.address)
|
|
17
|
-
this.ws.addEventListener('message', (message) => this.messageListener(message))
|
|
18
|
-
this.ws.addEventListener('open', () => this.onSocketOpen())
|
|
19
|
-
this.ws.addEventListener('close', () => this.onSocketClose())
|
|
20
|
-
|
|
21
|
-
return this.ws
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
/**
|
|
25
|
-
* Subscribe to an item in a stream.
|
|
26
|
-
*
|
|
27
|
-
* @argument streamName - The name of the stream to subscribe to.
|
|
28
|
-
* @argument groupId - The id of the group to subscribe to.
|
|
29
|
-
* @argument id - The id of the item to subscribe to.
|
|
30
|
-
*/
|
|
31
|
-
subscribeItem<TData extends { id: string }>(
|
|
32
|
-
streamName: string,
|
|
33
|
-
groupId: string,
|
|
34
|
-
id: string,
|
|
35
|
-
): StreamItemSubscription<TData> {
|
|
36
|
-
const subscriptionId = uuidv4()
|
|
37
|
-
const sub = { streamName, groupId, id, subscriptionId }
|
|
38
|
-
const subscription = new StreamItemSubscription<TData>(sub)
|
|
39
|
-
|
|
40
|
-
this.subscribe(subscription)
|
|
41
|
-
|
|
42
|
-
return subscription
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
/**
|
|
46
|
-
* Subscribe to a group in a stream.
|
|
47
|
-
*
|
|
48
|
-
* @argument streamName - The name of the stream to subscribe to.
|
|
49
|
-
* @argument groupId - The id of the group to subscribe to.
|
|
50
|
-
*/
|
|
51
|
-
subscribeGroup<TData extends { id: string }>(streamName: string, groupId: string): StreamGroupSubscription<TData> {
|
|
52
|
-
const subscriptionId = uuidv4()
|
|
53
|
-
const sub = { streamName, groupId, subscriptionId }
|
|
54
|
-
const subscription = new StreamGroupSubscription<TData>(sub)
|
|
55
|
-
|
|
56
|
-
this.subscribe(subscription)
|
|
57
|
-
|
|
58
|
-
return subscription
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
close() {
|
|
62
|
-
this.listeners = {} // clean up all listeners
|
|
63
|
-
this.ws.removeEventListener('message', this.messageListener)
|
|
64
|
-
this.ws.removeEventListener('open', this.onSocketOpen)
|
|
65
|
-
this.ws.removeEventListener('close', this.onSocketClose)
|
|
66
|
-
this.ws.close()
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
private onSocketClose(): void {
|
|
70
|
-
// retry to connect
|
|
71
|
-
setTimeout(() => this.createSocket(), 2000)
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
private onSocketOpen(): void {
|
|
75
|
-
Object.values(this.listeners).forEach((listeners) => {
|
|
76
|
-
listeners.forEach((subscription) => this.join(subscription))
|
|
77
|
-
})
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
messageListener(event: MessageEvent<string>): void {
|
|
81
|
-
const message: BaseMessage = JSON.parse(event.data)
|
|
82
|
-
const room = this.roomName(message)
|
|
83
|
-
|
|
84
|
-
this.listeners[room]?.forEach((listener) => listener.listener(message))
|
|
85
|
-
|
|
86
|
-
if (message.id) {
|
|
87
|
-
const groupRoom = this.roomName({
|
|
88
|
-
streamName: message.streamName,
|
|
89
|
-
groupId: message.groupId,
|
|
90
|
-
})
|
|
91
|
-
|
|
92
|
-
this.listeners[groupRoom]?.forEach((listener) => listener.listener(message))
|
|
93
|
-
}
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
97
|
-
private subscribe(subscription: StreamSubscription<any, any>): void {
|
|
98
|
-
const room = this.roomName(subscription.sub)
|
|
99
|
-
|
|
100
|
-
if (!this.listeners[room]) {
|
|
101
|
-
this.listeners[room] = new Set()
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
this.listeners[room].add(subscription)
|
|
105
|
-
this.join(subscription)
|
|
106
|
-
|
|
107
|
-
subscription.onClose(() => {
|
|
108
|
-
this.listeners[room]?.delete(subscription)
|
|
109
|
-
this.leave(subscription)
|
|
110
|
-
})
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
private join(subscription: StreamSubscription): void {
|
|
114
|
-
if (this.ws.readyState === WebSocket.OPEN) {
|
|
115
|
-
this.ws.send(JSON.stringify({ type: 'join', data: subscription.sub }))
|
|
116
|
-
}
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
private leave(subscription: StreamSubscription): void {
|
|
120
|
-
if (this.ws.readyState === WebSocket.OPEN) {
|
|
121
|
-
this.ws.send(JSON.stringify({ type: 'leave', data: subscription.sub }))
|
|
122
|
-
}
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
private roomName(message: BaseMessage): string {
|
|
126
|
-
return message.id
|
|
127
|
-
? `${message.streamName}:group:${message.groupId}:item:${message.id}`
|
|
128
|
-
: `${message.streamName}:group:${message.groupId}`
|
|
129
|
-
}
|
|
130
|
-
}
|
package/src/stream.types.ts
DELETED
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
export type BaseMessage = { streamName: string; groupId: string; id?: string }
|
|
2
|
-
export type JoinMessage = BaseMessage & { subscriptionId: string }
|
|
3
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
4
|
-
export type CustomEvent = { type: string; data: any }
|
|
5
|
-
export type StreamEvent<TData extends { id: string }> =
|
|
6
|
-
| { type: 'create'; data: TData }
|
|
7
|
-
| { type: 'update'; data: TData }
|
|
8
|
-
| { type: 'delete'; data: TData }
|
|
9
|
-
| { type: 'event'; event: CustomEvent }
|
|
10
|
-
export type ItemStreamEvent<TData extends { id: string }> = StreamEvent<TData> | { type: 'sync'; data: TData }
|
|
11
|
-
export type GroupStreamEvent<TData extends { id: string }> = StreamEvent<TData> | { type: 'sync'; data: TData[] }
|
|
12
|
-
export type ItemEventMessage<TData extends { id: string }> = BaseMessage & { event: ItemStreamEvent<TData> }
|
|
13
|
-
export type GroupEventMessage<TData extends { id: string }> = BaseMessage & { event: GroupStreamEvent<TData> }
|
|
14
|
-
|
|
15
|
-
export type Message = { type: 'join' | 'leave'; data: JoinMessage }
|
|
16
|
-
|
|
17
|
-
export type Listener<TData> = (state: TData | null) => void
|
|
18
|
-
export type CustomEventListener<TData> = (event: TData) => void
|
package/tsconfig.json
DELETED
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"compilerOptions": {
|
|
3
|
-
"target": "ES2020",
|
|
4
|
-
"module": "ESNext",
|
|
5
|
-
"moduleResolution": "bundler",
|
|
6
|
-
"esModuleInterop": true,
|
|
7
|
-
"strict": true,
|
|
8
|
-
"skipLibCheck": true,
|
|
9
|
-
"forceConsistentCasingInFileNames": true,
|
|
10
|
-
"resolveJsonModule": true,
|
|
11
|
-
"allowJs": true,
|
|
12
|
-
"outDir": "dist",
|
|
13
|
-
"rootDir": ".",
|
|
14
|
-
"baseUrl": ".",
|
|
15
|
-
"declaration": true,
|
|
16
|
-
"lib": ["dom"]
|
|
17
|
-
},
|
|
18
|
-
"include": ["src/*.ts", "index.ts"],
|
|
19
|
-
"exclude": [
|
|
20
|
-
"node_modules",
|
|
21
|
-
"dist"
|
|
22
|
-
]
|
|
23
|
-
}
|