@motiadev/stream-client 0.13.2-beta.164-110989 → 0.14.0-beta.164
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/cjs/__tests__/stream-group.spec.d.ts +1 -0
- package/dist/cjs/__tests__/stream-group.spec.js +107 -0
- package/dist/cjs/__tests__/stream-item.spec.d.ts +1 -0
- package/dist/cjs/__tests__/stream-item.spec.js +48 -0
- package/dist/cjs/__tests__/stream.spec.d.ts +1 -0
- package/dist/cjs/__tests__/stream.spec.js +83 -0
- package/dist/cjs/index.d.ts +7 -0
- package/dist/cjs/index.js +26 -0
- package/dist/cjs/src/adapter-factory.d.ts +2 -0
- package/dist/cjs/src/adapter-factory.js +2 -0
- package/dist/cjs/src/socket-adapter.d.ts +9 -0
- package/dist/cjs/src/socket-adapter.js +2 -0
- package/dist/cjs/src/stream-group.d.ts +13 -0
- package/dist/cjs/src/stream-group.js +69 -0
- package/dist/cjs/src/stream-item.d.ts +9 -0
- package/dist/cjs/src/stream-item.js +26 -0
- package/dist/cjs/src/stream-subscription.d.ts +36 -0
- package/dist/cjs/src/stream-subscription.js +63 -0
- package/dist/cjs/src/stream.d.ts +38 -0
- package/dist/cjs/src/stream.js +102 -0
- package/dist/cjs/src/stream.types.d.ts +56 -0
- package/dist/cjs/src/stream.types.js +2 -0
- package/dist/esm/__tests__/stream-group.spec.d.ts +1 -0
- package/dist/esm/__tests__/stream-group.spec.js +105 -0
- package/dist/esm/__tests__/stream-item.spec.d.ts +1 -0
- package/dist/esm/__tests__/stream-item.spec.js +46 -0
- package/dist/esm/__tests__/stream.spec.d.ts +1 -0
- package/dist/esm/__tests__/stream.spec.js +81 -0
- package/dist/esm/index.d.ts +7 -0
- package/dist/esm/index.js +5 -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-group.js +65 -0
- package/dist/esm/src/stream-item.d.ts +9 -0
- package/dist/esm/src/stream-item.js +22 -0
- package/dist/esm/src/stream-subscription.d.ts +36 -0
- package/dist/esm/src/stream-subscription.js +59 -0
- package/dist/esm/src/stream.d.ts +38 -0
- package/dist/esm/src/stream.js +98 -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 +22 -17
- package/scripts/build.sh +14 -0
- package/__mocks__/uuid.ts +0 -10
- package/dist/index.d.ts +0 -167
- package/dist/index.js +0 -230
- package/tsdown.config.ts +0 -18
|
@@ -0,0 +1,98 @@
|
|
|
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
|
+
}
|
|
@@ -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 { 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';
|
|
@@ -0,0 +1,13 @@
|
|
|
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
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
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
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
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 {};
|
|
@@ -0,0 +1,38 @@
|
|
|
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
|
+
}
|
|
@@ -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,30 +1,35 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@motiadev/stream-client",
|
|
3
3
|
"description": "Motia Stream Client Package – Responsible for managing streams of data.",
|
|
4
|
-
"version": "0.
|
|
4
|
+
"version": "0.14.0-beta.164",
|
|
5
5
|
"license": "Elastic-2.0",
|
|
6
|
-
"
|
|
7
|
-
"
|
|
8
|
-
"
|
|
9
|
-
"
|
|
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
|
+
},
|
|
10
21
|
"dependencies": {
|
|
11
|
-
"uuid": "^
|
|
22
|
+
"uuid": "^11.1.0"
|
|
12
23
|
},
|
|
13
24
|
"devDependencies": {
|
|
14
|
-
"@types/jest": "^
|
|
25
|
+
"@types/jest": "^29.5.14",
|
|
15
26
|
"@types/ws": "^8.18.1",
|
|
16
|
-
"jest": "^
|
|
17
|
-
"ts-jest": "^29.
|
|
18
|
-
"
|
|
19
|
-
"typescript": "^5.9.3"
|
|
20
|
-
},
|
|
21
|
-
"exports": {
|
|
22
|
-
".": "./dist/index.js",
|
|
23
|
-
"./package.json": "./package.json"
|
|
27
|
+
"jest": "^29.7.0",
|
|
28
|
+
"ts-jest": "^29.3.2",
|
|
29
|
+
"typescript": "^5.7.2"
|
|
24
30
|
},
|
|
25
31
|
"scripts": {
|
|
26
|
-
"build": "
|
|
27
|
-
"dev": "tsdown --watch",
|
|
32
|
+
"build": "sh scripts/build.sh",
|
|
28
33
|
"lint": "eslint --config ../../eslint.config.js",
|
|
29
34
|
"test": "jest"
|
|
30
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/__mocks__/uuid.ts
DELETED
package/dist/index.d.ts
DELETED
|
@@ -1,167 +0,0 @@
|
|
|
1
|
-
//#region src/socket-adapter.d.ts
|
|
2
|
-
interface SocketAdapter {
|
|
3
|
-
connect(): void;
|
|
4
|
-
close(): void;
|
|
5
|
-
send(message: string): void;
|
|
6
|
-
isOpen(): boolean;
|
|
7
|
-
onMessage(callback: (message: string) => void): void;
|
|
8
|
-
onOpen(callback: () => void): void;
|
|
9
|
-
onClose(callback: () => void): void;
|
|
10
|
-
}
|
|
11
|
-
//#endregion
|
|
12
|
-
//#region src/adapter-factory.d.ts
|
|
13
|
-
type SocketAdapterFactory = () => SocketAdapter;
|
|
14
|
-
//#endregion
|
|
15
|
-
//#region src/stream.types.d.ts
|
|
16
|
-
type BaseMessage = {
|
|
17
|
-
streamName: string;
|
|
18
|
-
groupId: string;
|
|
19
|
-
id?: string;
|
|
20
|
-
timestamp: number;
|
|
21
|
-
};
|
|
22
|
-
type JoinMessage = Omit<BaseMessage, 'timestamp'> & {
|
|
23
|
-
subscriptionId: string;
|
|
24
|
-
};
|
|
25
|
-
type CustomEvent = {
|
|
26
|
-
type: string;
|
|
27
|
-
data: any;
|
|
28
|
-
};
|
|
29
|
-
type StreamEvent<TData extends {
|
|
30
|
-
id: string;
|
|
31
|
-
}> = {
|
|
32
|
-
type: 'create';
|
|
33
|
-
data: TData;
|
|
34
|
-
} | {
|
|
35
|
-
type: 'update';
|
|
36
|
-
data: TData;
|
|
37
|
-
} | {
|
|
38
|
-
type: 'delete';
|
|
39
|
-
data: TData;
|
|
40
|
-
} | {
|
|
41
|
-
type: 'event';
|
|
42
|
-
event: CustomEvent;
|
|
43
|
-
};
|
|
44
|
-
type ItemStreamEvent<TData extends {
|
|
45
|
-
id: string;
|
|
46
|
-
}> = StreamEvent<TData> | {
|
|
47
|
-
type: 'sync';
|
|
48
|
-
data: TData;
|
|
49
|
-
};
|
|
50
|
-
type GroupStreamEvent<TData extends {
|
|
51
|
-
id: string;
|
|
52
|
-
}> = StreamEvent<TData> | {
|
|
53
|
-
type: 'sync';
|
|
54
|
-
data: TData[];
|
|
55
|
-
};
|
|
56
|
-
type ItemEventMessage<TData extends {
|
|
57
|
-
id: string;
|
|
58
|
-
}> = BaseMessage & {
|
|
59
|
-
event: ItemStreamEvent<TData>;
|
|
60
|
-
};
|
|
61
|
-
type GroupEventMessage<TData extends {
|
|
62
|
-
id: string;
|
|
63
|
-
}> = BaseMessage & {
|
|
64
|
-
event: GroupStreamEvent<TData>;
|
|
65
|
-
};
|
|
66
|
-
type Message = {
|
|
67
|
-
type: 'join' | 'leave';
|
|
68
|
-
data: JoinMessage;
|
|
69
|
-
};
|
|
70
|
-
type Listener<TData> = (state: TData | null) => void;
|
|
71
|
-
type CustomEventListener<TData> = (event: TData) => void;
|
|
72
|
-
//#endregion
|
|
73
|
-
//#region src/stream-subscription.d.ts
|
|
74
|
-
type CustomEventListener$1 = (event: any) => void;
|
|
75
|
-
declare abstract class StreamSubscription<TData = unknown, TEventData = unknown> {
|
|
76
|
-
private customEventListeners;
|
|
77
|
-
private closeListeners;
|
|
78
|
-
private onChangeListeners;
|
|
79
|
-
private state;
|
|
80
|
-
readonly sub: JoinMessage;
|
|
81
|
-
constructor(sub: JoinMessage, state: TData);
|
|
82
|
-
abstract listener(message: TEventData): void;
|
|
83
|
-
protected onEventReceived(event: CustomEvent): void;
|
|
84
|
-
/**
|
|
85
|
-
* Add a custom event listener. This listener will be called whenever the custom event is received.
|
|
86
|
-
*/
|
|
87
|
-
onEvent(type: string, listener: CustomEventListener$1): void;
|
|
88
|
-
/**
|
|
89
|
-
* Remove a custom event listener.
|
|
90
|
-
*/
|
|
91
|
-
offEvent(type: string, listener: CustomEventListener$1): void;
|
|
92
|
-
onClose(listener: () => void): void;
|
|
93
|
-
close(): void;
|
|
94
|
-
/**
|
|
95
|
-
* Add a change listener. This listener will be called whenever the state of the group changes.
|
|
96
|
-
*/
|
|
97
|
-
addChangeListener(listener: Listener<TData>): void;
|
|
98
|
-
/**
|
|
99
|
-
* Remove a change listener.
|
|
100
|
-
*/
|
|
101
|
-
removeChangeListener(listener: Listener<TData>): void;
|
|
102
|
-
/**
|
|
103
|
-
* Get the current state of the group.
|
|
104
|
-
*/
|
|
105
|
-
getState(): TData;
|
|
106
|
-
protected setState(state: TData): void;
|
|
107
|
-
}
|
|
108
|
-
//#endregion
|
|
109
|
-
//#region src/stream-group.d.ts
|
|
110
|
-
declare class StreamGroupSubscription<TData extends {
|
|
111
|
-
id: string;
|
|
112
|
-
}> extends StreamSubscription<TData[], GroupEventMessage<TData>> {
|
|
113
|
-
private sortKey?;
|
|
114
|
-
private lastTimestamp;
|
|
115
|
-
private lastTimestampMap;
|
|
116
|
-
constructor(sub: JoinMessage, sortKey?: keyof TData | undefined);
|
|
117
|
-
private sort;
|
|
118
|
-
protected setState(state: TData[]): void;
|
|
119
|
-
listener(message: GroupEventMessage<TData>): void;
|
|
120
|
-
}
|
|
121
|
-
//#endregion
|
|
122
|
-
//#region src/stream-item.d.ts
|
|
123
|
-
declare class StreamItemSubscription<TData extends {
|
|
124
|
-
id: string;
|
|
125
|
-
}> extends StreamSubscription<TData | null, ItemEventMessage<TData>> {
|
|
126
|
-
private lastEventTimestamp;
|
|
127
|
-
constructor(sub: JoinMessage);
|
|
128
|
-
listener(message: ItemEventMessage<TData>): void;
|
|
129
|
-
}
|
|
130
|
-
//#endregion
|
|
131
|
-
//#region src/stream.d.ts
|
|
132
|
-
declare class Stream {
|
|
133
|
-
private adapterFactory;
|
|
134
|
-
private ws;
|
|
135
|
-
private listeners;
|
|
136
|
-
constructor(adapterFactory: SocketAdapterFactory);
|
|
137
|
-
createSocket(): SocketAdapter;
|
|
138
|
-
/**
|
|
139
|
-
* Subscribe to an item in a stream.
|
|
140
|
-
*
|
|
141
|
-
* @argument streamName - The name of the stream to subscribe to.
|
|
142
|
-
* @argument groupId - The id of the group to subscribe to.
|
|
143
|
-
* @argument id - The id of the item to subscribe to.
|
|
144
|
-
*/
|
|
145
|
-
subscribeItem<TData extends {
|
|
146
|
-
id: string;
|
|
147
|
-
}>(streamName: string, groupId: string, id: string): StreamItemSubscription<TData>;
|
|
148
|
-
/**
|
|
149
|
-
* Subscribe to a group in a stream.
|
|
150
|
-
*
|
|
151
|
-
* @argument streamName - The name of the stream to subscribe to.
|
|
152
|
-
* @argument groupId - The id of the group to subscribe to.
|
|
153
|
-
*/
|
|
154
|
-
subscribeGroup<TData extends {
|
|
155
|
-
id: string;
|
|
156
|
-
}>(streamName: string, groupId: string, sortKey?: keyof TData): StreamGroupSubscription<TData>;
|
|
157
|
-
close(): void;
|
|
158
|
-
private onSocketClose;
|
|
159
|
-
private onSocketOpen;
|
|
160
|
-
messageListener(event: string): void;
|
|
161
|
-
private subscribe;
|
|
162
|
-
private join;
|
|
163
|
-
private leave;
|
|
164
|
-
private roomName;
|
|
165
|
-
}
|
|
166
|
-
//#endregion
|
|
167
|
-
export { BaseMessage, CustomEvent, CustomEventListener, GroupEventMessage, GroupStreamEvent, ItemEventMessage, ItemStreamEvent, JoinMessage, Listener, Message, type SocketAdapter, type SocketAdapterFactory, Stream, StreamEvent, StreamGroupSubscription, StreamItemSubscription, StreamSubscription };
|