@motiadev/stream-client 0.13.0 → 0.13.1-beta.163-660633
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/LICENSE +93 -21
- package/__mocks__/uuid.ts +10 -0
- package/dist/index.d.ts +167 -0
- package/dist/index.js +230 -0
- package/package.json +18 -23
- 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
package/LICENSE
CHANGED
|
@@ -1,21 +1,93 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
1
|
+
Elastic License 2.0
|
|
2
|
+
|
|
3
|
+
URL: https://www.elastic.co/licensing/elastic-license
|
|
4
|
+
|
|
5
|
+
## Acceptance
|
|
6
|
+
|
|
7
|
+
By using the software, you agree to all of the terms and conditions below.
|
|
8
|
+
|
|
9
|
+
## Copyright License
|
|
10
|
+
|
|
11
|
+
The licensor grants you a non-exclusive, royalty-free, worldwide,
|
|
12
|
+
non-sublicensable, non-transferable license to use, copy, distribute, make
|
|
13
|
+
available, and prepare derivative works of the software, in each case subject
|
|
14
|
+
to the limitations and conditions below.
|
|
15
|
+
|
|
16
|
+
## Limitations
|
|
17
|
+
|
|
18
|
+
You may not provide the software to third parties as a hosted or managed
|
|
19
|
+
service, where the service provides users with access to any substantial set of
|
|
20
|
+
the features or functionality of the software.
|
|
21
|
+
|
|
22
|
+
You may not move, change, disable, or circumvent the license key functionality
|
|
23
|
+
in the software, and you may not remove or obscure any functionality in the
|
|
24
|
+
software that is protected by the license key.
|
|
25
|
+
|
|
26
|
+
You may not alter, remove, or obscure any licensing, copyright, or other notices
|
|
27
|
+
of the licensor in the software. Any use of the licensor's trademarks is subject
|
|
28
|
+
to applicable law.
|
|
29
|
+
|
|
30
|
+
## Patents
|
|
31
|
+
|
|
32
|
+
The licensor grants you a license, under any patent claims the licensor can
|
|
33
|
+
license, or becomes able to license, to make, have made, use, sell, offer for
|
|
34
|
+
sale, import and have imported the software, in each case subject to the
|
|
35
|
+
limitations and conditions in this license. This license does not cover any
|
|
36
|
+
patent claims that you cause to be infringed by modifications or additions to
|
|
37
|
+
the software. If you or your company make any written claim that the software
|
|
38
|
+
infringes or contributes to infringement of any patent, your patent license for
|
|
39
|
+
the software granted under these terms ends immediately. If your company makes
|
|
40
|
+
such a claim, your patent license ends immediately for work on behalf of your
|
|
41
|
+
company.
|
|
42
|
+
|
|
43
|
+
## Notices
|
|
44
|
+
|
|
45
|
+
You must ensure that anyone who gets a copy of any part of the software from you
|
|
46
|
+
also gets a copy of these terms.
|
|
47
|
+
|
|
48
|
+
If you modify the software, you must include in any modified copies of the
|
|
49
|
+
software prominent notices stating that you have modified the software.
|
|
50
|
+
|
|
51
|
+
## No Other Rights
|
|
52
|
+
|
|
53
|
+
These terms do not imply any licenses other than those expressly granted in
|
|
54
|
+
these terms.
|
|
55
|
+
|
|
56
|
+
## Termination
|
|
57
|
+
|
|
58
|
+
If you use the software in violation of these terms, such use is not licensed,
|
|
59
|
+
and your licenses will automatically terminate. If the licensor provides you
|
|
60
|
+
with a notice of your violation, and you cease all violation of this license no
|
|
61
|
+
later than 30 days after you receive that notice, your licenses will be
|
|
62
|
+
reinstated retroactively. However, if you violate these terms after such
|
|
63
|
+
reinstatement, any additional violation of these terms will cause your licenses
|
|
64
|
+
to terminate automatically and permanently.
|
|
65
|
+
|
|
66
|
+
## No Liability
|
|
67
|
+
|
|
68
|
+
*As far as the law allows, the software comes as is, without any warranty or
|
|
69
|
+
condition, and the licensor will not be liable to you for any damages arising
|
|
70
|
+
out of these terms or the use or nature of the software, under any kind of
|
|
71
|
+
legal claim.*
|
|
72
|
+
|
|
73
|
+
## Definitions
|
|
74
|
+
|
|
75
|
+
The **licensor** is the entity offering these terms, and the **software** is the
|
|
76
|
+
software the licensor makes available under these terms, including any portion
|
|
77
|
+
of it.
|
|
78
|
+
|
|
79
|
+
**you** refers to the individual or entity agreeing to these terms.
|
|
80
|
+
|
|
81
|
+
**your company** is any legal entity, sole proprietorship, or other kind of
|
|
82
|
+
organization that you work for, plus all organizations that have control over,
|
|
83
|
+
are under the control of, or are under common control with that organization.
|
|
84
|
+
**control** means ownership of substantially all the assets of an entity, or the
|
|
85
|
+
power to direct its management and policies by vote, contract, or otherwise.
|
|
86
|
+
Control can be direct or indirect.
|
|
87
|
+
|
|
88
|
+
**your licenses** are all the licenses granted to you for the software under
|
|
89
|
+
these terms.
|
|
90
|
+
|
|
91
|
+
**use** means anything you do with the software requiring one of your licenses.
|
|
92
|
+
|
|
93
|
+
**trademark** means trademarks, service marks, and similar rights.
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,167 @@
|
|
|
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 };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,230 @@
|
|
|
1
|
+
import { v4 } from "uuid";
|
|
2
|
+
|
|
3
|
+
//#region src/stream-subscription.ts
|
|
4
|
+
var StreamSubscription = class {
|
|
5
|
+
constructor(sub, state) {
|
|
6
|
+
this.customEventListeners = /* @__PURE__ */ new Map();
|
|
7
|
+
this.closeListeners = /* @__PURE__ */ new Set();
|
|
8
|
+
this.onChangeListeners = /* @__PURE__ */ 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
|
+
|
|
64
|
+
//#endregion
|
|
65
|
+
//#region src/stream-group.ts
|
|
66
|
+
var StreamGroupSubscription = class extends StreamSubscription {
|
|
67
|
+
constructor(sub, sortKey) {
|
|
68
|
+
super(sub, []);
|
|
69
|
+
this.sortKey = sortKey;
|
|
70
|
+
this.lastTimestamp = 0;
|
|
71
|
+
this.lastTimestampMap = /* @__PURE__ */ new Map();
|
|
72
|
+
}
|
|
73
|
+
sort(state) {
|
|
74
|
+
const sortKey = this.sortKey;
|
|
75
|
+
if (sortKey) return state.sort((a, b) => {
|
|
76
|
+
const aValue = a[sortKey];
|
|
77
|
+
const bValue = b[sortKey];
|
|
78
|
+
if (aValue && bValue) return aValue.toString().localeCompare(bValue.toString());
|
|
79
|
+
return 0;
|
|
80
|
+
});
|
|
81
|
+
return state;
|
|
82
|
+
}
|
|
83
|
+
setState(state) {
|
|
84
|
+
super.setState(this.sort(state));
|
|
85
|
+
}
|
|
86
|
+
listener(message) {
|
|
87
|
+
if (message.event.type === "sync") {
|
|
88
|
+
if (message.timestamp < this.lastTimestamp) return;
|
|
89
|
+
this.lastTimestampMap = /* @__PURE__ */ new Map();
|
|
90
|
+
this.lastTimestamp = message.timestamp;
|
|
91
|
+
this.setState(message.event.data);
|
|
92
|
+
} else if (message.event.type === "create") {
|
|
93
|
+
const id = message.event.data.id;
|
|
94
|
+
const state = this.getState();
|
|
95
|
+
if (!state.find((item) => item.id === id)) this.setState([...state, message.event.data]);
|
|
96
|
+
} else if (message.event.type === "update") {
|
|
97
|
+
const messageData = message.event.data;
|
|
98
|
+
const messageDataId = messageData.id;
|
|
99
|
+
const state = this.getState();
|
|
100
|
+
const currentItemTimestamp = this.lastTimestampMap.get(messageDataId);
|
|
101
|
+
if (currentItemTimestamp && currentItemTimestamp >= message.timestamp) return;
|
|
102
|
+
this.lastTimestamp = message.timestamp;
|
|
103
|
+
this.lastTimestampMap.set(messageDataId, message.timestamp);
|
|
104
|
+
this.setState(state.map((item) => item.id === messageDataId ? messageData : item));
|
|
105
|
+
} else if (message.event.type === "delete") {
|
|
106
|
+
const messageDataId = message.event.data.id;
|
|
107
|
+
const state = this.getState();
|
|
108
|
+
this.lastTimestamp = message.timestamp;
|
|
109
|
+
this.lastTimestampMap.set(messageDataId, message.timestamp);
|
|
110
|
+
this.setState(state.filter((item) => item.id !== messageDataId));
|
|
111
|
+
} else if (message.event.type === "event") this.onEventReceived(message.event.event);
|
|
112
|
+
}
|
|
113
|
+
};
|
|
114
|
+
|
|
115
|
+
//#endregion
|
|
116
|
+
//#region src/stream-item.ts
|
|
117
|
+
var StreamItemSubscription = class extends StreamSubscription {
|
|
118
|
+
constructor(sub) {
|
|
119
|
+
super(sub, null);
|
|
120
|
+
this.lastEventTimestamp = 0;
|
|
121
|
+
}
|
|
122
|
+
listener(message) {
|
|
123
|
+
if (message.timestamp <= this.lastEventTimestamp) return;
|
|
124
|
+
this.lastEventTimestamp = message.timestamp;
|
|
125
|
+
if (message.event.type === "sync" || message.event.type === "create" || message.event.type === "update") this.setState(message.event.data);
|
|
126
|
+
else if (message.event.type === "delete") this.setState(null);
|
|
127
|
+
else if (message.event.type === "event") this.onEventReceived(message.event.event);
|
|
128
|
+
}
|
|
129
|
+
};
|
|
130
|
+
|
|
131
|
+
//#endregion
|
|
132
|
+
//#region src/stream.ts
|
|
133
|
+
var Stream = class {
|
|
134
|
+
constructor(adapterFactory) {
|
|
135
|
+
this.adapterFactory = adapterFactory;
|
|
136
|
+
this.listeners = {};
|
|
137
|
+
this.ws = this.createSocket();
|
|
138
|
+
}
|
|
139
|
+
createSocket() {
|
|
140
|
+
this.ws = this.adapterFactory();
|
|
141
|
+
this.ws.onMessage((message) => this.messageListener(message));
|
|
142
|
+
this.ws.onOpen(() => this.onSocketOpen());
|
|
143
|
+
this.ws.onClose(() => this.onSocketClose());
|
|
144
|
+
return this.ws;
|
|
145
|
+
}
|
|
146
|
+
/**
|
|
147
|
+
* Subscribe to an item in a stream.
|
|
148
|
+
*
|
|
149
|
+
* @argument streamName - The name of the stream to subscribe to.
|
|
150
|
+
* @argument groupId - The id of the group to subscribe to.
|
|
151
|
+
* @argument id - The id of the item to subscribe to.
|
|
152
|
+
*/
|
|
153
|
+
subscribeItem(streamName, groupId, id) {
|
|
154
|
+
const subscription = new StreamItemSubscription({
|
|
155
|
+
streamName,
|
|
156
|
+
groupId,
|
|
157
|
+
id,
|
|
158
|
+
subscriptionId: v4()
|
|
159
|
+
});
|
|
160
|
+
this.subscribe(subscription);
|
|
161
|
+
return subscription;
|
|
162
|
+
}
|
|
163
|
+
/**
|
|
164
|
+
* Subscribe to a group in a stream.
|
|
165
|
+
*
|
|
166
|
+
* @argument streamName - The name of the stream to subscribe to.
|
|
167
|
+
* @argument groupId - The id of the group to subscribe to.
|
|
168
|
+
*/
|
|
169
|
+
subscribeGroup(streamName, groupId, sortKey) {
|
|
170
|
+
const subscription = new StreamGroupSubscription({
|
|
171
|
+
streamName,
|
|
172
|
+
groupId,
|
|
173
|
+
subscriptionId: v4()
|
|
174
|
+
}, sortKey);
|
|
175
|
+
this.subscribe(subscription);
|
|
176
|
+
return subscription;
|
|
177
|
+
}
|
|
178
|
+
close() {
|
|
179
|
+
this.listeners = {};
|
|
180
|
+
this.ws.close();
|
|
181
|
+
}
|
|
182
|
+
onSocketClose() {
|
|
183
|
+
setTimeout(() => this.createSocket(), 2e3);
|
|
184
|
+
}
|
|
185
|
+
onSocketOpen() {
|
|
186
|
+
Object.values(this.listeners).forEach((listeners) => {
|
|
187
|
+
listeners.forEach((subscription) => this.join(subscription));
|
|
188
|
+
});
|
|
189
|
+
}
|
|
190
|
+
messageListener(event) {
|
|
191
|
+
const message = JSON.parse(event);
|
|
192
|
+
const room = this.roomName(message);
|
|
193
|
+
this.listeners[room]?.forEach((listener) => listener.listener(message));
|
|
194
|
+
if (message.id && message.event.type !== "sync") {
|
|
195
|
+
const groupRoom = this.roomName({
|
|
196
|
+
streamName: message.streamName,
|
|
197
|
+
groupId: message.groupId
|
|
198
|
+
});
|
|
199
|
+
this.listeners[groupRoom]?.forEach((listener) => listener.listener(message));
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
subscribe(subscription) {
|
|
203
|
+
const room = this.roomName(subscription.sub);
|
|
204
|
+
if (!this.listeners[room]) this.listeners[room] = /* @__PURE__ */ new Set();
|
|
205
|
+
this.listeners[room].add(subscription);
|
|
206
|
+
this.join(subscription);
|
|
207
|
+
subscription.onClose(() => {
|
|
208
|
+
this.listeners[room]?.delete(subscription);
|
|
209
|
+
this.leave(subscription);
|
|
210
|
+
});
|
|
211
|
+
}
|
|
212
|
+
join(subscription) {
|
|
213
|
+
if (this.ws.isOpen()) this.ws.send(JSON.stringify({
|
|
214
|
+
type: "join",
|
|
215
|
+
data: subscription.sub
|
|
216
|
+
}));
|
|
217
|
+
}
|
|
218
|
+
leave(subscription) {
|
|
219
|
+
if (this.ws.isOpen()) this.ws.send(JSON.stringify({
|
|
220
|
+
type: "leave",
|
|
221
|
+
data: subscription.sub
|
|
222
|
+
}));
|
|
223
|
+
}
|
|
224
|
+
roomName(message) {
|
|
225
|
+
return message.id ? `${message.streamName}:group:${message.groupId}:item:${message.id}` : `${message.streamName}:group:${message.groupId}`;
|
|
226
|
+
}
|
|
227
|
+
};
|
|
228
|
+
|
|
229
|
+
//#endregion
|
|
230
|
+
export { Stream, StreamGroupSubscription, StreamItemSubscription, StreamSubscription };
|
package/package.json
CHANGED
|
@@ -1,35 +1,30 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@motiadev/stream-client",
|
|
3
3
|
"description": "Motia Stream Client Package – Responsible for managing streams of data.",
|
|
4
|
-
"version": "0.13.
|
|
5
|
-
"license": "
|
|
6
|
-
"
|
|
7
|
-
"
|
|
8
|
-
"
|
|
9
|
-
"
|
|
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
|
-
},
|
|
4
|
+
"version": "0.13.1-beta.163-660633",
|
|
5
|
+
"license": "Elastic-2.0",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"main": "./dist/index.js",
|
|
8
|
+
"module": "./dist/index.js",
|
|
9
|
+
"types": "./dist/index.d.ts",
|
|
21
10
|
"dependencies": {
|
|
22
|
-
"uuid": "^
|
|
11
|
+
"uuid": "^13.0.0"
|
|
23
12
|
},
|
|
24
13
|
"devDependencies": {
|
|
25
|
-
"@types/jest": "^
|
|
14
|
+
"@types/jest": "^30.0.0",
|
|
26
15
|
"@types/ws": "^8.18.1",
|
|
27
|
-
"jest": "^
|
|
28
|
-
"ts-jest": "^29.
|
|
29
|
-
"
|
|
16
|
+
"jest": "^30.2.0",
|
|
17
|
+
"ts-jest": "^29.4.5",
|
|
18
|
+
"tsdown": "^0.16.6",
|
|
19
|
+
"typescript": "^5.9.3"
|
|
20
|
+
},
|
|
21
|
+
"exports": {
|
|
22
|
+
".": "./dist/index.js",
|
|
23
|
+
"./package.json": "./package.json"
|
|
30
24
|
},
|
|
31
25
|
"scripts": {
|
|
32
|
-
"build": "
|
|
26
|
+
"build": "tsdown",
|
|
27
|
+
"dev": "tsdown --watch",
|
|
33
28
|
"lint": "eslint --config ../../eslint.config.js",
|
|
34
29
|
"test": "jest"
|
|
35
30
|
}
|
package/tsdown.config.ts
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { defineConfig } from 'tsdown'
|
|
2
|
+
|
|
3
|
+
export default defineConfig({
|
|
4
|
+
entry: {
|
|
5
|
+
index: './index.ts',
|
|
6
|
+
},
|
|
7
|
+
format: 'esm',
|
|
8
|
+
platform: 'neutral',
|
|
9
|
+
external: ['uuid'],
|
|
10
|
+
dts: {
|
|
11
|
+
build: true,
|
|
12
|
+
},
|
|
13
|
+
clean: true,
|
|
14
|
+
outDir: 'dist',
|
|
15
|
+
exports: {
|
|
16
|
+
devExports: 'development',
|
|
17
|
+
},
|
|
18
|
+
})
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|