@motiadev/stream-client 0.13.0-beta.162-080298 → 0.13.0-beta.162-660221
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 +21 -93
- package/__mocks__/uuid.ts +10 -0
- package/dist/index.d.mts +8 -0
- package/dist/index.mjs +6 -0
- package/dist/src/adapter-factory.d.mts +7 -0
- package/dist/src/socket-adapter.d.mts +13 -0
- package/dist/src/stream-group.d.mts +18 -0
- package/dist/src/stream-group.mjs +55 -0
- package/dist/src/stream-group.mjs.map +1 -0
- package/dist/src/stream-item.d.mts +14 -0
- package/dist/src/stream-item.mjs +20 -0
- package/dist/src/stream-item.mjs.map +1 -0
- package/dist/src/stream-subscription.d.mts +40 -0
- package/dist/src/stream-subscription.mjs +64 -0
- package/dist/src/stream-subscription.mjs.map +1 -0
- package/dist/src/stream.d.mts +43 -0
- package/dist/src/stream.mjs +104 -0
- package/dist/src/stream.mjs.map +1 -0
- package/dist/src/stream.types.d.mts +60 -0
- package/package.json +14 -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,93 +1,21 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
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.
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Motia
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { SocketAdapter } from "./src/socket-adapter.mjs";
|
|
2
|
+
import { SocketAdapterFactory } from "./src/adapter-factory.mjs";
|
|
3
|
+
import { BaseMessage, CustomEvent, CustomEventListener, GroupEventMessage, GroupStreamEvent, ItemEventMessage, ItemStreamEvent, JoinMessage, Listener, Message, StreamEvent } from "./src/stream.types.mjs";
|
|
4
|
+
import { StreamSubscription } from "./src/stream-subscription.mjs";
|
|
5
|
+
import { StreamGroupSubscription } from "./src/stream-group.mjs";
|
|
6
|
+
import { StreamItemSubscription } from "./src/stream-item.mjs";
|
|
7
|
+
import { Stream } from "./src/stream.mjs";
|
|
8
|
+
export { BaseMessage, CustomEvent, CustomEventListener, GroupEventMessage, GroupStreamEvent, ItemEventMessage, ItemStreamEvent, JoinMessage, Listener, Message, type SocketAdapter, type SocketAdapterFactory, Stream, StreamEvent, StreamGroupSubscription, StreamItemSubscription, StreamSubscription };
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { StreamSubscription } from "./src/stream-subscription.mjs";
|
|
2
|
+
import { StreamGroupSubscription } from "./src/stream-group.mjs";
|
|
3
|
+
import { StreamItemSubscription } from "./src/stream-item.mjs";
|
|
4
|
+
import { Stream } from "./src/stream.mjs";
|
|
5
|
+
|
|
6
|
+
export { Stream, StreamGroupSubscription, StreamItemSubscription, StreamSubscription };
|
|
@@ -0,0 +1,13 @@
|
|
|
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
|
+
export { SocketAdapter };
|
|
13
|
+
//# sourceMappingURL=socket-adapter.d.mts.map
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { GroupEventMessage, JoinMessage } from "./stream.types.mjs";
|
|
2
|
+
import { StreamSubscription } from "./stream-subscription.mjs";
|
|
3
|
+
|
|
4
|
+
//#region src/stream-group.d.ts
|
|
5
|
+
declare class StreamGroupSubscription<TData extends {
|
|
6
|
+
id: string;
|
|
7
|
+
}> extends StreamSubscription<TData[], GroupEventMessage<TData>> {
|
|
8
|
+
private sortKey?;
|
|
9
|
+
private lastTimestamp;
|
|
10
|
+
private lastTimestampMap;
|
|
11
|
+
constructor(sub: JoinMessage, sortKey?: keyof TData | undefined);
|
|
12
|
+
private sort;
|
|
13
|
+
protected setState(state: TData[]): void;
|
|
14
|
+
listener(message: GroupEventMessage<TData>): void;
|
|
15
|
+
}
|
|
16
|
+
//#endregion
|
|
17
|
+
export { StreamGroupSubscription };
|
|
18
|
+
//# sourceMappingURL=stream-group.d.mts.map
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { StreamSubscription } from "./stream-subscription.mjs";
|
|
2
|
+
|
|
3
|
+
//#region src/stream-group.ts
|
|
4
|
+
var StreamGroupSubscription = class extends StreamSubscription {
|
|
5
|
+
constructor(sub, sortKey) {
|
|
6
|
+
super(sub, []);
|
|
7
|
+
this.sortKey = sortKey;
|
|
8
|
+
this.lastTimestamp = 0;
|
|
9
|
+
this.lastTimestampMap = /* @__PURE__ */ new Map();
|
|
10
|
+
}
|
|
11
|
+
sort(state) {
|
|
12
|
+
const sortKey = this.sortKey;
|
|
13
|
+
if (sortKey) return state.sort((a, b) => {
|
|
14
|
+
const aValue = a[sortKey];
|
|
15
|
+
const bValue = b[sortKey];
|
|
16
|
+
if (aValue && bValue) return aValue.toString().localeCompare(bValue.toString());
|
|
17
|
+
return 0;
|
|
18
|
+
});
|
|
19
|
+
return state;
|
|
20
|
+
}
|
|
21
|
+
setState(state) {
|
|
22
|
+
super.setState(this.sort(state));
|
|
23
|
+
}
|
|
24
|
+
listener(message) {
|
|
25
|
+
if (message.event.type === "sync") {
|
|
26
|
+
if (message.timestamp < this.lastTimestamp) return;
|
|
27
|
+
this.lastTimestampMap = /* @__PURE__ */ new Map();
|
|
28
|
+
this.lastTimestamp = message.timestamp;
|
|
29
|
+
this.setState(message.event.data);
|
|
30
|
+
} else if (message.event.type === "create") {
|
|
31
|
+
const id = message.event.data.id;
|
|
32
|
+
const state = this.getState();
|
|
33
|
+
if (!state.find((item) => item.id === id)) this.setState([...state, message.event.data]);
|
|
34
|
+
} else if (message.event.type === "update") {
|
|
35
|
+
const messageData = message.event.data;
|
|
36
|
+
const messageDataId = messageData.id;
|
|
37
|
+
const state = this.getState();
|
|
38
|
+
const currentItemTimestamp = this.lastTimestampMap.get(messageDataId);
|
|
39
|
+
if (currentItemTimestamp && currentItemTimestamp >= message.timestamp) return;
|
|
40
|
+
this.lastTimestamp = message.timestamp;
|
|
41
|
+
this.lastTimestampMap.set(messageDataId, message.timestamp);
|
|
42
|
+
this.setState(state.map((item) => item.id === messageDataId ? messageData : item));
|
|
43
|
+
} else if (message.event.type === "delete") {
|
|
44
|
+
const messageDataId = message.event.data.id;
|
|
45
|
+
const state = this.getState();
|
|
46
|
+
this.lastTimestamp = message.timestamp;
|
|
47
|
+
this.lastTimestampMap.set(messageDataId, message.timestamp);
|
|
48
|
+
this.setState(state.filter((item) => item.id !== messageDataId));
|
|
49
|
+
} else if (message.event.type === "event") this.onEventReceived(message.event.event);
|
|
50
|
+
}
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
//#endregion
|
|
54
|
+
export { StreamGroupSubscription };
|
|
55
|
+
//# sourceMappingURL=stream-group.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"stream-group.mjs","names":["sortKey?: keyof TData"],"sources":["../../src/stream-group.ts"],"sourcesContent":["import type { GroupEventMessage, JoinMessage } from './stream.types'\nimport { StreamSubscription } from './stream-subscription'\n\nexport class StreamGroupSubscription<TData extends { id: string }> extends StreamSubscription<\n TData[],\n GroupEventMessage<TData>\n> {\n private lastTimestamp: number = 0\n private lastTimestampMap: Map<string, number> = new Map()\n\n constructor(\n sub: JoinMessage,\n private sortKey?: keyof TData,\n ) {\n super(sub, [])\n }\n\n private sort(state: TData[]): TData[] {\n const sortKey = this.sortKey\n\n if (sortKey) {\n return state.sort((a: TData, b: TData) => {\n const aValue = a[sortKey]\n const bValue = b[sortKey]\n\n if (aValue && bValue) {\n return aValue.toString().localeCompare(bValue.toString())\n }\n\n return 0\n })\n }\n\n return state\n }\n\n protected setState(state: TData[]): void {\n super.setState(this.sort(state))\n }\n\n listener(message: GroupEventMessage<TData>): void {\n if (message.event.type === 'sync') {\n if (message.timestamp < this.lastTimestamp) {\n return\n }\n\n this.lastTimestampMap = new Map()\n this.lastTimestamp = message.timestamp\n this.setState(message.event.data)\n } else if (message.event.type === 'create') {\n const id = message.event.data.id\n const state = this.getState()\n\n if (!state.find((item) => item.id === id)) {\n this.setState([...state, message.event.data])\n }\n } else if (message.event.type === 'update') {\n const messageData = message.event.data\n const messageDataId = messageData.id\n const state = this.getState()\n const currentItemTimestamp = this.lastTimestampMap.get(messageDataId)\n\n if (currentItemTimestamp && currentItemTimestamp >= message.timestamp) {\n return\n }\n\n this.lastTimestamp = message.timestamp\n this.lastTimestampMap.set(messageDataId, message.timestamp)\n this.setState(state.map((item) => (item.id === messageDataId ? messageData : item)))\n } else if (message.event.type === 'delete') {\n const messageDataId = message.event.data.id\n const state = this.getState()\n\n this.lastTimestamp = message.timestamp\n this.lastTimestampMap.set(messageDataId, message.timestamp)\n this.setState(state.filter((item) => item.id !== messageDataId))\n } else if (message.event.type === 'event') {\n this.onEventReceived(message.event.event)\n }\n }\n}\n"],"mappings":";;;AAGA,IAAa,0BAAb,cAA2E,mBAGzE;CAIA,YACE,KACA,AAAQA,SACR;AACA,QAAM,KAAK,EAAE,CAAC;EAFN;uBALsB;0CACgB,IAAI,KAAK;;CASzD,AAAQ,KAAK,OAAyB;EACpC,MAAM,UAAU,KAAK;AAErB,MAAI,QACF,QAAO,MAAM,MAAM,GAAU,MAAa;GACxC,MAAM,SAAS,EAAE;GACjB,MAAM,SAAS,EAAE;AAEjB,OAAI,UAAU,OACZ,QAAO,OAAO,UAAU,CAAC,cAAc,OAAO,UAAU,CAAC;AAG3D,UAAO;IACP;AAGJ,SAAO;;CAGT,AAAU,SAAS,OAAsB;AACvC,QAAM,SAAS,KAAK,KAAK,MAAM,CAAC;;CAGlC,SAAS,SAAyC;AAChD,MAAI,QAAQ,MAAM,SAAS,QAAQ;AACjC,OAAI,QAAQ,YAAY,KAAK,cAC3B;AAGF,QAAK,mCAAmB,IAAI,KAAK;AACjC,QAAK,gBAAgB,QAAQ;AAC7B,QAAK,SAAS,QAAQ,MAAM,KAAK;aACxB,QAAQ,MAAM,SAAS,UAAU;GAC1C,MAAM,KAAK,QAAQ,MAAM,KAAK;GAC9B,MAAM,QAAQ,KAAK,UAAU;AAE7B,OAAI,CAAC,MAAM,MAAM,SAAS,KAAK,OAAO,GAAG,CACvC,MAAK,SAAS,CAAC,GAAG,OAAO,QAAQ,MAAM,KAAK,CAAC;aAEtC,QAAQ,MAAM,SAAS,UAAU;GAC1C,MAAM,cAAc,QAAQ,MAAM;GAClC,MAAM,gBAAgB,YAAY;GAClC,MAAM,QAAQ,KAAK,UAAU;GAC7B,MAAM,uBAAuB,KAAK,iBAAiB,IAAI,cAAc;AAErE,OAAI,wBAAwB,wBAAwB,QAAQ,UAC1D;AAGF,QAAK,gBAAgB,QAAQ;AAC7B,QAAK,iBAAiB,IAAI,eAAe,QAAQ,UAAU;AAC3D,QAAK,SAAS,MAAM,KAAK,SAAU,KAAK,OAAO,gBAAgB,cAAc,KAAM,CAAC;aAC3E,QAAQ,MAAM,SAAS,UAAU;GAC1C,MAAM,gBAAgB,QAAQ,MAAM,KAAK;GACzC,MAAM,QAAQ,KAAK,UAAU;AAE7B,QAAK,gBAAgB,QAAQ;AAC7B,QAAK,iBAAiB,IAAI,eAAe,QAAQ,UAAU;AAC3D,QAAK,SAAS,MAAM,QAAQ,SAAS,KAAK,OAAO,cAAc,CAAC;aACvD,QAAQ,MAAM,SAAS,QAChC,MAAK,gBAAgB,QAAQ,MAAM,MAAM"}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { ItemEventMessage, JoinMessage } from "./stream.types.mjs";
|
|
2
|
+
import { StreamSubscription } from "./stream-subscription.mjs";
|
|
3
|
+
|
|
4
|
+
//#region src/stream-item.d.ts
|
|
5
|
+
declare class StreamItemSubscription<TData extends {
|
|
6
|
+
id: string;
|
|
7
|
+
}> extends StreamSubscription<TData | null, ItemEventMessage<TData>> {
|
|
8
|
+
private lastEventTimestamp;
|
|
9
|
+
constructor(sub: JoinMessage);
|
|
10
|
+
listener(message: ItemEventMessage<TData>): void;
|
|
11
|
+
}
|
|
12
|
+
//#endregion
|
|
13
|
+
export { StreamItemSubscription };
|
|
14
|
+
//# sourceMappingURL=stream-item.d.mts.map
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { StreamSubscription } from "./stream-subscription.mjs";
|
|
2
|
+
|
|
3
|
+
//#region src/stream-item.ts
|
|
4
|
+
var StreamItemSubscription = class extends StreamSubscription {
|
|
5
|
+
constructor(sub) {
|
|
6
|
+
super(sub, null);
|
|
7
|
+
this.lastEventTimestamp = 0;
|
|
8
|
+
}
|
|
9
|
+
listener(message) {
|
|
10
|
+
if (message.timestamp <= this.lastEventTimestamp) return;
|
|
11
|
+
this.lastEventTimestamp = message.timestamp;
|
|
12
|
+
if (message.event.type === "sync" || message.event.type === "create" || message.event.type === "update") this.setState(message.event.data);
|
|
13
|
+
else if (message.event.type === "delete") this.setState(null);
|
|
14
|
+
else if (message.event.type === "event") this.onEventReceived(message.event.event);
|
|
15
|
+
}
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
//#endregion
|
|
19
|
+
export { StreamItemSubscription };
|
|
20
|
+
//# sourceMappingURL=stream-item.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"stream-item.mjs","names":[],"sources":["../../src/stream-item.ts"],"sourcesContent":["import type { ItemEventMessage, JoinMessage } from './stream.types'\nimport { StreamSubscription } from './stream-subscription'\n\nexport class StreamItemSubscription<TData extends { id: string }> extends StreamSubscription<\n TData | null,\n ItemEventMessage<TData>\n> {\n private lastEventTimestamp = 0\n\n constructor(sub: JoinMessage) {\n super(sub, null)\n }\n\n listener(message: ItemEventMessage<TData>): void {\n if (message.timestamp <= this.lastEventTimestamp) {\n return\n }\n\n this.lastEventTimestamp = message.timestamp\n\n if (message.event.type === 'sync' || message.event.type === 'create' || message.event.type === 'update') {\n this.setState(message.event.data)\n } else if (message.event.type === 'delete') {\n this.setState(null)\n } else if (message.event.type === 'event') {\n this.onEventReceived(message.event.event)\n }\n }\n}\n"],"mappings":";;;AAGA,IAAa,yBAAb,cAA0E,mBAGxE;CAGA,YAAY,KAAkB;AAC5B,QAAM,KAAK,KAAK;4BAHW;;CAM7B,SAAS,SAAwC;AAC/C,MAAI,QAAQ,aAAa,KAAK,mBAC5B;AAGF,OAAK,qBAAqB,QAAQ;AAElC,MAAI,QAAQ,MAAM,SAAS,UAAU,QAAQ,MAAM,SAAS,YAAY,QAAQ,MAAM,SAAS,SAC7F,MAAK,SAAS,QAAQ,MAAM,KAAK;WACxB,QAAQ,MAAM,SAAS,SAChC,MAAK,SAAS,KAAK;WACV,QAAQ,MAAM,SAAS,QAChC,MAAK,gBAAgB,QAAQ,MAAM,MAAM"}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { CustomEvent, JoinMessage, Listener } from "./stream.types.mjs";
|
|
2
|
+
|
|
3
|
+
//#region src/stream-subscription.d.ts
|
|
4
|
+
type CustomEventListener = (event: any) => void;
|
|
5
|
+
declare abstract class StreamSubscription<TData = unknown, TEventData = unknown> {
|
|
6
|
+
private customEventListeners;
|
|
7
|
+
private closeListeners;
|
|
8
|
+
private onChangeListeners;
|
|
9
|
+
private state;
|
|
10
|
+
readonly sub: JoinMessage;
|
|
11
|
+
constructor(sub: JoinMessage, state: TData);
|
|
12
|
+
abstract listener(message: TEventData): void;
|
|
13
|
+
protected onEventReceived(event: CustomEvent): void;
|
|
14
|
+
/**
|
|
15
|
+
* Add a custom event listener. This listener will be called whenever the custom event is received.
|
|
16
|
+
*/
|
|
17
|
+
onEvent(type: string, listener: CustomEventListener): void;
|
|
18
|
+
/**
|
|
19
|
+
* Remove a custom event listener.
|
|
20
|
+
*/
|
|
21
|
+
offEvent(type: string, listener: CustomEventListener): void;
|
|
22
|
+
onClose(listener: () => void): void;
|
|
23
|
+
close(): void;
|
|
24
|
+
/**
|
|
25
|
+
* Add a change listener. This listener will be called whenever the state of the group changes.
|
|
26
|
+
*/
|
|
27
|
+
addChangeListener(listener: Listener<TData>): void;
|
|
28
|
+
/**
|
|
29
|
+
* Remove a change listener.
|
|
30
|
+
*/
|
|
31
|
+
removeChangeListener(listener: Listener<TData>): void;
|
|
32
|
+
/**
|
|
33
|
+
* Get the current state of the group.
|
|
34
|
+
*/
|
|
35
|
+
getState(): TData;
|
|
36
|
+
protected setState(state: TData): void;
|
|
37
|
+
}
|
|
38
|
+
//#endregion
|
|
39
|
+
export { StreamSubscription };
|
|
40
|
+
//# sourceMappingURL=stream-subscription.d.mts.map
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
//#region src/stream-subscription.ts
|
|
2
|
+
var StreamSubscription = class {
|
|
3
|
+
constructor(sub, state) {
|
|
4
|
+
this.customEventListeners = /* @__PURE__ */ new Map();
|
|
5
|
+
this.closeListeners = /* @__PURE__ */ new Set();
|
|
6
|
+
this.onChangeListeners = /* @__PURE__ */ new Set();
|
|
7
|
+
this.sub = sub;
|
|
8
|
+
this.state = state;
|
|
9
|
+
}
|
|
10
|
+
onEventReceived(event) {
|
|
11
|
+
const customEventListeners = this.customEventListeners.get(event.type);
|
|
12
|
+
if (customEventListeners) {
|
|
13
|
+
const eventData = event.data;
|
|
14
|
+
customEventListeners.forEach((listener) => listener(eventData));
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Add a custom event listener. This listener will be called whenever the custom event is received.
|
|
19
|
+
*/
|
|
20
|
+
onEvent(type, listener) {
|
|
21
|
+
const listeners = this.customEventListeners.get(type) || [];
|
|
22
|
+
this.customEventListeners.set(type, [...listeners, listener]);
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Remove a custom event listener.
|
|
26
|
+
*/
|
|
27
|
+
offEvent(type, listener) {
|
|
28
|
+
const listeners = this.customEventListeners.get(type) || [];
|
|
29
|
+
this.customEventListeners.set(type, listeners.filter((l) => l !== listener));
|
|
30
|
+
}
|
|
31
|
+
onClose(listener) {
|
|
32
|
+
this.closeListeners.add(listener);
|
|
33
|
+
}
|
|
34
|
+
close() {
|
|
35
|
+
this.closeListeners.forEach((listener) => listener());
|
|
36
|
+
this.closeListeners.clear();
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Add a change listener. This listener will be called whenever the state of the group changes.
|
|
40
|
+
*/
|
|
41
|
+
addChangeListener(listener) {
|
|
42
|
+
this.onChangeListeners.add(listener);
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Remove a change listener.
|
|
46
|
+
*/
|
|
47
|
+
removeChangeListener(listener) {
|
|
48
|
+
this.onChangeListeners.delete(listener);
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Get the current state of the group.
|
|
52
|
+
*/
|
|
53
|
+
getState() {
|
|
54
|
+
return this.state;
|
|
55
|
+
}
|
|
56
|
+
setState(state) {
|
|
57
|
+
this.state = state;
|
|
58
|
+
this.onChangeListeners.forEach((listener) => listener(state));
|
|
59
|
+
}
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
//#endregion
|
|
63
|
+
export { StreamSubscription };
|
|
64
|
+
//# sourceMappingURL=stream-subscription.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"stream-subscription.mjs","names":[],"sources":["../../src/stream-subscription.ts"],"sourcesContent":["import type { CustomEvent, JoinMessage, Listener } from './stream.types'\n\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\ntype CustomEventListener = (event: any) => void\n\nexport abstract class StreamSubscription<TData = unknown, TEventData = unknown> {\n private customEventListeners: Map<string, CustomEventListener[]> = new Map()\n private closeListeners: Set<() => void> = new Set()\n\n private onChangeListeners: Set<Listener<TData>> = new Set()\n private state: TData\n\n readonly sub: JoinMessage\n\n constructor(sub: JoinMessage, state: TData) {\n this.sub = sub\n this.state = state\n }\n\n abstract listener(message: TEventData): void\n\n protected onEventReceived(event: CustomEvent) {\n const customEventListeners = this.customEventListeners.get(event.type)\n\n if (customEventListeners) {\n const eventData = event.data\n customEventListeners.forEach((listener) => listener(eventData))\n }\n }\n\n /**\n * Add a custom event listener. This listener will be called whenever the custom event is received.\n */\n onEvent(type: string, listener: CustomEventListener) {\n const listeners = this.customEventListeners.get(type) || []\n this.customEventListeners.set(type, [...listeners, listener])\n }\n\n /**\n * Remove a custom event listener.\n */\n offEvent(type: string, listener: CustomEventListener) {\n const listeners = this.customEventListeners.get(type) || []\n this.customEventListeners.set(\n type,\n listeners.filter((l) => l !== listener),\n )\n }\n\n onClose(listener: () => void) {\n this.closeListeners.add(listener)\n }\n\n close() {\n this.closeListeners.forEach((listener) => listener())\n this.closeListeners.clear()\n }\n\n /**\n * Add a change listener. This listener will be called whenever the state of the group changes.\n */\n addChangeListener(listener: Listener<TData>) {\n this.onChangeListeners.add(listener)\n }\n\n /**\n * Remove a change listener.\n */\n removeChangeListener(listener: Listener<TData>) {\n this.onChangeListeners.delete(listener)\n }\n\n /**\n * Get the current state of the group.\n */\n getState(): TData {\n return this.state\n }\n\n protected setState(state: TData) {\n this.state = state\n this.onChangeListeners.forEach((listener) => listener(state))\n }\n}\n"],"mappings":";AAKA,IAAsB,qBAAtB,MAAgF;CAS9E,YAAY,KAAkB,OAAc;8CARuB,IAAI,KAAK;wCAClC,IAAI,KAAK;2CAED,IAAI,KAAK;AAMzD,OAAK,MAAM;AACX,OAAK,QAAQ;;CAKf,AAAU,gBAAgB,OAAoB;EAC5C,MAAM,uBAAuB,KAAK,qBAAqB,IAAI,MAAM,KAAK;AAEtE,MAAI,sBAAsB;GACxB,MAAM,YAAY,MAAM;AACxB,wBAAqB,SAAS,aAAa,SAAS,UAAU,CAAC;;;;;;CAOnE,QAAQ,MAAc,UAA+B;EACnD,MAAM,YAAY,KAAK,qBAAqB,IAAI,KAAK,IAAI,EAAE;AAC3D,OAAK,qBAAqB,IAAI,MAAM,CAAC,GAAG,WAAW,SAAS,CAAC;;;;;CAM/D,SAAS,MAAc,UAA+B;EACpD,MAAM,YAAY,KAAK,qBAAqB,IAAI,KAAK,IAAI,EAAE;AAC3D,OAAK,qBAAqB,IACxB,MACA,UAAU,QAAQ,MAAM,MAAM,SAAS,CACxC;;CAGH,QAAQ,UAAsB;AAC5B,OAAK,eAAe,IAAI,SAAS;;CAGnC,QAAQ;AACN,OAAK,eAAe,SAAS,aAAa,UAAU,CAAC;AACrD,OAAK,eAAe,OAAO;;;;;CAM7B,kBAAkB,UAA2B;AAC3C,OAAK,kBAAkB,IAAI,SAAS;;;;;CAMtC,qBAAqB,UAA2B;AAC9C,OAAK,kBAAkB,OAAO,SAAS;;;;;CAMzC,WAAkB;AAChB,SAAO,KAAK;;CAGd,AAAU,SAAS,OAAc;AAC/B,OAAK,QAAQ;AACb,OAAK,kBAAkB,SAAS,aAAa,SAAS,MAAM,CAAC"}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { SocketAdapter } from "./socket-adapter.mjs";
|
|
2
|
+
import { SocketAdapterFactory } from "./adapter-factory.mjs";
|
|
3
|
+
import { StreamGroupSubscription } from "./stream-group.mjs";
|
|
4
|
+
import { StreamItemSubscription } from "./stream-item.mjs";
|
|
5
|
+
|
|
6
|
+
//#region src/stream.d.ts
|
|
7
|
+
declare class Stream {
|
|
8
|
+
private adapterFactory;
|
|
9
|
+
private ws;
|
|
10
|
+
private listeners;
|
|
11
|
+
constructor(adapterFactory: SocketAdapterFactory);
|
|
12
|
+
createSocket(): SocketAdapter;
|
|
13
|
+
/**
|
|
14
|
+
* Subscribe to an item in a stream.
|
|
15
|
+
*
|
|
16
|
+
* @argument streamName - The name of the stream to subscribe to.
|
|
17
|
+
* @argument groupId - The id of the group to subscribe to.
|
|
18
|
+
* @argument id - The id of the item to subscribe to.
|
|
19
|
+
*/
|
|
20
|
+
subscribeItem<TData extends {
|
|
21
|
+
id: string;
|
|
22
|
+
}>(streamName: string, groupId: string, id: string): StreamItemSubscription<TData>;
|
|
23
|
+
/**
|
|
24
|
+
* Subscribe to a group in a stream.
|
|
25
|
+
*
|
|
26
|
+
* @argument streamName - The name of the stream to subscribe to.
|
|
27
|
+
* @argument groupId - The id of the group to subscribe to.
|
|
28
|
+
*/
|
|
29
|
+
subscribeGroup<TData extends {
|
|
30
|
+
id: string;
|
|
31
|
+
}>(streamName: string, groupId: string, sortKey?: keyof TData): StreamGroupSubscription<TData>;
|
|
32
|
+
close(): void;
|
|
33
|
+
private onSocketClose;
|
|
34
|
+
private onSocketOpen;
|
|
35
|
+
messageListener(event: string): void;
|
|
36
|
+
private subscribe;
|
|
37
|
+
private join;
|
|
38
|
+
private leave;
|
|
39
|
+
private roomName;
|
|
40
|
+
}
|
|
41
|
+
//#endregion
|
|
42
|
+
export { Stream };
|
|
43
|
+
//# sourceMappingURL=stream.d.mts.map
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
import { StreamGroupSubscription } from "./stream-group.mjs";
|
|
2
|
+
import { StreamItemSubscription } from "./stream-item.mjs";
|
|
3
|
+
import { v4 } from "uuid";
|
|
4
|
+
|
|
5
|
+
//#region src/stream.ts
|
|
6
|
+
var Stream = class {
|
|
7
|
+
constructor(adapterFactory) {
|
|
8
|
+
this.adapterFactory = adapterFactory;
|
|
9
|
+
this.listeners = {};
|
|
10
|
+
this.ws = this.createSocket();
|
|
11
|
+
}
|
|
12
|
+
createSocket() {
|
|
13
|
+
this.ws = this.adapterFactory();
|
|
14
|
+
this.ws.onMessage((message) => this.messageListener(message));
|
|
15
|
+
this.ws.onOpen(() => this.onSocketOpen());
|
|
16
|
+
this.ws.onClose(() => this.onSocketClose());
|
|
17
|
+
return this.ws;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Subscribe to an item 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
|
+
* @argument id - The id of the item to subscribe to.
|
|
25
|
+
*/
|
|
26
|
+
subscribeItem(streamName, groupId, id) {
|
|
27
|
+
const subscription = new StreamItemSubscription({
|
|
28
|
+
streamName,
|
|
29
|
+
groupId,
|
|
30
|
+
id,
|
|
31
|
+
subscriptionId: v4()
|
|
32
|
+
});
|
|
33
|
+
this.subscribe(subscription);
|
|
34
|
+
return subscription;
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Subscribe to a group in a stream.
|
|
38
|
+
*
|
|
39
|
+
* @argument streamName - The name of the stream to subscribe to.
|
|
40
|
+
* @argument groupId - The id of the group to subscribe to.
|
|
41
|
+
*/
|
|
42
|
+
subscribeGroup(streamName, groupId, sortKey) {
|
|
43
|
+
const subscription = new StreamGroupSubscription({
|
|
44
|
+
streamName,
|
|
45
|
+
groupId,
|
|
46
|
+
subscriptionId: v4()
|
|
47
|
+
}, sortKey);
|
|
48
|
+
this.subscribe(subscription);
|
|
49
|
+
return subscription;
|
|
50
|
+
}
|
|
51
|
+
close() {
|
|
52
|
+
this.listeners = {};
|
|
53
|
+
this.ws.close();
|
|
54
|
+
}
|
|
55
|
+
onSocketClose() {
|
|
56
|
+
setTimeout(() => this.createSocket(), 2e3);
|
|
57
|
+
}
|
|
58
|
+
onSocketOpen() {
|
|
59
|
+
Object.values(this.listeners).forEach((listeners) => {
|
|
60
|
+
listeners.forEach((subscription) => this.join(subscription));
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
messageListener(event) {
|
|
64
|
+
const message = JSON.parse(event);
|
|
65
|
+
const room = this.roomName(message);
|
|
66
|
+
this.listeners[room]?.forEach((listener) => listener.listener(message));
|
|
67
|
+
if (message.id && message.event.type !== "sync") {
|
|
68
|
+
const groupRoom = this.roomName({
|
|
69
|
+
streamName: message.streamName,
|
|
70
|
+
groupId: message.groupId
|
|
71
|
+
});
|
|
72
|
+
this.listeners[groupRoom]?.forEach((listener) => listener.listener(message));
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
subscribe(subscription) {
|
|
76
|
+
const room = this.roomName(subscription.sub);
|
|
77
|
+
if (!this.listeners[room]) this.listeners[room] = /* @__PURE__ */ new Set();
|
|
78
|
+
this.listeners[room].add(subscription);
|
|
79
|
+
this.join(subscription);
|
|
80
|
+
subscription.onClose(() => {
|
|
81
|
+
this.listeners[room]?.delete(subscription);
|
|
82
|
+
this.leave(subscription);
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
join(subscription) {
|
|
86
|
+
if (this.ws.isOpen()) this.ws.send(JSON.stringify({
|
|
87
|
+
type: "join",
|
|
88
|
+
data: subscription.sub
|
|
89
|
+
}));
|
|
90
|
+
}
|
|
91
|
+
leave(subscription) {
|
|
92
|
+
if (this.ws.isOpen()) this.ws.send(JSON.stringify({
|
|
93
|
+
type: "leave",
|
|
94
|
+
data: subscription.sub
|
|
95
|
+
}));
|
|
96
|
+
}
|
|
97
|
+
roomName(message) {
|
|
98
|
+
return message.id ? `${message.streamName}:group:${message.groupId}:item:${message.id}` : `${message.streamName}:group:${message.groupId}`;
|
|
99
|
+
}
|
|
100
|
+
};
|
|
101
|
+
|
|
102
|
+
//#endregion
|
|
103
|
+
export { Stream };
|
|
104
|
+
//# sourceMappingURL=stream.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"stream.mjs","names":["adapterFactory: SocketAdapterFactory","uuidv4","message: ItemEventMessage<never>"],"sources":["../../src/stream.ts"],"sourcesContent":["import { v4 as uuidv4 } from 'uuid'\nimport type { SocketAdapterFactory } from './adapter-factory'\nimport type { SocketAdapter } from './socket-adapter'\nimport type { BaseMessage, ItemEventMessage } from './stream.types'\nimport { StreamGroupSubscription } from './stream-group'\nimport { StreamItemSubscription } from './stream-item'\nimport type { StreamSubscription } from './stream-subscription'\n\nexport class Stream {\n private ws: SocketAdapter\n private listeners: { [channelId: string]: Set<StreamSubscription> } = {}\n\n constructor(private adapterFactory: SocketAdapterFactory) {\n this.ws = this.createSocket()\n }\n\n createSocket(): SocketAdapter {\n this.ws = this.adapterFactory()\n this.ws.onMessage((message) => this.messageListener(message))\n this.ws.onOpen(() => this.onSocketOpen())\n this.ws.onClose(() => this.onSocketClose())\n\n return this.ws\n }\n\n /**\n * Subscribe to an item in a stream.\n *\n * @argument streamName - The name of the stream to subscribe to.\n * @argument groupId - The id of the group to subscribe to.\n * @argument id - The id of the item to subscribe to.\n */\n subscribeItem<TData extends { id: string }>(\n streamName: string,\n groupId: string,\n id: string,\n ): StreamItemSubscription<TData> {\n const subscriptionId = uuidv4()\n const sub = { streamName, groupId, id, subscriptionId }\n const subscription = new StreamItemSubscription<TData>(sub)\n\n this.subscribe(subscription)\n\n return subscription\n }\n\n /**\n * Subscribe to a group in a stream.\n *\n * @argument streamName - The name of the stream to subscribe to.\n * @argument groupId - The id of the group to subscribe to.\n */\n subscribeGroup<TData extends { id: string }>(\n streamName: string,\n groupId: string,\n sortKey?: keyof TData,\n ): StreamGroupSubscription<TData> {\n const subscriptionId = uuidv4()\n const sub = { streamName, groupId, subscriptionId }\n const subscription = new StreamGroupSubscription<TData>(sub, sortKey)\n\n this.subscribe(subscription)\n\n return subscription\n }\n\n close() {\n this.listeners = {} // clean up all listeners\n this.ws.close()\n }\n\n private onSocketClose(): void {\n // retry to connect\n setTimeout(() => this.createSocket(), 2000)\n }\n\n private onSocketOpen(): void {\n Object.values(this.listeners).forEach((listeners) => {\n listeners.forEach((subscription) => this.join(subscription))\n })\n }\n\n messageListener(event: string): void {\n const message: ItemEventMessage<never> = JSON.parse(event)\n const room = this.roomName(message)\n\n this.listeners[room]?.forEach((listener) => listener.listener(message))\n\n // we need to discard sync to group subs when it's an item event\n if (message.id && message.event.type !== 'sync') {\n const groupRoom = this.roomName({\n streamName: message.streamName,\n groupId: message.groupId,\n })\n\n this.listeners[groupRoom]?.forEach((listener) => listener.listener(message))\n }\n }\n\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n private subscribe(subscription: StreamSubscription<any, any>): void {\n const room = this.roomName(subscription.sub)\n\n if (!this.listeners[room]) {\n this.listeners[room] = new Set()\n }\n\n this.listeners[room].add(subscription)\n this.join(subscription)\n\n subscription.onClose(() => {\n this.listeners[room]?.delete(subscription)\n this.leave(subscription)\n })\n }\n\n private join(subscription: StreamSubscription): void {\n if (this.ws.isOpen()) {\n this.ws.send(JSON.stringify({ type: 'join', data: subscription.sub }))\n }\n }\n\n private leave(subscription: StreamSubscription): void {\n if (this.ws.isOpen()) {\n this.ws.send(JSON.stringify({ type: 'leave', data: subscription.sub }))\n }\n }\n\n private roomName(message: Omit<BaseMessage, 'timestamp'>): string {\n return message.id\n ? `${message.streamName}:group:${message.groupId}:item:${message.id}`\n : `${message.streamName}:group:${message.groupId}`\n }\n}\n"],"mappings":";;;;;AAQA,IAAa,SAAb,MAAoB;CAIlB,YAAY,AAAQA,gBAAsC;EAAtC;mBAFkD,EAAE;AAGtE,OAAK,KAAK,KAAK,cAAc;;CAG/B,eAA8B;AAC5B,OAAK,KAAK,KAAK,gBAAgB;AAC/B,OAAK,GAAG,WAAW,YAAY,KAAK,gBAAgB,QAAQ,CAAC;AAC7D,OAAK,GAAG,aAAa,KAAK,cAAc,CAAC;AACzC,OAAK,GAAG,cAAc,KAAK,eAAe,CAAC;AAE3C,SAAO,KAAK;;;;;;;;;CAUd,cACE,YACA,SACA,IAC+B;EAG/B,MAAM,eAAe,IAAI,uBADb;GAAE;GAAY;GAAS;GAAI,gBADhBC,IAAQ;GACwB,CACI;AAE3D,OAAK,UAAU,aAAa;AAE5B,SAAO;;;;;;;;CAST,eACE,YACA,SACA,SACgC;EAGhC,MAAM,eAAe,IAAI,wBADb;GAAE;GAAY;GAAS,gBADZA,IAAQ;GACoB,EACU,QAAQ;AAErE,OAAK,UAAU,aAAa;AAE5B,SAAO;;CAGT,QAAQ;AACN,OAAK,YAAY,EAAE;AACnB,OAAK,GAAG,OAAO;;CAGjB,AAAQ,gBAAsB;AAE5B,mBAAiB,KAAK,cAAc,EAAE,IAAK;;CAG7C,AAAQ,eAAqB;AAC3B,SAAO,OAAO,KAAK,UAAU,CAAC,SAAS,cAAc;AACnD,aAAU,SAAS,iBAAiB,KAAK,KAAK,aAAa,CAAC;IAC5D;;CAGJ,gBAAgB,OAAqB;EACnC,MAAMC,UAAmC,KAAK,MAAM,MAAM;EAC1D,MAAM,OAAO,KAAK,SAAS,QAAQ;AAEnC,OAAK,UAAU,OAAO,SAAS,aAAa,SAAS,SAAS,QAAQ,CAAC;AAGvE,MAAI,QAAQ,MAAM,QAAQ,MAAM,SAAS,QAAQ;GAC/C,MAAM,YAAY,KAAK,SAAS;IAC9B,YAAY,QAAQ;IACpB,SAAS,QAAQ;IAClB,CAAC;AAEF,QAAK,UAAU,YAAY,SAAS,aAAa,SAAS,SAAS,QAAQ,CAAC;;;CAKhF,AAAQ,UAAU,cAAkD;EAClE,MAAM,OAAO,KAAK,SAAS,aAAa,IAAI;AAE5C,MAAI,CAAC,KAAK,UAAU,MAClB,MAAK,UAAU,wBAAQ,IAAI,KAAK;AAGlC,OAAK,UAAU,MAAM,IAAI,aAAa;AACtC,OAAK,KAAK,aAAa;AAEvB,eAAa,cAAc;AACzB,QAAK,UAAU,OAAO,OAAO,aAAa;AAC1C,QAAK,MAAM,aAAa;IACxB;;CAGJ,AAAQ,KAAK,cAAwC;AACnD,MAAI,KAAK,GAAG,QAAQ,CAClB,MAAK,GAAG,KAAK,KAAK,UAAU;GAAE,MAAM;GAAQ,MAAM,aAAa;GAAK,CAAC,CAAC;;CAI1E,AAAQ,MAAM,cAAwC;AACpD,MAAI,KAAK,GAAG,QAAQ,CAClB,MAAK,GAAG,KAAK,KAAK,UAAU;GAAE,MAAM;GAAS,MAAM,aAAa;GAAK,CAAC,CAAC;;CAI3E,AAAQ,SAAS,SAAiD;AAChE,SAAO,QAAQ,KACX,GAAG,QAAQ,WAAW,SAAS,QAAQ,QAAQ,QAAQ,QAAQ,OAC/D,GAAG,QAAQ,WAAW,SAAS,QAAQ"}
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
//#region src/stream.types.d.ts
|
|
2
|
+
type BaseMessage = {
|
|
3
|
+
streamName: string;
|
|
4
|
+
groupId: string;
|
|
5
|
+
id?: string;
|
|
6
|
+
timestamp: number;
|
|
7
|
+
};
|
|
8
|
+
type JoinMessage = Omit<BaseMessage, 'timestamp'> & {
|
|
9
|
+
subscriptionId: string;
|
|
10
|
+
};
|
|
11
|
+
type CustomEvent = {
|
|
12
|
+
type: string;
|
|
13
|
+
data: any;
|
|
14
|
+
};
|
|
15
|
+
type StreamEvent<TData extends {
|
|
16
|
+
id: string;
|
|
17
|
+
}> = {
|
|
18
|
+
type: 'create';
|
|
19
|
+
data: TData;
|
|
20
|
+
} | {
|
|
21
|
+
type: 'update';
|
|
22
|
+
data: TData;
|
|
23
|
+
} | {
|
|
24
|
+
type: 'delete';
|
|
25
|
+
data: TData;
|
|
26
|
+
} | {
|
|
27
|
+
type: 'event';
|
|
28
|
+
event: CustomEvent;
|
|
29
|
+
};
|
|
30
|
+
type ItemStreamEvent<TData extends {
|
|
31
|
+
id: string;
|
|
32
|
+
}> = StreamEvent<TData> | {
|
|
33
|
+
type: 'sync';
|
|
34
|
+
data: TData;
|
|
35
|
+
};
|
|
36
|
+
type GroupStreamEvent<TData extends {
|
|
37
|
+
id: string;
|
|
38
|
+
}> = StreamEvent<TData> | {
|
|
39
|
+
type: 'sync';
|
|
40
|
+
data: TData[];
|
|
41
|
+
};
|
|
42
|
+
type ItemEventMessage<TData extends {
|
|
43
|
+
id: string;
|
|
44
|
+
}> = BaseMessage & {
|
|
45
|
+
event: ItemStreamEvent<TData>;
|
|
46
|
+
};
|
|
47
|
+
type GroupEventMessage<TData extends {
|
|
48
|
+
id: string;
|
|
49
|
+
}> = BaseMessage & {
|
|
50
|
+
event: GroupStreamEvent<TData>;
|
|
51
|
+
};
|
|
52
|
+
type Message = {
|
|
53
|
+
type: 'join' | 'leave';
|
|
54
|
+
data: JoinMessage;
|
|
55
|
+
};
|
|
56
|
+
type Listener<TData> = (state: TData | null) => void;
|
|
57
|
+
type CustomEventListener<TData> = (event: TData) => void;
|
|
58
|
+
//#endregion
|
|
59
|
+
export { BaseMessage, CustomEvent, CustomEventListener, GroupEventMessage, GroupStreamEvent, ItemEventMessage, ItemStreamEvent, JoinMessage, Listener, Message, StreamEvent };
|
|
60
|
+
//# sourceMappingURL=stream.types.d.mts.map
|