@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
package/dist/index.js
DELETED
|
@@ -1,230 +0,0 @@
|
|
|
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/tsdown.config.ts
DELETED
|
@@ -1,18 +0,0 @@
|
|
|
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
|
-
})
|