@webiny/app-websockets 0.0.0-unstable.a4637c5ce6 β 0.0.0-unstable.b6d7105cee
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/README.md +8 -9
- package/WebsocketsContextProvider.d.ts +1 -1
- package/WebsocketsContextProvider.js +139 -156
- package/WebsocketsContextProvider.js.map +1 -1
- package/domain/WebsocketsAction.d.ts +1 -1
- package/domain/WebsocketsAction.js +17 -24
- package/domain/WebsocketsAction.js.map +1 -1
- package/domain/WebsocketsActions.d.ts +1 -3
- package/domain/WebsocketsActions.js +52 -78
- package/domain/WebsocketsActions.js.map +1 -1
- package/domain/WebsocketsConnection.d.ts +1 -4
- package/domain/WebsocketsConnection.js +73 -107
- package/domain/WebsocketsConnection.js.map +1 -1
- package/domain/WebsocketsManager.d.ts +1 -1
- package/domain/WebsocketsManager.js +33 -34
- package/domain/WebsocketsManager.js.map +1 -1
- package/domain/WebsocketsSubscriptionManager.d.ts +2 -2
- package/domain/WebsocketsSubscriptionManager.js +58 -65
- package/domain/WebsocketsSubscriptionManager.js.map +1 -1
- package/domain/abstractions/IWebsocketsAction.d.ts +1 -1
- package/domain/abstractions/IWebsocketsAction.js +0 -3
- package/domain/abstractions/IWebsocketsActions.d.ts +2 -2
- package/domain/abstractions/IWebsocketsActions.js +0 -3
- package/domain/abstractions/IWebsocketsConnection.d.ts +2 -2
- package/domain/abstractions/IWebsocketsConnection.js +7 -6
- package/domain/abstractions/IWebsocketsConnection.js.map +1 -1
- package/domain/abstractions/IWebsocketsManager.d.ts +2 -6
- package/domain/abstractions/IWebsocketsManager.js +0 -3
- package/domain/abstractions/IWebsocketsSubscriptionManager.d.ts +2 -2
- package/domain/abstractions/IWebsocketsSubscriptionManager.js +0 -3
- package/domain/abstractions/types.d.ts +1 -1
- package/domain/abstractions/types.js +19 -18
- package/domain/abstractions/types.js.map +1 -1
- package/domain/index.d.ts +5 -5
- package/domain/index.js +5 -7
- package/domain/types.d.ts +6 -6
- package/domain/types.js +2 -8
- package/events/WebsocketEvent.d.ts +10 -0
- package/events/WebsocketEvent.js +13 -0
- package/events/WebsocketEvent.js.map +1 -0
- package/events/abstractions.d.ts +13 -0
- package/events/abstractions.js +5 -0
- package/events/abstractions.js.map +1 -0
- package/exports/admin/websockets.d.ts +1 -0
- package/exports/admin/websockets.js +1 -0
- package/hooks/index.d.ts +1 -1
- package/hooks/index.js +1 -3
- package/hooks/useWebsockets.d.ts +1 -1
- package/hooks/useWebsockets.js +6 -7
- package/hooks/useWebsockets.js.map +1 -1
- package/index.d.ts +6 -3
- package/index.js +14 -15
- package/index.js.map +1 -1
- package/package.json +20 -19
- package/types.d.ts +2 -2
- package/types.js +1 -4
- package/utils/getUrl.js +4 -3
- package/utils/getUrl.js.map +1 -1
- package/domain/abstractions/IWebsocketsAction.js.map +0 -1
- package/domain/abstractions/IWebsocketsActions.js.map +0 -1
- package/domain/abstractions/IWebsocketsManager.js.map +0 -1
- package/domain/abstractions/IWebsocketsSubscriptionManager.js.map +0 -1
- package/domain/index.js.map +0 -1
- package/domain/types.js.map +0 -1
- package/hooks/index.js.map +0 -1
- package/types.js.map +0 -1
package/README.md
CHANGED
|
@@ -1,12 +1,11 @@
|
|
|
1
1
|
# @webiny/app-websockets
|
|
2
|
-
[](https://www.npmjs.com/package/@webiny/app-websockets)
|
|
3
|
-
[](https://www.npmjs.com/package/@webiny/app-websockets)
|
|
4
|
-
[](https://github.com/prettier/prettier)
|
|
5
|
-
[](http://makeapullrequest.com)
|
|
6
2
|
|
|
7
|
-
|
|
3
|
+
> [!NOTE]
|
|
4
|
+
> This package is part of the [Webiny](https://www.webiny.com) monorepo.
|
|
5
|
+
> Itβs **included in every Webiny project by default** and is not meant to be used as a standalone package.
|
|
8
6
|
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
7
|
+
π **Documentation:** [https://www.webiny.com/docs](https://www.webiny.com/docs)
|
|
8
|
+
|
|
9
|
+
---
|
|
10
|
+
|
|
11
|
+
_This README file is automatically generated during the publish process._
|
|
@@ -1,163 +1,146 @@
|
|
|
1
|
-
import
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
7
|
-
import {
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
tenant
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
currentIteration++;
|
|
33
|
-
setTimeout(() => {
|
|
34
|
-
if (!socketsRef.current) {
|
|
35
|
-
return;
|
|
36
|
-
} else if (socketsRef.current.isClosed()) {
|
|
37
|
-
console.log("Running auto-reconnect.");
|
|
38
|
-
socketsRef.current.connect();
|
|
39
|
-
}
|
|
40
|
-
}, 1000);
|
|
41
|
-
});
|
|
42
|
-
return manager;
|
|
43
|
-
}, []);
|
|
44
|
-
|
|
45
|
-
/**
|
|
46
|
-
* We need this useEffect to close the websocket connection and remove window focus event in case component is unmounted.
|
|
47
|
-
* This will, probably, happen only during the development phase.
|
|
48
|
-
*
|
|
49
|
-
* If we did not disconnect on component unmount, we would have a memory leak - multiple connections would be opened.
|
|
50
|
-
*/
|
|
51
|
-
useEffect(() => {
|
|
52
|
-
/**
|
|
53
|
-
* We want to add a window event listener which will check if the connection is closed, and if its - it will connect again.
|
|
54
|
-
*/
|
|
55
|
-
const abortController = new AbortController();
|
|
56
|
-
window.addEventListener("focus", () => {
|
|
57
|
-
if (!socketsRef.current) {
|
|
58
|
-
return;
|
|
59
|
-
} else if (socketsRef.current.isClosed()) {
|
|
60
|
-
console.log("Running auto-reconnect on focus.");
|
|
61
|
-
socketsRef.current.connect();
|
|
62
|
-
}
|
|
63
|
-
}, {
|
|
64
|
-
signal: abortController.signal
|
|
65
|
-
});
|
|
66
|
-
window.addEventListener("close", () => {
|
|
67
|
-
subscriptionManager.triggerOnClose(new CloseEvent("windowClose", {
|
|
68
|
-
code: WebsocketsCloseCode.GOING_AWAY,
|
|
69
|
-
reason: "Closing Window or Tab."
|
|
70
|
-
}));
|
|
71
|
-
}, {
|
|
72
|
-
signal: abortController.signal
|
|
73
|
-
});
|
|
74
|
-
return () => {
|
|
75
|
-
abortController.abort();
|
|
76
|
-
};
|
|
77
|
-
}, []);
|
|
78
|
-
useEffect(() => {
|
|
79
|
-
(async () => {
|
|
80
|
-
const token = await getToken();
|
|
81
|
-
if (!token || !tenant || !locale) {
|
|
82
|
-
return;
|
|
83
|
-
} else if (current.tenant === tenant && current.locale === locale) {
|
|
84
|
-
return;
|
|
85
|
-
} else if (socketsRef.current) {
|
|
86
|
-
await socketsRef.current.close(WebsocketsCloseCode.NORMAL, "Changing tenant/locale.");
|
|
87
|
-
}
|
|
88
|
-
const url = getUrl();
|
|
89
|
-
if (!url) {
|
|
90
|
-
console.error("Not possible to connect to the websocket without a valid URL.", {
|
|
91
|
-
tenant,
|
|
92
|
-
locale,
|
|
93
|
-
token
|
|
1
|
+
import react, { useCallback, useEffect, useMemo, useRef, useState } from "react";
|
|
2
|
+
import { useFeature, useTenantContext } from "@webiny/app-admin";
|
|
3
|
+
import { AuthenticationContextFeature } from "@webiny/app-admin/features/security/AuthenticationContext/feature.js";
|
|
4
|
+
import { EventPublisherFeature } from "@webiny/app/features/eventPublisher/feature.js";
|
|
5
|
+
import { WebsocketEvent } from "./events/WebsocketEvent.js";
|
|
6
|
+
import { WebsocketsCloseCode } from "./types.js";
|
|
7
|
+
import { createWebsocketsAction, createWebsocketsActions, createWebsocketsConnection, createWebsocketsManager, createWebsocketsSubscriptionManager } from "./domain/index.js";
|
|
8
|
+
import { getUrl } from "./utils/getUrl.js";
|
|
9
|
+
const WebsocketsContext = /*#__PURE__*/ react.createContext(void 0);
|
|
10
|
+
const WebsocketsContextProvider = (props)=>{
|
|
11
|
+
const { tenant } = useTenantContext();
|
|
12
|
+
const { authenticationContext } = useFeature(AuthenticationContextFeature);
|
|
13
|
+
const { eventPublisher } = useFeature(EventPublisherFeature);
|
|
14
|
+
const socketsRef = useRef(null);
|
|
15
|
+
const [current, setCurrent] = useState({});
|
|
16
|
+
const getToken = useCallback(async ()=>await authenticationContext.getIdToken(), [
|
|
17
|
+
authenticationContext
|
|
18
|
+
]);
|
|
19
|
+
const subscriptionManager = useMemo(()=>{
|
|
20
|
+
const manager = createWebsocketsSubscriptionManager();
|
|
21
|
+
let currentIteration = 0;
|
|
22
|
+
manager.onClose((event)=>{
|
|
23
|
+
if (currentIteration > 5 || event.code !== WebsocketsCloseCode.GOING_AWAY) return;
|
|
24
|
+
currentIteration++;
|
|
25
|
+
setTimeout(()=>{
|
|
26
|
+
if (!socketsRef.current) return;
|
|
27
|
+
if (socketsRef.current.isClosed()) {
|
|
28
|
+
console.log("Running auto-reconnect.");
|
|
29
|
+
socketsRef.current.connect();
|
|
30
|
+
}
|
|
31
|
+
}, 1000);
|
|
94
32
|
});
|
|
95
|
-
return;
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
33
|
+
return manager;
|
|
34
|
+
}, []);
|
|
35
|
+
useEffect(()=>{
|
|
36
|
+
const abortController = new AbortController();
|
|
37
|
+
window.addEventListener("focus", ()=>{
|
|
38
|
+
if (!socketsRef.current) return;
|
|
39
|
+
if (socketsRef.current.isClosed()) {
|
|
40
|
+
console.log("Running auto-reconnect on focus.");
|
|
41
|
+
socketsRef.current.connect();
|
|
42
|
+
}
|
|
43
|
+
}, {
|
|
44
|
+
signal: abortController.signal
|
|
45
|
+
});
|
|
46
|
+
window.addEventListener("close", ()=>{
|
|
47
|
+
subscriptionManager.triggerOnClose(new CloseEvent("windowClose", {
|
|
48
|
+
code: WebsocketsCloseCode.GOING_AWAY,
|
|
49
|
+
reason: "Closing Window or Tab."
|
|
50
|
+
}));
|
|
51
|
+
}, {
|
|
52
|
+
signal: abortController.signal
|
|
53
|
+
});
|
|
54
|
+
return ()=>{
|
|
55
|
+
abortController.abort();
|
|
56
|
+
};
|
|
57
|
+
}, []);
|
|
58
|
+
useEffect(()=>{
|
|
59
|
+
(async ()=>{
|
|
60
|
+
const token = await getToken();
|
|
61
|
+
if (!token || !tenant) return;
|
|
62
|
+
if (current.tenant === tenant) return;
|
|
63
|
+
if (socketsRef.current) await socketsRef.current.close(WebsocketsCloseCode.NORMAL, "Changing tenant.");
|
|
64
|
+
const url = getUrl();
|
|
65
|
+
if (!url) return void console.error("Not possible to connect to the websocket without a valid URL.", {
|
|
66
|
+
tenant,
|
|
67
|
+
token
|
|
68
|
+
});
|
|
69
|
+
socketsRef.current = createWebsocketsManager(createWebsocketsConnection({
|
|
70
|
+
subscriptionManager,
|
|
71
|
+
url,
|
|
72
|
+
tenant,
|
|
73
|
+
getToken,
|
|
74
|
+
protocol: [
|
|
75
|
+
"webiny-ws-v1"
|
|
76
|
+
]
|
|
77
|
+
}));
|
|
78
|
+
await socketsRef.current.connect();
|
|
79
|
+
setCurrent({
|
|
80
|
+
tenant
|
|
81
|
+
});
|
|
82
|
+
})();
|
|
83
|
+
}, [
|
|
100
84
|
tenant,
|
|
101
|
-
|
|
102
|
-
getToken
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
85
|
+
subscriptionManager,
|
|
86
|
+
getToken
|
|
87
|
+
]);
|
|
88
|
+
useEffect(()=>{
|
|
89
|
+
if (!socketsRef.current) return;
|
|
90
|
+
const subscription = socketsRef.current.onMessage(async (event)=>{
|
|
91
|
+
await eventPublisher.publish(new WebsocketEvent(event.data));
|
|
92
|
+
});
|
|
93
|
+
return ()=>{
|
|
94
|
+
subscription.off();
|
|
95
|
+
};
|
|
96
|
+
}, [
|
|
97
|
+
current.tenant,
|
|
98
|
+
eventPublisher
|
|
99
|
+
]);
|
|
100
|
+
const websocketActions = useMemo(()=>createWebsocketsActions({
|
|
101
|
+
manager: socketsRef.current,
|
|
102
|
+
tenant,
|
|
103
|
+
getToken
|
|
104
|
+
}), [
|
|
105
|
+
socketsRef.current,
|
|
107
106
|
tenant,
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
const onClose = useCallback(cb => {
|
|
144
|
-
return socketsRef.current.onClose(data => {
|
|
145
|
-
return cb(data);
|
|
107
|
+
getToken
|
|
108
|
+
]);
|
|
109
|
+
const send = useCallback(async (action, data, timeout)=>websocketActions.run({
|
|
110
|
+
action,
|
|
111
|
+
data,
|
|
112
|
+
timeout
|
|
113
|
+
}), [
|
|
114
|
+
websocketActions
|
|
115
|
+
]);
|
|
116
|
+
const createAction = useCallback((name)=>createWebsocketsAction(websocketActions, name), [
|
|
117
|
+
websocketActions
|
|
118
|
+
]);
|
|
119
|
+
const onMessage = useCallback((action, cb)=>socketsRef.current.onMessage(async (event)=>{
|
|
120
|
+
if (event.data.action !== action) return;
|
|
121
|
+
cb(event.data);
|
|
122
|
+
}), [
|
|
123
|
+
socketsRef.current
|
|
124
|
+
]);
|
|
125
|
+
const onError = useCallback((cb)=>socketsRef.current.onError((data)=>cb(data)), [
|
|
126
|
+
socketsRef.current
|
|
127
|
+
]);
|
|
128
|
+
const onClose = useCallback((cb)=>socketsRef.current.onClose((data)=>cb(data)), [
|
|
129
|
+
socketsRef.current
|
|
130
|
+
]);
|
|
131
|
+
if (!socketsRef.current) return props.loader || null;
|
|
132
|
+
const value = {
|
|
133
|
+
send,
|
|
134
|
+
createAction,
|
|
135
|
+
onMessage,
|
|
136
|
+
onError,
|
|
137
|
+
onClose
|
|
138
|
+
};
|
|
139
|
+
return /*#__PURE__*/ react.createElement(WebsocketsContext.Provider, {
|
|
140
|
+
value: value,
|
|
141
|
+
...props
|
|
146
142
|
});
|
|
147
|
-
}, [socketsRef.current]);
|
|
148
|
-
if (!socketsRef.current) {
|
|
149
|
-
return props.loader || null;
|
|
150
|
-
}
|
|
151
|
-
const value = {
|
|
152
|
-
send,
|
|
153
|
-
createAction,
|
|
154
|
-
onMessage,
|
|
155
|
-
onError,
|
|
156
|
-
onClose
|
|
157
|
-
};
|
|
158
|
-
return /*#__PURE__*/React.createElement(WebsocketsContext.Provider, Object.assign({
|
|
159
|
-
value: value
|
|
160
|
-
}, props));
|
|
161
143
|
};
|
|
144
|
+
export { WebsocketsContext, WebsocketsContextProvider };
|
|
162
145
|
|
|
163
146
|
//# sourceMappingURL=WebsocketsContextProvider.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"names":["React","useCallback","useEffect","useMemo","useRef","useState","useTenancy","useI18N","useSecurity","WebsocketsCloseCode","createWebsocketsAction","createWebsocketsActions","createWebsocketsConnection","createWebsocketsManager","createWebsocketsSubscriptionManager","getUrl","WebsocketsContext","createContext","undefined","WebsocketsContextProvider","props","tenant","getCurrentLocale","getIdToken","locale","socketsRef","current","setCurrent","getToken","subscriptionManager","manager","currentIteration","onClose","event","code","GOING_AWAY","setTimeout","isClosed","console","log","connect","abortController","AbortController","window","addEventListener","signal","triggerOnClose","CloseEvent","reason","abort","token","close","NORMAL","url","error","protocol","websocketActions","send","action","data","timeout","run","createAction","name","onMessage","cb","onError","loader","value","createElement","Provider","Object","assign"],"sources":["WebsocketsContextProvider.tsx"],"sourcesContent":["import React, { useCallback, useEffect, useMemo, useRef, useState } from \"react\";\nimport { useTenancy } from \"@webiny/app-tenancy\";\nimport { useI18N } from \"@webiny/app-i18n\";\nimport { useSecurity } from \"@webiny/app-security\";\nimport type {\n IncomingGenericData,\n IWebsocketsContext,\n IWebsocketsContextSendCallable,\n IWebsocketsManagerCloseEvent,\n IWebsocketsManagerErrorEvent\n} from \"~/types\";\nimport { WebsocketsCloseCode } from \"~/types\";\nimport {\n createWebsocketsAction,\n createWebsocketsActions,\n createWebsocketsConnection,\n createWebsocketsManager,\n createWebsocketsSubscriptionManager\n} from \"./domain\";\nimport type { IGenericData, IWebsocketsManager } from \"./domain/types\";\nimport { getUrl } from \"./utils/getUrl\";\n\nexport interface IWebsocketsContextProviderProps {\n loader?: React.ReactElement;\n children: React.ReactNode;\n}\n\nexport const WebsocketsContext = React.createContext<IWebsocketsContext>(\n undefined as unknown as IWebsocketsContext\n);\n\ninterface ICurrentData {\n tenant?: string;\n locale?: string;\n}\n\nexport const WebsocketsContextProvider = (props: IWebsocketsContextProviderProps) => {\n const { tenant } = useTenancy();\n const { getCurrentLocale } = useI18N();\n const { getIdToken } = useSecurity();\n const locale = getCurrentLocale(\"default\");\n\n const socketsRef = useRef<IWebsocketsManager>();\n\n const [current, setCurrent] = useState<ICurrentData>({});\n\n const getToken = useCallback(async () => {\n return await getIdToken();\n }, [getIdToken]);\n\n const subscriptionManager = useMemo(() => {\n const manager = createWebsocketsSubscriptionManager();\n\n let currentIteration = 0;\n manager.onClose(event => {\n if (currentIteration > 5 || event.code !== WebsocketsCloseCode.GOING_AWAY) {\n return;\n }\n currentIteration++;\n setTimeout(() => {\n if (!socketsRef.current) {\n return;\n } else if (socketsRef.current.isClosed()) {\n console.log(\"Running auto-reconnect.\");\n\n socketsRef.current.connect();\n }\n }, 1000);\n });\n\n return manager;\n }, []);\n\n /**\n * We need this useEffect to close the websocket connection and remove window focus event in case component is unmounted.\n * This will, probably, happen only during the development phase.\n *\n * If we did not disconnect on component unmount, we would have a memory leak - multiple connections would be opened.\n */\n useEffect(() => {\n /**\n * We want to add a window event listener which will check if the connection is closed, and if its - it will connect again.\n */\n const abortController = new AbortController();\n\n window.addEventListener(\n \"focus\",\n () => {\n if (!socketsRef.current) {\n return;\n } else if (socketsRef.current.isClosed()) {\n console.log(\"Running auto-reconnect on focus.\");\n socketsRef.current.connect();\n }\n },\n { signal: abortController.signal }\n );\n window.addEventListener(\n \"close\",\n () => {\n subscriptionManager.triggerOnClose(\n new CloseEvent(\"windowClose\", {\n code: WebsocketsCloseCode.GOING_AWAY,\n reason: \"Closing Window or Tab.\"\n })\n );\n },\n { signal: abortController.signal }\n );\n\n return () => {\n abortController.abort();\n };\n }, []);\n\n useEffect(() => {\n (async () => {\n const token = await getToken();\n if (!token || !tenant || !locale) {\n return;\n } else if (current.tenant === tenant && current.locale === locale) {\n return;\n } else if (socketsRef.current) {\n await socketsRef.current.close(\n WebsocketsCloseCode.NORMAL,\n \"Changing tenant/locale.\"\n );\n }\n const url = getUrl();\n\n if (!url) {\n console.error(\"Not possible to connect to the websocket without a valid URL.\", {\n tenant,\n locale,\n token\n });\n return;\n }\n\n socketsRef.current = createWebsocketsManager(\n createWebsocketsConnection({\n subscriptionManager,\n url,\n tenant,\n locale,\n getToken,\n protocol: [\"webiny-ws-v1\"]\n })\n );\n await socketsRef.current.connect();\n\n setCurrent({\n tenant,\n locale\n });\n })();\n }, [tenant, locale, subscriptionManager, getToken]);\n\n const websocketActions = useMemo(() => {\n return createWebsocketsActions({\n manager: socketsRef.current!,\n tenant,\n locale,\n getToken\n });\n }, [socketsRef.current, tenant, locale, getToken]);\n\n const send = useCallback<IWebsocketsContextSendCallable>(\n async (action, data, timeout) => {\n return websocketActions.run({\n action,\n data,\n timeout\n });\n },\n [websocketActions]\n );\n\n const createAction = useCallback(\n <T extends IGenericData = IGenericData, R extends IGenericData = IGenericData>(\n name: string\n ) => {\n return createWebsocketsAction<T, R>(websocketActions, name);\n },\n [websocketActions]\n );\n\n const onMessage = useCallback(\n <T extends IncomingGenericData = IncomingGenericData>(\n action: string,\n cb: (data: T) => void\n ) => {\n return socketsRef.current!.onMessage<T>(async event => {\n if (event.data.action !== action) {\n return;\n }\n cb(event.data);\n });\n },\n [socketsRef.current]\n );\n\n const onError = useCallback(\n (cb: (data: IWebsocketsManagerErrorEvent) => void) => {\n return socketsRef.current!.onError(data => {\n return cb(data);\n });\n },\n [socketsRef.current]\n );\n\n const onClose = useCallback(\n (cb: (data: IWebsocketsManagerCloseEvent) => void) => {\n return socketsRef.current!.onClose(data => {\n return cb(data);\n });\n },\n [socketsRef.current]\n );\n\n if (!socketsRef.current) {\n return props.loader || null;\n }\n\n const value: IWebsocketsContext = {\n send,\n createAction,\n onMessage,\n onError,\n onClose\n };\n return <WebsocketsContext.Provider value={value} {...props} />;\n};\n"],"mappings":"AAAA,OAAOA,KAAK,IAAIC,WAAW,EAAEC,SAAS,EAAEC,OAAO,EAAEC,MAAM,EAAEC,QAAQ,QAAQ,OAAO;AAChF,SAASC,UAAU,QAAQ,qBAAqB;AAChD,SAASC,OAAO,QAAQ,kBAAkB;AAC1C,SAASC,WAAW,QAAQ,sBAAsB;AAQlD,SAASC,mBAAmB;AAC5B,SACIC,sBAAsB,EACtBC,uBAAuB,EACvBC,0BAA0B,EAC1BC,uBAAuB,EACvBC,mCAAmC;AAGvC,SAASC,MAAM;AAOf,OAAO,MAAMC,iBAAiB,gBAAGhB,KAAK,CAACiB,aAAa,CAChDC,SACJ,CAAC;AAOD,OAAO,MAAMC,yBAAyB,GAAIC,KAAsC,IAAK;EACjF,MAAM;IAAEC;EAAO,CAAC,GAAGf,UAAU,CAAC,CAAC;EAC/B,MAAM;IAAEgB;EAAiB,CAAC,GAAGf,OAAO,CAAC,CAAC;EACtC,MAAM;IAAEgB;EAAW,CAAC,GAAGf,WAAW,CAAC,CAAC;EACpC,MAAMgB,MAAM,GAAGF,gBAAgB,CAAC,SAAS,CAAC;EAE1C,MAAMG,UAAU,GAAG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AC,EACD,CAAClC,UAAU,CAACC,OAAO,CACvB,CAAC;EAED,MAAMwC,OAAO,GAAGjE,WAAW,CACtBgE,EAAgD,IAAK;IAClD,OAAOxC,UAAU,CAACC,OAAO,CAAEwC,OAAO,CAACP,IAAI,IAAI;MACvC,OAAOM,EAAE,CAACN,IAAI,CAAC;IACnB,CAAC,CAAC;EACN,CAAC,EACD,CAAClC,UAAU,CAACC,OAAO,CACvB,CAAC;EAED,MAAMM,OAAO,GAAG/B,WAAW,CACtBgE,EAAgD,IAAK;IAClD,OAAOxC,UAAU,CAACC,OAAO,CAAEM,OAAO,CAAC2B,IAAI,IAAI;MACvC,OAAOM,EAAE,CAACN,IAAI,CAAC;IACnB,CAAC,CAAC;EACN,CAAC,EACD,CAAClC,UAAU,CAACC,OAAO,CACvB,CAAC;EAED,IAAI,CAACD,UAAU,CAACC,OAAO,EAAE;IACrB,OAAON,KAAK,CAAC+C,MAAM,IAAI,IAAI;EAC/B;EAEA,MAAMC,KAAyB,GAAG;IAC9BX,IAAI;IACJK,YAAY;IACZE,SAAS;IACTE,OAAO;IACPlC;EACJ,CAAC;EACD,oBAAOhC,KAAA,CAAAqE,aAAA,CAACrD,iBAAiB,CAACsD,QAAQ,EAAAC,MAAA,CAAAC,MAAA;IAACJ,KAAK,EAAEA;EAAM,GAAKhD,KAAK,CAAG,CAAC;AAClE,CAAC","ignoreList":[]}
|
|
1
|
+
{"version":3,"file":"WebsocketsContextProvider.js","sources":["../src/WebsocketsContextProvider.tsx"],"sourcesContent":["import React, { useCallback, useEffect, useMemo, useRef, useState } from \"react\";\nimport { useFeature, useTenantContext } from \"@webiny/app-admin\";\nimport { AuthenticationContextFeature } from \"@webiny/app-admin/features/security/AuthenticationContext/feature.js\";\nimport { EventPublisherFeature } from \"@webiny/app/features/eventPublisher/feature.js\";\nimport { WebsocketEvent } from \"./events/WebsocketEvent.js\";\nimport type {\n IncomingGenericData,\n IWebsocketsContext,\n IWebsocketsContextSendCallable,\n IWebsocketsManagerCloseEvent,\n IWebsocketsManagerErrorEvent\n} from \"~/types.js\";\nimport { WebsocketsCloseCode } from \"~/types.js\";\nimport {\n createWebsocketsAction,\n createWebsocketsActions,\n createWebsocketsConnection,\n createWebsocketsManager,\n createWebsocketsSubscriptionManager\n} from \"./domain/index.js\";\nimport type { IGenericData, IWebsocketsManager } from \"./domain/types.js\";\nimport { getUrl } from \"./utils/getUrl.js\";\n\nexport interface IWebsocketsContextProviderProps {\n loader?: React.ReactElement;\n children: React.ReactNode;\n}\n\nexport const WebsocketsContext = React.createContext<IWebsocketsContext>(\n undefined as unknown as IWebsocketsContext\n);\n\ninterface ICurrentData {\n tenant?: string;\n}\n\nexport const WebsocketsContextProvider = (props: IWebsocketsContextProviderProps) => {\n const { tenant } = useTenantContext();\n const { authenticationContext } = useFeature(AuthenticationContextFeature);\n const { eventPublisher } = useFeature(EventPublisherFeature);\n\n const socketsRef = useRef<IWebsocketsManager | null>(null);\n\n const [current, setCurrent] = useState<ICurrentData>({});\n\n const getToken = useCallback(async () => {\n return await authenticationContext.getIdToken();\n }, [authenticationContext]);\n\n const subscriptionManager = useMemo(() => {\n const manager = createWebsocketsSubscriptionManager();\n\n let currentIteration = 0;\n manager.onClose(event => {\n if (currentIteration > 5 || event.code !== WebsocketsCloseCode.GOING_AWAY) {\n return;\n }\n currentIteration++;\n setTimeout(() => {\n if (!socketsRef.current) {\n return;\n } else if (socketsRef.current.isClosed()) {\n console.log(\"Running auto-reconnect.\");\n\n socketsRef.current.connect();\n }\n }, 1000);\n });\n\n return manager;\n }, []);\n\n /**\n * We need this useEffect to close the websocket connection and remove window focus event in case component is unmounted.\n * This will, probably, happen only during the development phase.\n *\n * If we did not disconnect on component unmount, we would have a memory leak - multiple connections would be opened.\n */\n useEffect(() => {\n /**\n * We want to add a window event listener which will check if the connection is closed, and if its - it will connect again.\n */\n const abortController = new AbortController();\n\n window.addEventListener(\n \"focus\",\n () => {\n if (!socketsRef.current) {\n return;\n } else if (socketsRef.current.isClosed()) {\n console.log(\"Running auto-reconnect on focus.\");\n socketsRef.current.connect();\n }\n },\n { signal: abortController.signal }\n );\n window.addEventListener(\n \"close\",\n () => {\n subscriptionManager.triggerOnClose(\n new CloseEvent(\"windowClose\", {\n code: WebsocketsCloseCode.GOING_AWAY,\n reason: \"Closing Window or Tab.\"\n })\n );\n },\n { signal: abortController.signal }\n );\n\n return () => {\n abortController.abort();\n };\n }, []);\n\n useEffect(() => {\n (async () => {\n const token = await getToken();\n if (!token || !tenant) {\n return;\n } else if (current.tenant === tenant) {\n return;\n } else if (socketsRef.current) {\n await socketsRef.current.close(WebsocketsCloseCode.NORMAL, \"Changing tenant.\");\n }\n const url = getUrl();\n\n if (!url) {\n console.error(\"Not possible to connect to the websocket without a valid URL.\", {\n tenant,\n token\n });\n return;\n }\n\n socketsRef.current = createWebsocketsManager(\n createWebsocketsConnection({\n subscriptionManager,\n url,\n tenant,\n getToken,\n protocol: [\"webiny-ws-v1\"]\n })\n );\n await socketsRef.current.connect();\n\n setCurrent({ tenant });\n })();\n }, [tenant, subscriptionManager, getToken]);\n\n /**\n * Bridge: subscribe once to ALL incoming websocket messages and re-publish each one through\n * the EventPublisher as a `WebsocketEvent`. Feature code registers `WebsocketEventHandler`\n * handlers instead of subscribing to the websocket service directly.\n */\n useEffect(() => {\n if (!socketsRef.current) {\n return;\n }\n const subscription = socketsRef.current.onMessage<IncomingGenericData>(async event => {\n await eventPublisher.publish(new WebsocketEvent(event.data));\n });\n return () => {\n subscription.off();\n };\n }, [current.tenant, eventPublisher]);\n\n const websocketActions = useMemo(() => {\n return createWebsocketsActions({\n manager: socketsRef.current!,\n tenant,\n getToken\n });\n }, [socketsRef.current, tenant, getToken]);\n\n const send = useCallback<IWebsocketsContextSendCallable>(\n async (action, data, timeout) => {\n return websocketActions.run({\n action,\n data,\n timeout\n });\n },\n [websocketActions]\n );\n\n const createAction = useCallback(\n <T extends IGenericData = IGenericData, R extends IGenericData = IGenericData>(\n name: string\n ) => {\n return createWebsocketsAction<T, R>(websocketActions, name);\n },\n [websocketActions]\n );\n\n const onMessage = useCallback(\n <T extends IncomingGenericData = IncomingGenericData>(\n action: string,\n cb: (data: T) => void\n ) => {\n return socketsRef.current!.onMessage<T>(async event => {\n if (event.data.action !== action) {\n return;\n }\n cb(event.data);\n });\n },\n [socketsRef.current]\n );\n\n const onError = useCallback(\n (cb: (data: IWebsocketsManagerErrorEvent) => void) => {\n return socketsRef.current!.onError(data => {\n return cb(data);\n });\n },\n [socketsRef.current]\n );\n\n const onClose = useCallback(\n (cb: (data: IWebsocketsManagerCloseEvent) => void) => {\n return socketsRef.current!.onClose(data => {\n return cb(data);\n });\n },\n [socketsRef.current]\n );\n\n if (!socketsRef.current) {\n return props.loader || null;\n }\n\n const value: IWebsocketsContext = {\n send,\n createAction,\n onMessage,\n onError,\n onClose\n };\n return <WebsocketsContext.Provider value={value} {...props} />;\n};\n"],"names":["WebsocketsContext","React","undefined","WebsocketsContextProvider","props","tenant","useTenantContext","authenticationContext","useFeature","AuthenticationContextFeature","eventPublisher","EventPublisherFeature","socketsRef","useRef","current","setCurrent","useState","getToken","useCallback","subscriptionManager","useMemo","manager","createWebsocketsSubscriptionManager","currentIteration","event","WebsocketsCloseCode","setTimeout","console","useEffect","abortController","AbortController","window","CloseEvent","token","url","getUrl","createWebsocketsManager","createWebsocketsConnection","subscription","WebsocketEvent","websocketActions","createWebsocketsActions","send","action","data","timeout","createAction","name","createWebsocketsAction","onMessage","cb","onError","onClose","value"],"mappings":";;;;;;;;AA4BO,MAAMA,oBAAoB,WAAHA,GAAGC,MAAAA,aAAmB,CAChDC;AAOG,MAAMC,4BAA4B,CAACC;IACtC,MAAM,EAAEC,MAAM,EAAE,GAAGC;IACnB,MAAM,EAAEC,qBAAqB,EAAE,GAAGC,WAAWC;IAC7C,MAAM,EAAEC,cAAc,EAAE,GAAGF,WAAWG;IAEtC,MAAMC,aAAaC,OAAkC;IAErD,MAAM,CAACC,SAASC,WAAW,GAAGC,SAAuB,CAAC;I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,UAAUlC,YACZ,CAACgC,KACUtC,WAAW,OAAO,CAAE,OAAO,CAACgC,CAAAA,OACxBM,GAAGN,QAGlB;QAAChC,WAAW,OAAO;KAAC;IAGxB,IAAI,CAACA,WAAW,OAAO,EACnB,OAAOR,MAAM,MAAM,IAAI;IAG3B,MAAMiD,QAA4B;QAC9BX;QACAI;QACAG;QACAE;QACAC;IACJ;IACA,OAAO,WAAP,GAAO,oBAACpD,kBAAkB,QAAQ;QAAC,OAAOqD;QAAQ,GAAGjD,KAAK;;AAC9D"}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { IGenericData, IWebsocketsAction, IWebsocketsActions, IWebsocketsActionsTriggerParams } from "./types";
|
|
1
|
+
import type { IGenericData, IWebsocketsAction, IWebsocketsActions, IWebsocketsActionsTriggerParams } from "./types.js";
|
|
2
2
|
export declare class WebsocketsAction<T extends IGenericData = IGenericData, R extends IGenericData = IGenericData> implements IWebsocketsAction<T, R> {
|
|
3
3
|
private readonly actions;
|
|
4
4
|
private readonly name;
|
|
@@ -1,28 +1,21 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
});
|
|
17
|
-
if (!onResponse) {
|
|
18
|
-
return null;
|
|
1
|
+
class WebsocketsAction {
|
|
2
|
+
constructor(actions, name){
|
|
3
|
+
this.name = name;
|
|
4
|
+
this.actions = actions;
|
|
5
|
+
}
|
|
6
|
+
async trigger(params) {
|
|
7
|
+
const { data, onResponse, timeout = 10000 } = params || {};
|
|
8
|
+
const promise = this.actions.run({
|
|
9
|
+
action: this.name,
|
|
10
|
+
data,
|
|
11
|
+
timeout: onResponse && timeout > 0 ? timeout : void 0
|
|
12
|
+
});
|
|
13
|
+
if (!onResponse) return null;
|
|
14
|
+
const result = await promise;
|
|
15
|
+
return onResponse(result);
|
|
19
16
|
}
|
|
20
|
-
const result = await promise;
|
|
21
|
-
return onResponse(result);
|
|
22
|
-
}
|
|
23
17
|
}
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
};
|
|
18
|
+
const createWebsocketsAction = (actions, name)=>new WebsocketsAction(actions, name);
|
|
19
|
+
export { WebsocketsAction, createWebsocketsAction };
|
|
27
20
|
|
|
28
21
|
//# sourceMappingURL=WebsocketsAction.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"
|
|
1
|
+
{"version":3,"file":"domain/WebsocketsAction.js","sources":["../../src/domain/WebsocketsAction.ts"],"sourcesContent":["import type {\n IGenericData,\n IWebsocketsAction,\n IWebsocketsActions,\n IWebsocketsActionsTriggerParams\n} from \"./types.js\";\n\nexport class WebsocketsAction<\n T extends IGenericData = IGenericData,\n R extends IGenericData = IGenericData\n> implements IWebsocketsAction<T, R> {\n private readonly actions: IWebsocketsActions;\n private readonly name: string;\n\n public constructor(actions: IWebsocketsActions, name: string) {\n this.name = name;\n this.actions = actions;\n }\n\n public async trigger(params?: IWebsocketsActionsTriggerParams<T, R>): Promise<R | null> {\n const { data, onResponse, timeout = 10000 } = params || {};\n const promise = this.actions.run<T, R>({\n action: this.name,\n data,\n timeout: onResponse && timeout > 0 ? timeout : undefined\n });\n if (!onResponse) {\n return null;\n }\n const result = await promise;\n\n return onResponse(result);\n }\n}\n\nexport const createWebsocketsAction = <\n T extends IGenericData = IGenericData,\n R extends IGenericData = IGenericData\n>(\n actions: IWebsocketsActions,\n name: string\n): IWebsocketsAction<T, R> => {\n return new WebsocketsAction<T, R>(actions, name);\n};\n"],"names":["WebsocketsAction","actions","name","params","data","onResponse","timeout","promise","undefined","result","createWebsocketsAction"],"mappings":"AAOO,MAAMA;IAOT,YAAmBC,OAA2B,EAAEC,IAAY,CAAE;QAC1D,IAAI,CAAC,IAAI,GAAGA;QACZ,IAAI,CAAC,OAAO,GAAGD;IACnB;IAEA,MAAa,QAAQE,MAA8C,EAAqB;QACpF,MAAM,EAAEC,IAAI,EAAEC,UAAU,EAAEC,UAAU,KAAK,EAAE,GAAGH,UAAU,CAAC;QACzD,MAAMI,UAAU,IAAI,CAAC,OAAO,CAAC,GAAG,CAAO;YACnC,QAAQ,IAAI,CAAC,IAAI;YACjBH;YACA,SAASC,cAAcC,UAAU,IAAIA,UAAUE;QACnD;QACA,IAAI,CAACH,YACD,OAAO;QAEX,MAAMI,SAAS,MAAMF;QAErB,OAAOF,WAAWI;IACtB;AACJ;AAEO,MAAMC,yBAAyB,CAIlCT,SACAC,OAEO,IAAIF,iBAAuBC,SAASC"}
|
|
@@ -1,15 +1,13 @@
|
|
|
1
|
-
import type { IGenericData, IWebsocketsActions, IWebsocketsActionsRunParams, IWebsocketsManager } from "./types";
|
|
1
|
+
import type { IGenericData, IWebsocketsActions, IWebsocketsActionsRunParams, IWebsocketsManager } from "./types.js";
|
|
2
2
|
export interface IWebsocketActionsParams {
|
|
3
3
|
manager: IWebsocketsManager;
|
|
4
4
|
tenant: string | null;
|
|
5
|
-
locale: string | null;
|
|
6
5
|
getToken: () => Promise<string | undefined>;
|
|
7
6
|
}
|
|
8
7
|
export declare class WebsocketsActions implements IWebsocketsActions {
|
|
9
8
|
readonly manager: IWebsocketsManager;
|
|
10
9
|
private readonly getToken;
|
|
11
10
|
private readonly tenant;
|
|
12
|
-
private readonly locale;
|
|
13
11
|
constructor(params: IWebsocketActionsParams);
|
|
14
12
|
run<T extends IGenericData = IGenericData, R extends IGenericData = IGenericData>(params: IWebsocketsActionsRunParams<T>): Promise<R | null>;
|
|
15
13
|
}
|
|
@@ -1,84 +1,58 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
this.getToken = params.getToken;
|
|
7
|
-
}
|
|
8
|
-
async run(params) {
|
|
9
|
-
const {
|
|
10
|
-
action,
|
|
11
|
-
timeout,
|
|
12
|
-
data
|
|
13
|
-
} = params;
|
|
14
|
-
const token = await this.getToken();
|
|
15
|
-
if (!token) {
|
|
16
|
-
console.error("Token is not set - cannot send a websocket message.");
|
|
17
|
-
return null;
|
|
18
|
-
} else if (!this.tenant) {
|
|
19
|
-
console.error("Tenant is not set - cannot send a websocket message.");
|
|
20
|
-
return null;
|
|
21
|
-
} else if (!this.locale) {
|
|
22
|
-
console.error("Locale is not set - cannot send a websocket message.");
|
|
23
|
-
return null;
|
|
1
|
+
class WebsocketsActions {
|
|
2
|
+
constructor(params){
|
|
3
|
+
this.manager = params.manager;
|
|
4
|
+
this.tenant = params.tenant;
|
|
5
|
+
this.getToken = params.getToken;
|
|
24
6
|
}
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
locale: this.locale,
|
|
37
|
-
action,
|
|
38
|
-
data: data || {}
|
|
39
|
-
});
|
|
40
|
-
return null;
|
|
41
|
-
}
|
|
42
|
-
/**
|
|
43
|
-
* In case of a timeout, we will send the message and wait for the response.
|
|
44
|
-
*/
|
|
45
|
-
return await new Promise((resolve, reject) => {
|
|
46
|
-
let promiseTimeout = null;
|
|
47
|
-
const subscription = this.manager.onMessage(async event => {
|
|
48
|
-
if (event.data.messageId !== subscription.id) {
|
|
49
|
-
return;
|
|
7
|
+
async run(params) {
|
|
8
|
+
const { action, timeout, data } = params;
|
|
9
|
+
const token = await this.getToken();
|
|
10
|
+
if (token) {
|
|
11
|
+
if (!this.tenant) {
|
|
12
|
+
console.error("Tenant is not set - cannot send a websocket message.");
|
|
13
|
+
return null;
|
|
14
|
+
}
|
|
15
|
+
} else {
|
|
16
|
+
console.error("Token is not set - cannot send a websocket message.");
|
|
17
|
+
return null;
|
|
50
18
|
}
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
19
|
+
if (!timeout || timeout < 0) {
|
|
20
|
+
this.manager.send({
|
|
21
|
+
token,
|
|
22
|
+
tenant: this.tenant,
|
|
23
|
+
action,
|
|
24
|
+
data: data || {}
|
|
25
|
+
});
|
|
26
|
+
return null;
|
|
55
27
|
}
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
28
|
+
return await new Promise((resolve, reject)=>{
|
|
29
|
+
let promiseTimeout = null;
|
|
30
|
+
const subscription = this.manager.onMessage(async (event)=>{
|
|
31
|
+
if (event.data.messageId !== subscription.id) return;
|
|
32
|
+
resolve(event.data);
|
|
33
|
+
subscription.off();
|
|
34
|
+
if (!promiseTimeout) return;
|
|
35
|
+
clearTimeout(promiseTimeout);
|
|
36
|
+
});
|
|
37
|
+
promiseTimeout = setTimeout(()=>{
|
|
38
|
+
const message = `Websocket action "${action}" timeout.`;
|
|
39
|
+
subscription.off();
|
|
40
|
+
reject(new Error(message));
|
|
41
|
+
}, timeout);
|
|
42
|
+
this.manager.send({
|
|
43
|
+
token,
|
|
44
|
+
tenant: this.tenant,
|
|
45
|
+
messageId: subscription.id,
|
|
46
|
+
action,
|
|
47
|
+
data: data || {}
|
|
48
|
+
});
|
|
49
|
+
}).catch((ex)=>{
|
|
50
|
+
console.error("Error while sending websocket message.", ex);
|
|
51
|
+
return null;
|
|
52
|
+
});
|
|
53
|
+
}
|
|
79
54
|
}
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
};
|
|
55
|
+
const createWebsocketsActions = (params)=>new WebsocketsActions(params);
|
|
56
|
+
export { WebsocketsActions, createWebsocketsActions };
|
|
83
57
|
|
|
84
58
|
//# sourceMappingURL=WebsocketsActions.js.map
|