@motiadev/stream-client-browser 0.5.2-beta.103 → 0.5.2-beta.104-330172

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/index.d.ts CHANGED
@@ -1,4 +1,2 @@
1
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';
2
+ export { StreamItemSubscription, StreamGroupSubscription, StreamSubscription } from '@motiadev/stream-client';
package/dist/index.js CHANGED
@@ -1,4 +1,2 @@
1
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';
2
+ export { StreamItemSubscription, StreamGroupSubscription, StreamSubscription } from '@motiadev/stream-client';
@@ -0,0 +1,16 @@
1
+ import type { SocketAdapter } from '@motiadev/stream-client';
2
+ export declare class StreamSocketAdapter implements SocketAdapter {
3
+ private address;
4
+ private ws;
5
+ private onMessageListeners;
6
+ private onOpenListeners;
7
+ private onCloseListeners;
8
+ constructor(address: string);
9
+ connect(): void;
10
+ send(message: string): void;
11
+ onMessage(callback: (message: string) => void): void;
12
+ onOpen(callback: () => void): void;
13
+ onClose(callback: () => void): void;
14
+ close(): void;
15
+ isOpen(): boolean;
16
+ }
@@ -0,0 +1,35 @@
1
+ export class StreamSocketAdapter {
2
+ constructor(address) {
3
+ this.address = address;
4
+ this.onMessageListeners = [];
5
+ this.onOpenListeners = [];
6
+ this.onCloseListeners = [];
7
+ this.ws = new WebSocket(this.address);
8
+ }
9
+ connect() { }
10
+ send(message) {
11
+ this.ws.send(message);
12
+ }
13
+ onMessage(callback) {
14
+ const listener = (message) => callback(message.data);
15
+ this.ws.addEventListener('message', listener);
16
+ this.onMessageListeners.push(listener);
17
+ }
18
+ onOpen(callback) {
19
+ this.ws.addEventListener('open', callback);
20
+ this.onOpenListeners.push(callback);
21
+ }
22
+ onClose(callback) {
23
+ this.ws.addEventListener('close', callback);
24
+ this.onCloseListeners.push(callback);
25
+ }
26
+ close() {
27
+ this.ws.close();
28
+ this.onMessageListeners.forEach((listener) => this.ws.removeEventListener('message', listener));
29
+ this.onOpenListeners.forEach((listener) => this.ws.removeEventListener('open', listener));
30
+ this.onCloseListeners.forEach((listener) => this.ws.removeEventListener('close', listener));
31
+ }
32
+ isOpen() {
33
+ return this.ws.readyState === WebSocket.OPEN;
34
+ }
35
+ }
@@ -1,36 +1,4 @@
1
- import { StreamGroupSubscription } from './stream-group';
2
- import { StreamItemSubscription } from './stream-item';
3
- export declare class Stream {
4
- private address;
5
- private ws;
6
- private listeners;
1
+ import { Stream as StreamClient } from '@motiadev/stream-client';
2
+ export declare class Stream extends StreamClient {
7
3
  constructor(address: string);
8
- createSocket(): WebSocket;
9
- /**
10
- * Subscribe to an item in a stream.
11
- *
12
- * @argument streamName - The name of the stream to subscribe to.
13
- * @argument groupId - The id of the group to subscribe to.
14
- * @argument id - The id of the item to subscribe to.
15
- */
16
- subscribeItem<TData extends {
17
- id: string;
18
- }>(streamName: string, groupId: string, id: string): StreamItemSubscription<TData>;
19
- /**
20
- * Subscribe to a group in a stream.
21
- *
22
- * @argument streamName - The name of the stream to subscribe to.
23
- * @argument groupId - The id of the group to subscribe to.
24
- */
25
- subscribeGroup<TData extends {
26
- id: string;
27
- }>(streamName: string, groupId: string, sortKey?: keyof TData): StreamGroupSubscription<TData>;
28
- close(): void;
29
- private onSocketClose;
30
- private onSocketOpen;
31
- messageListener(event: MessageEvent<string>): void;
32
- private subscribe;
33
- private join;
34
- private leave;
35
- private roomName;
36
4
  }
@@ -1,101 +1,7 @@
1
- import { v4 as uuidv4 } from 'uuid';
2
- import { StreamGroupSubscription } from './stream-group';
3
- import { StreamItemSubscription } from './stream-item';
4
- export class Stream {
1
+ import { Stream as StreamClient } from '@motiadev/stream-client';
2
+ import { StreamSocketAdapter } from './stream-adapter';
3
+ export class Stream extends StreamClient {
5
4
  constructor(address) {
6
- this.address = address;
7
- this.listeners = {};
8
- this.ws = this.createSocket();
9
- }
10
- createSocket() {
11
- this.ws = new WebSocket(this.address);
12
- this.ws.addEventListener('message', (message) => this.messageListener(message));
13
- this.ws.addEventListener('open', () => this.onSocketOpen());
14
- this.ws.addEventListener('close', () => 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.removeEventListener('message', this.messageListener);
47
- this.ws.removeEventListener('open', this.onSocketOpen);
48
- this.ws.removeEventListener('close', this.onSocketClose);
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.data);
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.readyState === WebSocket.OPEN) {
88
- this.ws.send(JSON.stringify({ type: 'join', data: subscription.sub }));
89
- }
90
- }
91
- leave(subscription) {
92
- if (this.ws.readyState === WebSocket.OPEN) {
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}`;
5
+ super(() => new StreamSocketAdapter(address));
100
6
  }
101
7
  }
package/index.ts CHANGED
@@ -1,4 +1,2 @@
1
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'
2
+ export { StreamItemSubscription, StreamGroupSubscription, StreamSubscription } from '@motiadev/stream-client'
package/package.json CHANGED
@@ -1,12 +1,13 @@
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.5.2-beta.103",
4
+ "version": "0.5.2-beta.104-330172",
5
5
  "license": "MIT",
6
6
  "main": "dist/index.js",
7
7
  "types": "dist/index.d.ts",
8
8
  "dependencies": {
9
- "uuid": "^11.1.0"
9
+ "uuid": "^11.1.0",
10
+ "@motiadev/stream-client": "0.5.2-beta.104-330172"
10
11
  },
11
12
  "devDependencies": {
12
13
  "@types/jest": "^29.5.14",
@@ -17,7 +18,6 @@
17
18
  },
18
19
  "scripts": {
19
20
  "build": "rm -rf dist && tsc",
20
- "lint": "eslint --config ../../eslint.config.js",
21
- "test": "jest"
21
+ "lint": "eslint --config ../../eslint.config.js"
22
22
  }
23
23
  }
@@ -1 +0,0 @@
1
- export {};
@@ -1,105 +0,0 @@
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
- });
@@ -1 +0,0 @@
1
- export {};
@@ -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,68 +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
- send(data) {
12
- this.listeners['message']?.forEach((cb) => cb({ data: JSON.stringify(data) }));
13
- }
14
- close() {
15
- this.listeners = {};
16
- }
17
- }
18
- function getSocket(stream) {
19
- return stream.ws;
20
- }
21
- function makeGroupMessage(type, data, timestamp = Date.now()) {
22
- return {
23
- streamName: 'test-stream',
24
- groupId: 'test-group',
25
- timestamp,
26
- event: { type, ...(type === 'event' ? { event: data } : { data }) },
27
- };
28
- }
29
- function makeItemMessage(type, data, timestamp = Date.now()) {
30
- return {
31
- streamName: 'test-stream',
32
- groupId: 'test-group',
33
- id: '1',
34
- timestamp,
35
- event: { type, ...(type === 'event' ? { event: data } : { data }) },
36
- };
37
- }
38
- describe('Stream', () => {
39
- beforeEach(() => {
40
- global.WebSocket = MockSocket;
41
- });
42
- afterEach(() => {
43
- delete global.WebSocket;
44
- });
45
- it('should sync group events', () => {
46
- const stream = new Stream('ws://localhost:3000');
47
- const socket = getSocket(stream);
48
- const sub = stream.subscribeGroup('test-stream', 'test-group');
49
- const syncData = [
50
- { id: '1', name: 'A' },
51
- { id: '2', name: 'B' },
52
- ];
53
- socket.send(makeGroupMessage('sync', syncData));
54
- expect(sub.getState()).toEqual(syncData);
55
- });
56
- it('should not sync item events in group subscriptions', () => {
57
- const stream = new Stream('ws://localhost:3000');
58
- const socket = getSocket(stream);
59
- const sub = stream.subscribeGroup('test-stream', 'test-group');
60
- const syncData = [
61
- { id: '1', name: 'A' },
62
- { id: '2', name: 'B' },
63
- ];
64
- socket.send(makeGroupMessage('sync', syncData));
65
- socket.send(makeItemMessage('sync', syncData[0]));
66
- expect(sub.getState()).toEqual(syncData);
67
- });
68
- });
@@ -1,13 +0,0 @@
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
- }
@@ -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 { 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
- }
@@ -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 { 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
- }
@@ -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 {};