@motiadev/stream-client 0.5.2-beta.104-330172 → 0.5.2-beta.104-266644
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/README.md +3 -182
- package/dist/cjs/__tests__/stream-group.spec.js +107 -0
- package/dist/cjs/__tests__/stream-item.spec.js +48 -0
- package/dist/cjs/__tests__/stream.spec.js +83 -0
- package/dist/cjs/index.js +26 -0
- package/dist/cjs/src/adapter-factory.js +2 -0
- package/dist/cjs/src/socket-adapter.js +2 -0
- package/dist/cjs/src/stream-group.js +69 -0
- package/dist/cjs/src/stream-item.js +26 -0
- package/dist/cjs/src/stream-subscription.js +63 -0
- package/dist/cjs/src/stream.js +102 -0
- package/dist/cjs/src/stream.types.js +2 -0
- package/dist/esm/index.d.ts +7 -0
- package/dist/esm/src/adapter-factory.d.ts +2 -0
- package/dist/esm/src/adapter-factory.js +1 -0
- package/dist/esm/src/socket-adapter.d.ts +9 -0
- package/dist/esm/src/socket-adapter.js +1 -0
- package/dist/esm/src/stream-group.d.ts +13 -0
- package/dist/esm/src/stream-item.d.ts +9 -0
- package/dist/esm/src/stream-subscription.d.ts +36 -0
- package/dist/esm/src/stream.d.ts +38 -0
- package/dist/esm/src/stream.types.d.ts +56 -0
- package/dist/esm/src/stream.types.js +1 -0
- package/dist/types/__tests__/stream-group.spec.d.ts +1 -0
- package/dist/types/__tests__/stream-item.spec.d.ts +1 -0
- package/dist/types/__tests__/stream.spec.d.ts +1 -0
- package/dist/types/index.d.ts +7 -0
- package/dist/types/src/adapter-factory.d.ts +2 -0
- package/dist/types/src/socket-adapter.d.ts +9 -0
- package/dist/types/src/stream-group.d.ts +13 -0
- package/dist/types/src/stream-item.d.ts +9 -0
- package/dist/types/src/stream-subscription.d.ts +36 -0
- package/dist/types/src/stream.d.ts +38 -0
- package/dist/types/src/stream.types.d.ts +56 -0
- package/package.json +17 -4
- package/scripts/build.sh +14 -0
- package/index.ts +0 -7
- /package/dist/{__tests__ → cjs/__tests__}/stream-group.spec.d.ts +0 -0
- /package/dist/{__tests__ → cjs/__tests__}/stream-item.spec.d.ts +0 -0
- /package/dist/{__tests__ → cjs/__tests__}/stream.spec.d.ts +0 -0
- /package/dist/{index.d.ts → cjs/index.d.ts} +0 -0
- /package/dist/{src → cjs/src}/adapter-factory.d.ts +0 -0
- /package/dist/{src → cjs/src}/socket-adapter.d.ts +0 -0
- /package/dist/{src → cjs/src}/stream-group.d.ts +0 -0
- /package/dist/{src → cjs/src}/stream-item.d.ts +0 -0
- /package/dist/{src → cjs/src}/stream-subscription.d.ts +0 -0
- /package/dist/{src → cjs/src}/stream.d.ts +0 -0
- /package/dist/{src → cjs/src}/stream.types.d.ts +0 -0
- /package/dist/{src/adapter-factory.js → esm/__tests__/stream-group.spec.d.ts} +0 -0
- /package/dist/{__tests__ → esm/__tests__}/stream-group.spec.js +0 -0
- /package/dist/{src/socket-adapter.js → esm/__tests__/stream-item.spec.d.ts} +0 -0
- /package/dist/{__tests__ → esm/__tests__}/stream-item.spec.js +0 -0
- /package/dist/{src/stream.types.js → esm/__tests__/stream.spec.d.ts} +0 -0
- /package/dist/{__tests__ → esm/__tests__}/stream.spec.js +0 -0
- /package/dist/{index.js → esm/index.js} +0 -0
- /package/dist/{src → esm/src}/stream-group.js +0 -0
- /package/dist/{src → esm/src}/stream-item.js +0 -0
- /package/dist/{src → esm/src}/stream-subscription.js +0 -0
- /package/dist/{src → esm/src}/stream.js +0 -0
package/README.md
CHANGED
|
@@ -1,185 +1,6 @@
|
|
|
1
1
|
# @motiadev/stream-client
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
This is a Core project used by
|
|
4
4
|
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
- **WebSocket-based streaming** for real-time data updates
|
|
8
|
-
- **Item and group subscriptions** with automatic state management
|
|
9
|
-
- **Change listeners** for reactive UI updates
|
|
10
|
-
- **Custom event handling** for extensibility
|
|
11
|
-
- **TypeScript support** for strong typing and autocompletion
|
|
12
|
-
|
|
13
|
-
---
|
|
14
|
-
|
|
15
|
-
## Installation
|
|
16
|
-
|
|
17
|
-
```bash
|
|
18
|
-
npm install @motiadev/stream-client
|
|
19
|
-
```
|
|
20
|
-
|
|
21
|
-
---
|
|
22
|
-
|
|
23
|
-
## Usage
|
|
24
|
-
|
|
25
|
-
### 1. Creating a Stream Connection
|
|
26
|
-
|
|
27
|
-
```typescript
|
|
28
|
-
import { Stream } from '@motiadev/stream-client-browser'
|
|
29
|
-
|
|
30
|
-
const stream = new Stream('wss://your-stream-server', () => {
|
|
31
|
-
console.log('WebSocket connection established!')
|
|
32
|
-
})
|
|
33
|
-
```
|
|
34
|
-
|
|
35
|
-
### 2. Subscribing to an Item Stream
|
|
36
|
-
|
|
37
|
-
```typescript
|
|
38
|
-
const itemSubscription = stream.subscribeItem<{ id: string; name: string }>('users', 'user-123')
|
|
39
|
-
|
|
40
|
-
itemSubscription.addChangeListener((user) => {
|
|
41
|
-
console.log('User updated:', user)
|
|
42
|
-
})
|
|
43
|
-
|
|
44
|
-
// Listen for custom events
|
|
45
|
-
itemSubscription.onEvent('custom-event', (data) => {
|
|
46
|
-
console.log('Received custom event:', data)
|
|
47
|
-
})
|
|
48
|
-
```
|
|
49
|
-
|
|
50
|
-
### 3. Subscribing to a Group Stream
|
|
51
|
-
|
|
52
|
-
```typescript
|
|
53
|
-
const groupSubscription = stream.subscribeGroup<{ id: string; name: string }>('users', 'group-abc')
|
|
54
|
-
|
|
55
|
-
groupSubscription.addChangeListener((users) => {
|
|
56
|
-
console.log('Group users updated:', users)
|
|
57
|
-
})
|
|
58
|
-
```
|
|
59
|
-
|
|
60
|
-
### 4. Cleaning Up
|
|
61
|
-
|
|
62
|
-
```typescript
|
|
63
|
-
itemSubscription.close()
|
|
64
|
-
groupSubscription.close()
|
|
65
|
-
stream.close()
|
|
66
|
-
```
|
|
67
|
-
|
|
68
|
-
---
|
|
69
|
-
|
|
70
|
-
## API Reference
|
|
71
|
-
|
|
72
|
-
### `Stream`
|
|
73
|
-
|
|
74
|
-
- **constructor(address: string, onReady: () => void)**
|
|
75
|
-
|
|
76
|
-
- Establishes a WebSocket connection to the given address.
|
|
77
|
-
- Calls `onReady` when the connection is open.
|
|
78
|
-
|
|
79
|
-
- **subscribeItem<T>(streamName: string, id: string): StreamItemSubscription<T>**
|
|
80
|
-
|
|
81
|
-
- Subscribes to a single item in a stream.
|
|
82
|
-
- Returns a `StreamItemSubscription` instance.
|
|
83
|
-
|
|
84
|
-
- **subscribeGroup<T>(streamName: string, groupId: string): StreamGroupSubscription<T>**
|
|
85
|
-
|
|
86
|
-
- Subscribes to a group of items in a stream.
|
|
87
|
-
- Returns a `StreamGroupSubscription` instance.
|
|
88
|
-
|
|
89
|
-
- **close()**
|
|
90
|
-
- Closes the WebSocket connection.
|
|
91
|
-
|
|
92
|
-
---
|
|
93
|
-
|
|
94
|
-
### `StreamItemSubscription<T>`
|
|
95
|
-
|
|
96
|
-
- **addChangeListener(listener: (item: T | null) => void)**
|
|
97
|
-
|
|
98
|
-
- Registers a callback for when the item changes.
|
|
99
|
-
|
|
100
|
-
- **removeChangeListener(listener)**
|
|
101
|
-
|
|
102
|
-
- Unregisters a change listener.
|
|
103
|
-
|
|
104
|
-
- **onEvent(type: string, listener: (event: any) => void)**
|
|
105
|
-
|
|
106
|
-
- Registers a custom event listener.
|
|
107
|
-
|
|
108
|
-
- **offEvent(type: string, listener)**
|
|
109
|
-
|
|
110
|
-
- Unregisters a custom event listener.
|
|
111
|
-
|
|
112
|
-
- **getState(): T | null**
|
|
113
|
-
|
|
114
|
-
- Returns the current state of the item.
|
|
115
|
-
|
|
116
|
-
- **close()**
|
|
117
|
-
- Unsubscribes from the item stream and cleans up listeners.
|
|
118
|
-
|
|
119
|
-
---
|
|
120
|
-
|
|
121
|
-
### `StreamGroupSubscription<T>`
|
|
122
|
-
|
|
123
|
-
- **addChangeListener(listener: (items: T[]) => void)**
|
|
124
|
-
|
|
125
|
-
- Registers a callback for when the group changes.
|
|
126
|
-
|
|
127
|
-
- **removeChangeListener(listener)**
|
|
128
|
-
|
|
129
|
-
- Unregisters a change listener.
|
|
130
|
-
|
|
131
|
-
- **onEvent(type: string, listener: (event: any) => void)**
|
|
132
|
-
|
|
133
|
-
- Registers a custom event listener.
|
|
134
|
-
|
|
135
|
-
- **offEvent(type: string, listener: (event: any) => void)**
|
|
136
|
-
|
|
137
|
-
- Unregisters a custom event listener.
|
|
138
|
-
|
|
139
|
-
- **getState(): T[]**
|
|
140
|
-
|
|
141
|
-
- Returns the current state of the group.
|
|
142
|
-
|
|
143
|
-
- **close()**
|
|
144
|
-
- Unsubscribes from the group stream and cleans up listeners.
|
|
145
|
-
|
|
146
|
-
---
|
|
147
|
-
|
|
148
|
-
### `StreamSubscription` (Base Class)
|
|
149
|
-
|
|
150
|
-
- **onEvent(type: string, listener: (event: any) => void)**
|
|
151
|
-
- **offEvent(type: string, listener: (event: any) => void)**
|
|
152
|
-
|
|
153
|
-
---
|
|
154
|
-
|
|
155
|
-
## Types
|
|
156
|
-
|
|
157
|
-
All types are exported from `stream.types.ts` for advanced usage and type safety.
|
|
158
|
-
|
|
159
|
-
---
|
|
160
|
-
|
|
161
|
-
## Example
|
|
162
|
-
|
|
163
|
-
```typescript
|
|
164
|
-
import { Stream } from '@motiadev/stream-client-browser'
|
|
165
|
-
|
|
166
|
-
const stream = new Stream('wss://example.com', () => {
|
|
167
|
-
const userSub = stream.subscribeItem<{ id: string; name: string }>('users', 'user-1')
|
|
168
|
-
|
|
169
|
-
userSub.addChangeListener((user) => {
|
|
170
|
-
// React to user changes
|
|
171
|
-
})
|
|
172
|
-
|
|
173
|
-
const groupSub = stream.subscribeGroup<{ id: string; name: string }>('users', 'group-1')
|
|
174
|
-
|
|
175
|
-
groupSub.addChangeListener((users) => {
|
|
176
|
-
// React to group changes
|
|
177
|
-
})
|
|
178
|
-
})
|
|
179
|
-
```
|
|
180
|
-
|
|
181
|
-
---
|
|
182
|
-
|
|
183
|
-
## License
|
|
184
|
-
|
|
185
|
-
MIT
|
|
5
|
+
- [@motiadev/stream-client-browser](https://www.npmjs.com/package/@motiadev/stream-client-browser)
|
|
6
|
+
- [@motiadev/stream-client-node](https://www.npmjs.com/package/@motiadev/stream-client-node)
|
|
@@ -0,0 +1,107 @@
|
|
|
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
|
+
});
|
|
@@ -0,0 +1,48 @@
|
|
|
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
|
+
});
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const stream_1 = require("../src/stream");
|
|
4
|
+
class MockSocket {
|
|
5
|
+
constructor() {
|
|
6
|
+
this.listeners = {};
|
|
7
|
+
}
|
|
8
|
+
addEventListener(event, cb) {
|
|
9
|
+
if (!this.listeners[event])
|
|
10
|
+
this.listeners[event] = [];
|
|
11
|
+
this.listeners[event].push(cb);
|
|
12
|
+
}
|
|
13
|
+
connect() { }
|
|
14
|
+
isOpen() {
|
|
15
|
+
return true;
|
|
16
|
+
}
|
|
17
|
+
onMessage(callback) {
|
|
18
|
+
this.addEventListener('message', callback);
|
|
19
|
+
}
|
|
20
|
+
onOpen(callback) {
|
|
21
|
+
this.addEventListener('open', callback);
|
|
22
|
+
}
|
|
23
|
+
onClose(callback) {
|
|
24
|
+
this.addEventListener('close', callback);
|
|
25
|
+
}
|
|
26
|
+
send(data) {
|
|
27
|
+
this.listeners['message']?.forEach((cb) => cb(data));
|
|
28
|
+
}
|
|
29
|
+
close() {
|
|
30
|
+
this.listeners = {};
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
function getSocket(stream) {
|
|
34
|
+
return stream.ws;
|
|
35
|
+
}
|
|
36
|
+
function makeGroupMessage(type, data, timestamp = Date.now()) {
|
|
37
|
+
return {
|
|
38
|
+
streamName: 'test-stream',
|
|
39
|
+
groupId: 'test-group',
|
|
40
|
+
timestamp,
|
|
41
|
+
event: { type, ...(type === 'event' ? { event: data } : { data }) },
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
function makeItemMessage(type, data, timestamp = Date.now()) {
|
|
45
|
+
return {
|
|
46
|
+
streamName: 'test-stream',
|
|
47
|
+
groupId: 'test-group',
|
|
48
|
+
id: '1',
|
|
49
|
+
timestamp,
|
|
50
|
+
event: { type, ...(type === 'event' ? { event: data } : { data }) },
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
describe('Stream', () => {
|
|
54
|
+
beforeEach(() => {
|
|
55
|
+
global.WebSocket = MockSocket;
|
|
56
|
+
});
|
|
57
|
+
afterEach(() => {
|
|
58
|
+
delete global.WebSocket;
|
|
59
|
+
});
|
|
60
|
+
it('should sync group events', () => {
|
|
61
|
+
const stream = new stream_1.Stream(() => new MockSocket());
|
|
62
|
+
const socket = getSocket(stream);
|
|
63
|
+
const sub = stream.subscribeGroup('test-stream', 'test-group');
|
|
64
|
+
const syncData = [
|
|
65
|
+
{ id: '1', name: 'A' },
|
|
66
|
+
{ id: '2', name: 'B' },
|
|
67
|
+
];
|
|
68
|
+
socket.send(JSON.stringify(makeGroupMessage('sync', syncData)));
|
|
69
|
+
expect(sub.getState()).toEqual(syncData);
|
|
70
|
+
});
|
|
71
|
+
it('should not sync item events in group subscriptions', () => {
|
|
72
|
+
const stream = new stream_1.Stream(() => new MockSocket());
|
|
73
|
+
const socket = getSocket(stream);
|
|
74
|
+
const sub = stream.subscribeGroup('test-stream', 'test-group');
|
|
75
|
+
const syncData = [
|
|
76
|
+
{ id: '1', name: 'A' },
|
|
77
|
+
{ id: '2', name: 'B' },
|
|
78
|
+
];
|
|
79
|
+
socket.send(JSON.stringify(makeGroupMessage('sync', syncData)));
|
|
80
|
+
socket.send(JSON.stringify(makeItemMessage('sync', syncData[0])));
|
|
81
|
+
expect(sub.getState()).toEqual(syncData);
|
|
82
|
+
});
|
|
83
|
+
});
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
exports.StreamSubscription = exports.StreamGroupSubscription = exports.StreamItemSubscription = exports.Stream = void 0;
|
|
18
|
+
var stream_1 = require("./src/stream");
|
|
19
|
+
Object.defineProperty(exports, "Stream", { enumerable: true, get: function () { return stream_1.Stream; } });
|
|
20
|
+
var stream_item_1 = require("./src/stream-item");
|
|
21
|
+
Object.defineProperty(exports, "StreamItemSubscription", { enumerable: true, get: function () { return stream_item_1.StreamItemSubscription; } });
|
|
22
|
+
var stream_group_1 = require("./src/stream-group");
|
|
23
|
+
Object.defineProperty(exports, "StreamGroupSubscription", { enumerable: true, get: function () { return stream_group_1.StreamGroupSubscription; } });
|
|
24
|
+
var stream_subscription_1 = require("./src/stream-subscription");
|
|
25
|
+
Object.defineProperty(exports, "StreamSubscription", { enumerable: true, get: function () { return stream_subscription_1.StreamSubscription; } });
|
|
26
|
+
__exportStar(require("./src/stream.types"), exports);
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.StreamGroupSubscription = void 0;
|
|
4
|
+
const stream_subscription_1 = require("./stream-subscription");
|
|
5
|
+
class StreamGroupSubscription extends stream_subscription_1.StreamSubscription {
|
|
6
|
+
constructor(sub, sortKey) {
|
|
7
|
+
super(sub, []);
|
|
8
|
+
this.sortKey = sortKey;
|
|
9
|
+
this.lastTimestamp = 0;
|
|
10
|
+
this.lastTimestampMap = new Map();
|
|
11
|
+
}
|
|
12
|
+
sort(state) {
|
|
13
|
+
const sortKey = this.sortKey;
|
|
14
|
+
if (sortKey) {
|
|
15
|
+
return state.sort((a, b) => {
|
|
16
|
+
const aValue = a[sortKey];
|
|
17
|
+
const bValue = b[sortKey];
|
|
18
|
+
if (aValue && bValue) {
|
|
19
|
+
return aValue.toString().localeCompare(bValue.toString());
|
|
20
|
+
}
|
|
21
|
+
return 0;
|
|
22
|
+
});
|
|
23
|
+
}
|
|
24
|
+
return state;
|
|
25
|
+
}
|
|
26
|
+
setState(state) {
|
|
27
|
+
super.setState(this.sort(state));
|
|
28
|
+
}
|
|
29
|
+
listener(message) {
|
|
30
|
+
if (message.event.type === 'sync') {
|
|
31
|
+
if (message.timestamp < this.lastTimestamp) {
|
|
32
|
+
return;
|
|
33
|
+
}
|
|
34
|
+
this.lastTimestampMap = new Map();
|
|
35
|
+
this.lastTimestamp = message.timestamp;
|
|
36
|
+
this.setState(message.event.data);
|
|
37
|
+
}
|
|
38
|
+
else if (message.event.type === 'create') {
|
|
39
|
+
const id = message.event.data.id;
|
|
40
|
+
const state = this.getState();
|
|
41
|
+
if (!state.find((item) => item.id === id)) {
|
|
42
|
+
this.setState([...state, message.event.data]);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
else if (message.event.type === 'update') {
|
|
46
|
+
const messageData = message.event.data;
|
|
47
|
+
const messageDataId = messageData.id;
|
|
48
|
+
const state = this.getState();
|
|
49
|
+
const currentItemTimestamp = this.lastTimestampMap.get(messageDataId);
|
|
50
|
+
if (currentItemTimestamp && currentItemTimestamp >= message.timestamp) {
|
|
51
|
+
return;
|
|
52
|
+
}
|
|
53
|
+
this.lastTimestamp = message.timestamp;
|
|
54
|
+
this.lastTimestampMap.set(messageDataId, message.timestamp);
|
|
55
|
+
this.setState(state.map((item) => (item.id === messageDataId ? messageData : item)));
|
|
56
|
+
}
|
|
57
|
+
else if (message.event.type === 'delete') {
|
|
58
|
+
const messageDataId = message.event.data.id;
|
|
59
|
+
const state = this.getState();
|
|
60
|
+
this.lastTimestamp = message.timestamp;
|
|
61
|
+
this.lastTimestampMap.set(messageDataId, message.timestamp);
|
|
62
|
+
this.setState(state.filter((item) => item.id !== messageDataId));
|
|
63
|
+
}
|
|
64
|
+
else if (message.event.type === 'event') {
|
|
65
|
+
this.onEventReceived(message.event.event);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
exports.StreamGroupSubscription = StreamGroupSubscription;
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.StreamItemSubscription = void 0;
|
|
4
|
+
const stream_subscription_1 = require("./stream-subscription");
|
|
5
|
+
class StreamItemSubscription extends stream_subscription_1.StreamSubscription {
|
|
6
|
+
constructor(sub) {
|
|
7
|
+
super(sub, null);
|
|
8
|
+
this.lastEventTimestamp = 0;
|
|
9
|
+
}
|
|
10
|
+
listener(message) {
|
|
11
|
+
if (message.timestamp <= this.lastEventTimestamp) {
|
|
12
|
+
return;
|
|
13
|
+
}
|
|
14
|
+
this.lastEventTimestamp = message.timestamp;
|
|
15
|
+
if (message.event.type === 'sync' || message.event.type === 'create' || message.event.type === 'update') {
|
|
16
|
+
this.setState(message.event.data);
|
|
17
|
+
}
|
|
18
|
+
else if (message.event.type === 'delete') {
|
|
19
|
+
this.setState(null);
|
|
20
|
+
}
|
|
21
|
+
else if (message.event.type === 'event') {
|
|
22
|
+
this.onEventReceived(message.event.event);
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
exports.StreamItemSubscription = StreamItemSubscription;
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.StreamSubscription = void 0;
|
|
4
|
+
class StreamSubscription {
|
|
5
|
+
constructor(sub, state) {
|
|
6
|
+
this.customEventListeners = new Map();
|
|
7
|
+
this.closeListeners = new Set();
|
|
8
|
+
this.onChangeListeners = 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
|
+
exports.StreamSubscription = StreamSubscription;
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Stream = void 0;
|
|
4
|
+
const uuid_1 = require("uuid");
|
|
5
|
+
const stream_group_1 = require("./stream-group");
|
|
6
|
+
const stream_item_1 = require("./stream-item");
|
|
7
|
+
class Stream {
|
|
8
|
+
constructor(adapterFactory) {
|
|
9
|
+
this.adapterFactory = adapterFactory;
|
|
10
|
+
this.listeners = {};
|
|
11
|
+
this.ws = this.createSocket();
|
|
12
|
+
}
|
|
13
|
+
createSocket() {
|
|
14
|
+
this.ws = this.adapterFactory();
|
|
15
|
+
this.ws.onMessage((message) => this.messageListener(message));
|
|
16
|
+
this.ws.onOpen(() => this.onSocketOpen());
|
|
17
|
+
this.ws.onClose(() => this.onSocketClose());
|
|
18
|
+
return this.ws;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Subscribe to an item in a stream.
|
|
22
|
+
*
|
|
23
|
+
* @argument streamName - The name of the stream to subscribe to.
|
|
24
|
+
* @argument groupId - The id of the group to subscribe to.
|
|
25
|
+
* @argument id - The id of the item to subscribe to.
|
|
26
|
+
*/
|
|
27
|
+
subscribeItem(streamName, groupId, id) {
|
|
28
|
+
const subscriptionId = (0, uuid_1.v4)();
|
|
29
|
+
const sub = { streamName, groupId, id, subscriptionId };
|
|
30
|
+
const subscription = new stream_item_1.StreamItemSubscription(sub);
|
|
31
|
+
this.subscribe(subscription);
|
|
32
|
+
return subscription;
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Subscribe to a group in a stream.
|
|
36
|
+
*
|
|
37
|
+
* @argument streamName - The name of the stream to subscribe to.
|
|
38
|
+
* @argument groupId - The id of the group to subscribe to.
|
|
39
|
+
*/
|
|
40
|
+
subscribeGroup(streamName, groupId, sortKey) {
|
|
41
|
+
const subscriptionId = (0, uuid_1.v4)();
|
|
42
|
+
const sub = { streamName, groupId, subscriptionId };
|
|
43
|
+
const subscription = new stream_group_1.StreamGroupSubscription(sub, sortKey);
|
|
44
|
+
this.subscribe(subscription);
|
|
45
|
+
return subscription;
|
|
46
|
+
}
|
|
47
|
+
close() {
|
|
48
|
+
this.listeners = {}; // clean up all listeners
|
|
49
|
+
this.ws.close();
|
|
50
|
+
}
|
|
51
|
+
onSocketClose() {
|
|
52
|
+
// retry to connect
|
|
53
|
+
setTimeout(() => this.createSocket(), 2000);
|
|
54
|
+
}
|
|
55
|
+
onSocketOpen() {
|
|
56
|
+
Object.values(this.listeners).forEach((listeners) => {
|
|
57
|
+
listeners.forEach((subscription) => this.join(subscription));
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
messageListener(event) {
|
|
61
|
+
const message = JSON.parse(event);
|
|
62
|
+
const room = this.roomName(message);
|
|
63
|
+
this.listeners[room]?.forEach((listener) => listener.listener(message));
|
|
64
|
+
// we need to discard sync to group subs when it's an item event
|
|
65
|
+
if (message.id && message.event.type !== 'sync') {
|
|
66
|
+
const groupRoom = this.roomName({
|
|
67
|
+
streamName: message.streamName,
|
|
68
|
+
groupId: message.groupId,
|
|
69
|
+
});
|
|
70
|
+
this.listeners[groupRoom]?.forEach((listener) => listener.listener(message));
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
74
|
+
subscribe(subscription) {
|
|
75
|
+
const room = this.roomName(subscription.sub);
|
|
76
|
+
if (!this.listeners[room]) {
|
|
77
|
+
this.listeners[room] = new Set();
|
|
78
|
+
}
|
|
79
|
+
this.listeners[room].add(subscription);
|
|
80
|
+
this.join(subscription);
|
|
81
|
+
subscription.onClose(() => {
|
|
82
|
+
this.listeners[room]?.delete(subscription);
|
|
83
|
+
this.leave(subscription);
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
join(subscription) {
|
|
87
|
+
if (this.ws.isOpen()) {
|
|
88
|
+
this.ws.send(JSON.stringify({ type: 'join', data: subscription.sub }));
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
leave(subscription) {
|
|
92
|
+
if (this.ws.isOpen()) {
|
|
93
|
+
this.ws.send(JSON.stringify({ type: 'leave', data: subscription.sub }));
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
roomName(message) {
|
|
97
|
+
return message.id
|
|
98
|
+
? `${message.streamName}:group:${message.groupId}:item:${message.id}`
|
|
99
|
+
: `${message.streamName}:group:${message.groupId}`;
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
exports.Stream = Stream;
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export { Stream } from './src/stream';
|
|
2
|
+
export { StreamItemSubscription } from './src/stream-item';
|
|
3
|
+
export { StreamGroupSubscription } from './src/stream-group';
|
|
4
|
+
export { StreamSubscription } from './src/stream-subscription';
|
|
5
|
+
export { SocketAdapter } from './src/socket-adapter';
|
|
6
|
+
export { SocketAdapterFactory } from './src/adapter-factory';
|
|
7
|
+
export * from './src/stream.types';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { StreamSubscription } from './stream-subscription';
|
|
2
|
+
import { GroupEventMessage, JoinMessage } from './stream.types';
|
|
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 { StreamSubscription } from './stream-subscription';
|
|
2
|
+
import { ItemEventMessage, JoinMessage } from './stream.types';
|
|
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 { 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 { StreamGroupSubscription } from './stream-group';
|
|
2
|
+
import { StreamItemSubscription } from './stream-item';
|
|
3
|
+
import { SocketAdapter } from './socket-adapter';
|
|
4
|
+
import { SocketAdapterFactory } from './adapter-factory';
|
|
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;
|
|
@@ -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 { Stream } from './src/stream';
|
|
2
|
+
export { StreamItemSubscription } from './src/stream-item';
|
|
3
|
+
export { StreamGroupSubscription } from './src/stream-group';
|
|
4
|
+
export { StreamSubscription } from './src/stream-subscription';
|
|
5
|
+
export { SocketAdapter } from './src/socket-adapter';
|
|
6
|
+
export { SocketAdapterFactory } from './src/adapter-factory';
|
|
7
|
+
export * from './src/stream.types';
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { StreamSubscription } from './stream-subscription';
|
|
2
|
+
import { GroupEventMessage, JoinMessage } from './stream.types';
|
|
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 { StreamSubscription } from './stream-subscription';
|
|
2
|
+
import { ItemEventMessage, JoinMessage } from './stream.types';
|
|
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 { 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 { StreamGroupSubscription } from './stream-group';
|
|
2
|
+
import { StreamItemSubscription } from './stream-item';
|
|
3
|
+
import { SocketAdapter } from './socket-adapter';
|
|
4
|
+
import { SocketAdapterFactory } from './adapter-factory';
|
|
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,10 +1,23 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@motiadev/stream-client",
|
|
3
3
|
"description": "Motia Stream Client Package – Responsible for managing streams of data.",
|
|
4
|
-
"version": "0.5.2-beta.104-
|
|
4
|
+
"version": "0.5.2-beta.104-266644",
|
|
5
5
|
"license": "MIT",
|
|
6
|
-
"main": "dist/index.js",
|
|
7
|
-
"
|
|
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
|
+
},
|
|
8
21
|
"dependencies": {
|
|
9
22
|
"uuid": "^11.1.0"
|
|
10
23
|
},
|
|
@@ -16,7 +29,7 @@
|
|
|
16
29
|
"typescript": "^5.7.2"
|
|
17
30
|
},
|
|
18
31
|
"scripts": {
|
|
19
|
-
"build": "
|
|
32
|
+
"build": "sh scripts/build.sh",
|
|
20
33
|
"lint": "eslint --config ../../eslint.config.js",
|
|
21
34
|
"test": "jest"
|
|
22
35
|
}
|
package/scripts/build.sh
ADDED
|
@@ -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/index.ts
DELETED
|
@@ -1,7 +0,0 @@
|
|
|
1
|
-
export { Stream } from './src/stream'
|
|
2
|
-
export { StreamItemSubscription } from './src/stream-item'
|
|
3
|
-
export { StreamGroupSubscription } from './src/stream-group'
|
|
4
|
-
export { StreamSubscription } from './src/stream-subscription'
|
|
5
|
-
export { SocketAdapter } from './src/socket-adapter'
|
|
6
|
-
export { SocketAdapterFactory } from './src/adapter-factory'
|
|
7
|
-
export * from './src/stream.types'
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|