@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
|
@@ -1,46 +0,0 @@
|
|
|
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
|
-
});
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
|
@@ -1,81 +0,0 @@
|
|
|
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
|
-
connect() { }
|
|
12
|
-
isOpen() {
|
|
13
|
-
return true;
|
|
14
|
-
}
|
|
15
|
-
onMessage(callback) {
|
|
16
|
-
this.addEventListener('message', callback);
|
|
17
|
-
}
|
|
18
|
-
onOpen(callback) {
|
|
19
|
-
this.addEventListener('open', callback);
|
|
20
|
-
}
|
|
21
|
-
onClose(callback) {
|
|
22
|
-
this.addEventListener('close', callback);
|
|
23
|
-
}
|
|
24
|
-
send(data) {
|
|
25
|
-
this.listeners['message']?.forEach((cb) => cb(data));
|
|
26
|
-
}
|
|
27
|
-
close() {
|
|
28
|
-
this.listeners = {};
|
|
29
|
-
}
|
|
30
|
-
}
|
|
31
|
-
function getSocket(stream) {
|
|
32
|
-
return stream.ws;
|
|
33
|
-
}
|
|
34
|
-
function makeGroupMessage(type, data, timestamp = Date.now()) {
|
|
35
|
-
return {
|
|
36
|
-
streamName: 'test-stream',
|
|
37
|
-
groupId: 'test-group',
|
|
38
|
-
timestamp,
|
|
39
|
-
event: { type, ...(type === 'event' ? { event: data } : { data }) },
|
|
40
|
-
};
|
|
41
|
-
}
|
|
42
|
-
function makeItemMessage(type, data, timestamp = Date.now()) {
|
|
43
|
-
return {
|
|
44
|
-
streamName: 'test-stream',
|
|
45
|
-
groupId: 'test-group',
|
|
46
|
-
id: '1',
|
|
47
|
-
timestamp,
|
|
48
|
-
event: { type, ...(type === 'event' ? { event: data } : { data }) },
|
|
49
|
-
};
|
|
50
|
-
}
|
|
51
|
-
describe('Stream', () => {
|
|
52
|
-
beforeEach(() => {
|
|
53
|
-
global.WebSocket = MockSocket;
|
|
54
|
-
});
|
|
55
|
-
afterEach(() => {
|
|
56
|
-
delete global.WebSocket;
|
|
57
|
-
});
|
|
58
|
-
it('should sync group events', () => {
|
|
59
|
-
const stream = new Stream(() => new MockSocket());
|
|
60
|
-
const socket = getSocket(stream);
|
|
61
|
-
const sub = stream.subscribeGroup('test-stream', 'test-group');
|
|
62
|
-
const syncData = [
|
|
63
|
-
{ id: '1', name: 'A' },
|
|
64
|
-
{ id: '2', name: 'B' },
|
|
65
|
-
];
|
|
66
|
-
socket.send(JSON.stringify(makeGroupMessage('sync', syncData)));
|
|
67
|
-
expect(sub.getState()).toEqual(syncData);
|
|
68
|
-
});
|
|
69
|
-
it('should not sync item events in group subscriptions', () => {
|
|
70
|
-
const stream = new Stream(() => new MockSocket());
|
|
71
|
-
const socket = getSocket(stream);
|
|
72
|
-
const sub = stream.subscribeGroup('test-stream', 'test-group');
|
|
73
|
-
const syncData = [
|
|
74
|
-
{ id: '1', name: 'A' },
|
|
75
|
-
{ id: '2', name: 'B' },
|
|
76
|
-
];
|
|
77
|
-
socket.send(JSON.stringify(makeGroupMessage('sync', syncData)));
|
|
78
|
-
socket.send(JSON.stringify(makeItemMessage('sync', syncData[0])));
|
|
79
|
-
expect(sub.getState()).toEqual(syncData);
|
|
80
|
-
});
|
|
81
|
-
});
|
package/dist/esm/index.d.ts
DELETED
|
@@ -1,7 +0,0 @@
|
|
|
1
|
-
export { SocketAdapterFactory } from './src/adapter-factory';
|
|
2
|
-
export { SocketAdapter } from './src/socket-adapter';
|
|
3
|
-
export { Stream } from './src/stream';
|
|
4
|
-
export * from './src/stream.types';
|
|
5
|
-
export { StreamGroupSubscription } from './src/stream-group';
|
|
6
|
-
export { StreamItemSubscription } from './src/stream-item';
|
|
7
|
-
export { StreamSubscription } from './src/stream-subscription';
|
package/dist/esm/index.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
import type { GroupEventMessage, JoinMessage } from './stream.types';
|
|
2
|
-
import { StreamSubscription } from './stream-subscription';
|
|
3
|
-
export declare class StreamGroupSubscription<TData extends {
|
|
4
|
-
id: string;
|
|
5
|
-
}> extends StreamSubscription<TData[], GroupEventMessage<TData>> {
|
|
6
|
-
private sortKey?;
|
|
7
|
-
private lastTimestamp;
|
|
8
|
-
private lastTimestampMap;
|
|
9
|
-
constructor(sub: JoinMessage, sortKey?: keyof TData | undefined);
|
|
10
|
-
private sort;
|
|
11
|
-
protected setState(state: TData[]): void;
|
|
12
|
-
listener(message: GroupEventMessage<TData>): void;
|
|
13
|
-
}
|
|
@@ -1,65 +0,0 @@
|
|
|
1
|
-
import { StreamSubscription } from './stream-subscription';
|
|
2
|
-
export class StreamGroupSubscription extends StreamSubscription {
|
|
3
|
-
constructor(sub, sortKey) {
|
|
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));
|
|
25
|
-
}
|
|
26
|
-
listener(message) {
|
|
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;
|
|
33
|
-
this.setState(message.event.data);
|
|
34
|
-
}
|
|
35
|
-
else if (message.event.type === 'create') {
|
|
36
|
-
const id = message.event.data.id;
|
|
37
|
-
const state = this.getState();
|
|
38
|
-
if (!state.find((item) => item.id === id)) {
|
|
39
|
-
this.setState([...state, message.event.data]);
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
else if (message.event.type === 'update') {
|
|
43
|
-
const messageData = message.event.data;
|
|
44
|
-
const messageDataId = messageData.id;
|
|
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);
|
|
52
|
-
this.setState(state.map((item) => (item.id === messageDataId ? messageData : item)));
|
|
53
|
-
}
|
|
54
|
-
else if (message.event.type === 'delete') {
|
|
55
|
-
const messageDataId = message.event.data.id;
|
|
56
|
-
const state = this.getState();
|
|
57
|
-
this.lastTimestamp = message.timestamp;
|
|
58
|
-
this.lastTimestampMap.set(messageDataId, message.timestamp);
|
|
59
|
-
this.setState(state.filter((item) => item.id !== messageDataId));
|
|
60
|
-
}
|
|
61
|
-
else if (message.event.type === 'event') {
|
|
62
|
-
this.onEventReceived(message.event.event);
|
|
63
|
-
}
|
|
64
|
-
}
|
|
65
|
-
}
|
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
import type { ItemEventMessage, JoinMessage } from './stream.types';
|
|
2
|
-
import { StreamSubscription } from './stream-subscription';
|
|
3
|
-
export declare class StreamItemSubscription<TData extends {
|
|
4
|
-
id: string;
|
|
5
|
-
}> extends StreamSubscription<TData | null, ItemEventMessage<TData>> {
|
|
6
|
-
private lastEventTimestamp;
|
|
7
|
-
constructor(sub: JoinMessage);
|
|
8
|
-
listener(message: ItemEventMessage<TData>): void;
|
|
9
|
-
}
|
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
import { StreamSubscription } from './stream-subscription';
|
|
2
|
-
export class StreamItemSubscription extends StreamSubscription {
|
|
3
|
-
constructor(sub) {
|
|
4
|
-
super(sub, null);
|
|
5
|
-
this.lastEventTimestamp = 0;
|
|
6
|
-
}
|
|
7
|
-
listener(message) {
|
|
8
|
-
if (message.timestamp <= this.lastEventTimestamp) {
|
|
9
|
-
return;
|
|
10
|
-
}
|
|
11
|
-
this.lastEventTimestamp = message.timestamp;
|
|
12
|
-
if (message.event.type === 'sync' || message.event.type === 'create' || message.event.type === 'update') {
|
|
13
|
-
this.setState(message.event.data);
|
|
14
|
-
}
|
|
15
|
-
else if (message.event.type === 'delete') {
|
|
16
|
-
this.setState(null);
|
|
17
|
-
}
|
|
18
|
-
else if (message.event.type === 'event') {
|
|
19
|
-
this.onEventReceived(message.event.event);
|
|
20
|
-
}
|
|
21
|
-
}
|
|
22
|
-
}
|
|
@@ -1,36 +0,0 @@
|
|
|
1
|
-
import type { CustomEvent, JoinMessage, Listener } from './stream.types';
|
|
2
|
-
type CustomEventListener = (event: any) => void;
|
|
3
|
-
export declare abstract class StreamSubscription<TData = unknown, TEventData = unknown> {
|
|
4
|
-
private customEventListeners;
|
|
5
|
-
private closeListeners;
|
|
6
|
-
private onChangeListeners;
|
|
7
|
-
private state;
|
|
8
|
-
readonly sub: JoinMessage;
|
|
9
|
-
constructor(sub: JoinMessage, state: TData);
|
|
10
|
-
abstract listener(message: TEventData): void;
|
|
11
|
-
protected onEventReceived(event: CustomEvent): void;
|
|
12
|
-
/**
|
|
13
|
-
* Add a custom event listener. This listener will be called whenever the custom event is received.
|
|
14
|
-
*/
|
|
15
|
-
onEvent(type: string, listener: CustomEventListener): void;
|
|
16
|
-
/**
|
|
17
|
-
* Remove a custom event listener.
|
|
18
|
-
*/
|
|
19
|
-
offEvent(type: string, listener: CustomEventListener): void;
|
|
20
|
-
onClose(listener: () => void): void;
|
|
21
|
-
close(): void;
|
|
22
|
-
/**
|
|
23
|
-
* Add a change listener. This listener will be called whenever the state of the group changes.
|
|
24
|
-
*/
|
|
25
|
-
addChangeListener(listener: Listener<TData>): void;
|
|
26
|
-
/**
|
|
27
|
-
* Remove a change listener.
|
|
28
|
-
*/
|
|
29
|
-
removeChangeListener(listener: Listener<TData>): void;
|
|
30
|
-
/**
|
|
31
|
-
* Get the current state of the group.
|
|
32
|
-
*/
|
|
33
|
-
getState(): TData;
|
|
34
|
-
protected setState(state: TData): void;
|
|
35
|
-
}
|
|
36
|
-
export {};
|
|
@@ -1,59 +0,0 @@
|
|
|
1
|
-
export class StreamSubscription {
|
|
2
|
-
constructor(sub, state) {
|
|
3
|
-
this.customEventListeners = new Map();
|
|
4
|
-
this.closeListeners = new Set();
|
|
5
|
-
this.onChangeListeners = new Set();
|
|
6
|
-
this.sub = sub;
|
|
7
|
-
this.state = state;
|
|
8
|
-
}
|
|
9
|
-
onEventReceived(event) {
|
|
10
|
-
const customEventListeners = this.customEventListeners.get(event.type);
|
|
11
|
-
if (customEventListeners) {
|
|
12
|
-
const eventData = event.data;
|
|
13
|
-
customEventListeners.forEach((listener) => listener(eventData));
|
|
14
|
-
}
|
|
15
|
-
}
|
|
16
|
-
/**
|
|
17
|
-
* Add a custom event listener. This listener will be called whenever the custom event is received.
|
|
18
|
-
*/
|
|
19
|
-
onEvent(type, listener) {
|
|
20
|
-
const listeners = this.customEventListeners.get(type) || [];
|
|
21
|
-
this.customEventListeners.set(type, [...listeners, listener]);
|
|
22
|
-
}
|
|
23
|
-
/**
|
|
24
|
-
* Remove a custom event listener.
|
|
25
|
-
*/
|
|
26
|
-
offEvent(type, listener) {
|
|
27
|
-
const listeners = this.customEventListeners.get(type) || [];
|
|
28
|
-
this.customEventListeners.set(type, listeners.filter((l) => l !== listener));
|
|
29
|
-
}
|
|
30
|
-
onClose(listener) {
|
|
31
|
-
this.closeListeners.add(listener);
|
|
32
|
-
}
|
|
33
|
-
close() {
|
|
34
|
-
this.closeListeners.forEach((listener) => listener());
|
|
35
|
-
this.closeListeners.clear();
|
|
36
|
-
}
|
|
37
|
-
/**
|
|
38
|
-
* Add a change listener. This listener will be called whenever the state of the group changes.
|
|
39
|
-
*/
|
|
40
|
-
addChangeListener(listener) {
|
|
41
|
-
this.onChangeListeners.add(listener);
|
|
42
|
-
}
|
|
43
|
-
/**
|
|
44
|
-
* Remove a change listener.
|
|
45
|
-
*/
|
|
46
|
-
removeChangeListener(listener) {
|
|
47
|
-
this.onChangeListeners.delete(listener);
|
|
48
|
-
}
|
|
49
|
-
/**
|
|
50
|
-
* Get the current state of the group.
|
|
51
|
-
*/
|
|
52
|
-
getState() {
|
|
53
|
-
return this.state;
|
|
54
|
-
}
|
|
55
|
-
setState(state) {
|
|
56
|
-
this.state = state;
|
|
57
|
-
this.onChangeListeners.forEach((listener) => listener(state));
|
|
58
|
-
}
|
|
59
|
-
}
|
package/dist/esm/src/stream.d.ts
DELETED
|
@@ -1,38 +0,0 @@
|
|
|
1
|
-
import type { SocketAdapterFactory } from './adapter-factory';
|
|
2
|
-
import type { SocketAdapter } from './socket-adapter';
|
|
3
|
-
import { StreamGroupSubscription } from './stream-group';
|
|
4
|
-
import { StreamItemSubscription } from './stream-item';
|
|
5
|
-
export declare class Stream {
|
|
6
|
-
private adapterFactory;
|
|
7
|
-
private ws;
|
|
8
|
-
private listeners;
|
|
9
|
-
constructor(adapterFactory: SocketAdapterFactory);
|
|
10
|
-
createSocket(): SocketAdapter;
|
|
11
|
-
/**
|
|
12
|
-
* Subscribe to an item in a stream.
|
|
13
|
-
*
|
|
14
|
-
* @argument streamName - The name of the stream to subscribe to.
|
|
15
|
-
* @argument groupId - The id of the group to subscribe to.
|
|
16
|
-
* @argument id - The id of the item to subscribe to.
|
|
17
|
-
*/
|
|
18
|
-
subscribeItem<TData extends {
|
|
19
|
-
id: string;
|
|
20
|
-
}>(streamName: string, groupId: string, id: string): StreamItemSubscription<TData>;
|
|
21
|
-
/**
|
|
22
|
-
* Subscribe to a group in a stream.
|
|
23
|
-
*
|
|
24
|
-
* @argument streamName - The name of the stream to subscribe to.
|
|
25
|
-
* @argument groupId - The id of the group to subscribe to.
|
|
26
|
-
*/
|
|
27
|
-
subscribeGroup<TData extends {
|
|
28
|
-
id: string;
|
|
29
|
-
}>(streamName: string, groupId: string, sortKey?: keyof TData): StreamGroupSubscription<TData>;
|
|
30
|
-
close(): void;
|
|
31
|
-
private onSocketClose;
|
|
32
|
-
private onSocketOpen;
|
|
33
|
-
messageListener(event: string): void;
|
|
34
|
-
private subscribe;
|
|
35
|
-
private join;
|
|
36
|
-
private leave;
|
|
37
|
-
private roomName;
|
|
38
|
-
}
|
package/dist/esm/src/stream.js
DELETED
|
@@ -1,98 +0,0 @@
|
|
|
1
|
-
import { v4 as uuidv4 } from 'uuid';
|
|
2
|
-
import { StreamGroupSubscription } from './stream-group';
|
|
3
|
-
import { StreamItemSubscription } from './stream-item';
|
|
4
|
-
export class Stream {
|
|
5
|
-
constructor(adapterFactory) {
|
|
6
|
-
this.adapterFactory = adapterFactory;
|
|
7
|
-
this.listeners = {};
|
|
8
|
-
this.ws = this.createSocket();
|
|
9
|
-
}
|
|
10
|
-
createSocket() {
|
|
11
|
-
this.ws = this.adapterFactory();
|
|
12
|
-
this.ws.onMessage((message) => this.messageListener(message));
|
|
13
|
-
this.ws.onOpen(() => this.onSocketOpen());
|
|
14
|
-
this.ws.onClose(() => this.onSocketClose());
|
|
15
|
-
return this.ws;
|
|
16
|
-
}
|
|
17
|
-
/**
|
|
18
|
-
* Subscribe to an item in a stream.
|
|
19
|
-
*
|
|
20
|
-
* @argument streamName - The name of the stream to subscribe to.
|
|
21
|
-
* @argument groupId - The id of the group to subscribe to.
|
|
22
|
-
* @argument id - The id of the item to subscribe to.
|
|
23
|
-
*/
|
|
24
|
-
subscribeItem(streamName, groupId, id) {
|
|
25
|
-
const subscriptionId = uuidv4();
|
|
26
|
-
const sub = { streamName, groupId, id, subscriptionId };
|
|
27
|
-
const subscription = new StreamItemSubscription(sub);
|
|
28
|
-
this.subscribe(subscription);
|
|
29
|
-
return subscription;
|
|
30
|
-
}
|
|
31
|
-
/**
|
|
32
|
-
* Subscribe to a group in a stream.
|
|
33
|
-
*
|
|
34
|
-
* @argument streamName - The name of the stream to subscribe to.
|
|
35
|
-
* @argument groupId - The id of the group to subscribe to.
|
|
36
|
-
*/
|
|
37
|
-
subscribeGroup(streamName, groupId, sortKey) {
|
|
38
|
-
const subscriptionId = uuidv4();
|
|
39
|
-
const sub = { streamName, groupId, subscriptionId };
|
|
40
|
-
const subscription = new StreamGroupSubscription(sub, sortKey);
|
|
41
|
-
this.subscribe(subscription);
|
|
42
|
-
return subscription;
|
|
43
|
-
}
|
|
44
|
-
close() {
|
|
45
|
-
this.listeners = {}; // clean up all listeners
|
|
46
|
-
this.ws.close();
|
|
47
|
-
}
|
|
48
|
-
onSocketClose() {
|
|
49
|
-
// retry to connect
|
|
50
|
-
setTimeout(() => this.createSocket(), 2000);
|
|
51
|
-
}
|
|
52
|
-
onSocketOpen() {
|
|
53
|
-
Object.values(this.listeners).forEach((listeners) => {
|
|
54
|
-
listeners.forEach((subscription) => this.join(subscription));
|
|
55
|
-
});
|
|
56
|
-
}
|
|
57
|
-
messageListener(event) {
|
|
58
|
-
const message = JSON.parse(event);
|
|
59
|
-
const room = this.roomName(message);
|
|
60
|
-
this.listeners[room]?.forEach((listener) => listener.listener(message));
|
|
61
|
-
// we need to discard sync to group subs when it's an item event
|
|
62
|
-
if (message.id && message.event.type !== 'sync') {
|
|
63
|
-
const groupRoom = this.roomName({
|
|
64
|
-
streamName: message.streamName,
|
|
65
|
-
groupId: message.groupId,
|
|
66
|
-
});
|
|
67
|
-
this.listeners[groupRoom]?.forEach((listener) => listener.listener(message));
|
|
68
|
-
}
|
|
69
|
-
}
|
|
70
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
71
|
-
subscribe(subscription) {
|
|
72
|
-
const room = this.roomName(subscription.sub);
|
|
73
|
-
if (!this.listeners[room]) {
|
|
74
|
-
this.listeners[room] = new Set();
|
|
75
|
-
}
|
|
76
|
-
this.listeners[room].add(subscription);
|
|
77
|
-
this.join(subscription);
|
|
78
|
-
subscription.onClose(() => {
|
|
79
|
-
this.listeners[room]?.delete(subscription);
|
|
80
|
-
this.leave(subscription);
|
|
81
|
-
});
|
|
82
|
-
}
|
|
83
|
-
join(subscription) {
|
|
84
|
-
if (this.ws.isOpen()) {
|
|
85
|
-
this.ws.send(JSON.stringify({ type: 'join', data: subscription.sub }));
|
|
86
|
-
}
|
|
87
|
-
}
|
|
88
|
-
leave(subscription) {
|
|
89
|
-
if (this.ws.isOpen()) {
|
|
90
|
-
this.ws.send(JSON.stringify({ type: 'leave', data: subscription.sub }));
|
|
91
|
-
}
|
|
92
|
-
}
|
|
93
|
-
roomName(message) {
|
|
94
|
-
return message.id
|
|
95
|
-
? `${message.streamName}:group:${message.groupId}:item:${message.id}`
|
|
96
|
-
: `${message.streamName}:group:${message.groupId}`;
|
|
97
|
-
}
|
|
98
|
-
}
|
|
@@ -1,56 +0,0 @@
|
|
|
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;
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
package/dist/types/index.d.ts
DELETED
|
@@ -1,7 +0,0 @@
|
|
|
1
|
-
export { SocketAdapterFactory } from './src/adapter-factory';
|
|
2
|
-
export { SocketAdapter } from './src/socket-adapter';
|
|
3
|
-
export { Stream } from './src/stream';
|
|
4
|
-
export * from './src/stream.types';
|
|
5
|
-
export { StreamGroupSubscription } from './src/stream-group';
|
|
6
|
-
export { StreamItemSubscription } from './src/stream-item';
|
|
7
|
-
export { StreamSubscription } from './src/stream-subscription';
|
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
import type { GroupEventMessage, JoinMessage } from './stream.types';
|
|
2
|
-
import { StreamSubscription } from './stream-subscription';
|
|
3
|
-
export declare class StreamGroupSubscription<TData extends {
|
|
4
|
-
id: string;
|
|
5
|
-
}> extends StreamSubscription<TData[], GroupEventMessage<TData>> {
|
|
6
|
-
private sortKey?;
|
|
7
|
-
private lastTimestamp;
|
|
8
|
-
private lastTimestampMap;
|
|
9
|
-
constructor(sub: JoinMessage, sortKey?: keyof TData | undefined);
|
|
10
|
-
private sort;
|
|
11
|
-
protected setState(state: TData[]): void;
|
|
12
|
-
listener(message: GroupEventMessage<TData>): void;
|
|
13
|
-
}
|
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
import type { ItemEventMessage, JoinMessage } from './stream.types';
|
|
2
|
-
import { StreamSubscription } from './stream-subscription';
|
|
3
|
-
export declare class StreamItemSubscription<TData extends {
|
|
4
|
-
id: string;
|
|
5
|
-
}> extends StreamSubscription<TData | null, ItemEventMessage<TData>> {
|
|
6
|
-
private lastEventTimestamp;
|
|
7
|
-
constructor(sub: JoinMessage);
|
|
8
|
-
listener(message: ItemEventMessage<TData>): void;
|
|
9
|
-
}
|
|
@@ -1,36 +0,0 @@
|
|
|
1
|
-
import type { CustomEvent, JoinMessage, Listener } from './stream.types';
|
|
2
|
-
type CustomEventListener = (event: any) => void;
|
|
3
|
-
export declare abstract class StreamSubscription<TData = unknown, TEventData = unknown> {
|
|
4
|
-
private customEventListeners;
|
|
5
|
-
private closeListeners;
|
|
6
|
-
private onChangeListeners;
|
|
7
|
-
private state;
|
|
8
|
-
readonly sub: JoinMessage;
|
|
9
|
-
constructor(sub: JoinMessage, state: TData);
|
|
10
|
-
abstract listener(message: TEventData): void;
|
|
11
|
-
protected onEventReceived(event: CustomEvent): void;
|
|
12
|
-
/**
|
|
13
|
-
* Add a custom event listener. This listener will be called whenever the custom event is received.
|
|
14
|
-
*/
|
|
15
|
-
onEvent(type: string, listener: CustomEventListener): void;
|
|
16
|
-
/**
|
|
17
|
-
* Remove a custom event listener.
|
|
18
|
-
*/
|
|
19
|
-
offEvent(type: string, listener: CustomEventListener): void;
|
|
20
|
-
onClose(listener: () => void): void;
|
|
21
|
-
close(): void;
|
|
22
|
-
/**
|
|
23
|
-
* Add a change listener. This listener will be called whenever the state of the group changes.
|
|
24
|
-
*/
|
|
25
|
-
addChangeListener(listener: Listener<TData>): void;
|
|
26
|
-
/**
|
|
27
|
-
* Remove a change listener.
|
|
28
|
-
*/
|
|
29
|
-
removeChangeListener(listener: Listener<TData>): void;
|
|
30
|
-
/**
|
|
31
|
-
* Get the current state of the group.
|
|
32
|
-
*/
|
|
33
|
-
getState(): TData;
|
|
34
|
-
protected setState(state: TData): void;
|
|
35
|
-
}
|
|
36
|
-
export {};
|