@jskit-ai/realtime 0.2.6 → 0.2.8
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/package.json +13 -3
- package/patterns/realtime-application/example/package.json +2 -2
- package/src/client/RealtimeClientProvider.js +88 -55
- package/src/client/components/RealtimeConnectionIndicator.js +11 -31
- package/src/client/composables/useRealtimeEvent.js +36 -38
- package/src/client/connectionRecovery.js +11 -9
- package/src/client/runtime.js +16 -15
- package/src/client/types.js +46 -0
- package/src/server/RealtimeProvider.js +36 -151
- package/src/server/realtimeAudience.js +172 -78
- package/src/server/realtimeCapability.js +160 -0
- package/src/server/realtimeDelivery.js +109 -72
- package/src/server/runtime.js +47 -26
- package/src/server/servedClientBuild.js +10 -4
- package/src/server/types.js +18 -0
- package/test/authorization.test.js +21 -13
- package/test/clientRuntime.test.js +24 -29
- package/test/providerRuntime.test.js +43 -4
- package/test/realtimeEvent.test.js +137 -3
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@jskit-ai/realtime",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.8",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"scripts": {
|
|
6
6
|
"test": "node --test --test-concurrency=1"
|
|
@@ -8,10 +8,12 @@
|
|
|
8
8
|
"exports": {
|
|
9
9
|
"./server/RealtimeProvider": "./src/server/RealtimeProvider.js",
|
|
10
10
|
"./server/runtime": "./src/server/runtime.js",
|
|
11
|
+
"./server/types": "./src/server/types.js",
|
|
11
12
|
"./client": "./src/client/RealtimeClientProvider.js",
|
|
12
13
|
"./client/RealtimeClientProvider": "./src/client/RealtimeClientProvider.js",
|
|
13
14
|
"./client/composables/useRealtimeEvent": "./src/client/composables/useRealtimeEvent.js",
|
|
14
15
|
"./client/runtime": "./src/client/runtime.js",
|
|
16
|
+
"./client/types": "./src/client/types.js",
|
|
15
17
|
"./locales/en.json": "./locales/en.json"
|
|
16
18
|
},
|
|
17
19
|
"dependencies": {
|
|
@@ -21,7 +23,7 @@
|
|
|
21
23
|
"socket.io-client": "^4.8.3"
|
|
22
24
|
},
|
|
23
25
|
"peerDependencies": {
|
|
24
|
-
"@jskit-ai/kernel": "0.2.
|
|
26
|
+
"@jskit-ai/kernel": "0.2.5",
|
|
25
27
|
"vue": "^3.5.13"
|
|
26
28
|
},
|
|
27
29
|
"description": "Thin, generic realtime runtime wrappers for socket.io server and client.",
|
|
@@ -87,15 +89,23 @@
|
|
|
87
89
|
},
|
|
88
90
|
{
|
|
89
91
|
"subpath": "./client/composables/useRealtimeEvent",
|
|
90
|
-
"summary": "Exports useRealtimeEvent and
|
|
92
|
+
"summary": "Exports useRealtimeEvent, useRealtimeSocket, REALTIME_SOCKET_KEY and NO_REALTIME_SOCKET; listeners are owned by their Vue effect scope."
|
|
91
93
|
},
|
|
92
94
|
{
|
|
93
95
|
"subpath": "./client/runtime",
|
|
94
96
|
"summary": "Exports createSocketIoClient, disconnectSocketIoClient and reconnectSocketIoClient."
|
|
95
97
|
},
|
|
98
|
+
{
|
|
99
|
+
"subpath": "./client/types",
|
|
100
|
+
"summary": "Types of the application's socket, useRealtimeEvent's options and the client.realtime capability."
|
|
101
|
+
},
|
|
96
102
|
{
|
|
97
103
|
"subpath": "./locales/en.json",
|
|
98
104
|
"summary": "English catalog for the realtime connection indicator, registered by the generated client bootstrap."
|
|
105
|
+
},
|
|
106
|
+
{
|
|
107
|
+
"subpath": "./server/types",
|
|
108
|
+
"summary": "Type of the runtime.realtime capability, for features that require it."
|
|
99
109
|
}
|
|
100
110
|
],
|
|
101
111
|
"capabilities": {
|
|
@@ -3,10 +3,18 @@ import { normalizeObject, normalizeText } from "@jskit-ai/kernel/shared/support/
|
|
|
3
3
|
import { createProviderLogger } from "@jskit-ai/kernel/shared/support/providerLogger";
|
|
4
4
|
import { getClientAppConfig, resolveClientBootstrapDebugEnabled, resolveMobileConfig } from "@jskit-ai/kernel/client";
|
|
5
5
|
import RealtimeConnectionIndicator from "./components/RealtimeConnectionIndicator.js";
|
|
6
|
+
import { REALTIME_SOCKET_KEY } from "./composables/useRealtimeEvent.js";
|
|
6
7
|
import { createSocketIoClient, disconnectSocketIoClient } from "./runtime.js";
|
|
7
8
|
import { attachSocketConnectionRecovery } from "./connectionRecovery.js";
|
|
8
9
|
import { SERVED_CLIENT_BUILD_EVENT } from "../shared/servedClientBuild.js";
|
|
9
10
|
|
|
11
|
+
function skipReconnectPreparation() {}
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* @param {object} [capabilities]
|
|
15
|
+
* @param {import("@jskit-ai/kernel/client/types").ClientEnv} [capabilities.env]
|
|
16
|
+
* @param {import("./types.js").RealtimeMobile | null} [capabilities.mobile]
|
|
17
|
+
*/
|
|
10
18
|
function resolveRealtimeClientConfig({ env = {}, mobile = null } = {}) {
|
|
11
19
|
const appConfig = normalizeObject(getClientAppConfig());
|
|
12
20
|
const realtime = normalizeObject(appConfig.realtime);
|
|
@@ -35,11 +43,21 @@ function resolveRealtimeClientConfig({ env = {}, mobile = null } = {}) {
|
|
|
35
43
|
debugEnvKey: "VITE_REALTIME_DEBUG"
|
|
36
44
|
});
|
|
37
45
|
|
|
38
|
-
return Object.freeze({
|
|
46
|
+
return Object.freeze({
|
|
47
|
+
url,
|
|
48
|
+
options,
|
|
49
|
+
debugEnabled
|
|
50
|
+
});
|
|
39
51
|
}
|
|
40
52
|
|
|
41
|
-
|
|
42
|
-
|
|
53
|
+
/**
|
|
54
|
+
* @param {object} options
|
|
55
|
+
* @param {import("./types.js").RealtimeClientConfig} options.config
|
|
56
|
+
* @param {import("@jskit-ai/kernel/client/types").ClientLogger} options.logger
|
|
57
|
+
* @returns {import("./types.js").RealtimeClient}
|
|
58
|
+
*/
|
|
59
|
+
function createRealtimeClient({ config, logger: appLogger }) {
|
|
60
|
+
const logger = createProviderLogger(appLogger, {
|
|
43
61
|
debugEnabled: config.debugEnabled
|
|
44
62
|
});
|
|
45
63
|
const socket = createSocketIoClient({
|
|
@@ -49,53 +67,52 @@ function createRealtimeClient({ config, loggerInput } = {}) {
|
|
|
49
67
|
const servedClientBuild = trackServedClientBuild(socket, logger);
|
|
50
68
|
const detach = [];
|
|
51
69
|
let initialized = false;
|
|
52
|
-
let beforeReconnect =
|
|
70
|
+
let beforeReconnect = skipReconnectPreparation;
|
|
53
71
|
|
|
54
|
-
|
|
72
|
+
/** @param {import("@jskit-ai/kernel/client/types").ClientVueApp} vueApp */
|
|
73
|
+
function initialize(vueApp) {
|
|
55
74
|
if (initialized) {
|
|
56
75
|
return;
|
|
57
76
|
}
|
|
58
77
|
initialized = true;
|
|
59
78
|
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
79
|
+
detach.push(attachSocketConnectionRecovery(socket, { prepareReconnect: () => beforeReconnect() }));
|
|
80
|
+
const onConnect = () => logger.debug({ socketConnected: true }, "Realtime client socket connected.");
|
|
81
|
+
const onDisconnect = (reason) =>
|
|
82
|
+
logger.debug(
|
|
83
|
+
{ socketConnected: false, reason: String(reason || "") },
|
|
84
|
+
"Realtime client socket disconnected."
|
|
85
|
+
);
|
|
86
|
+
const onConnectError = (error) =>
|
|
87
|
+
logger.warn(
|
|
88
|
+
{ error: String(error?.message || error || "unknown error") },
|
|
89
|
+
"Realtime client socket connect error."
|
|
90
|
+
);
|
|
91
|
+
|
|
92
|
+
socket.on("connect", onConnect);
|
|
93
|
+
socket.on("disconnect", onDisconnect);
|
|
94
|
+
socket.on("connect_error", onConnectError);
|
|
95
|
+
detach.push(() => {
|
|
96
|
+
socket.off("connect", onConnect);
|
|
97
|
+
socket.off("disconnect", onDisconnect);
|
|
98
|
+
socket.off("connect_error", onConnectError);
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
if (config.debugEnabled === true) {
|
|
102
|
+
const onAny = (eventName, payload) =>
|
|
64
103
|
logger.debug(
|
|
65
|
-
{
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
"Realtime client socket connect error."
|
|
104
|
+
{
|
|
105
|
+
event: String(eventName || ""),
|
|
106
|
+
payloadScope: payload?.scope || null,
|
|
107
|
+
payloadEntityId: payload?.entityId || null
|
|
108
|
+
},
|
|
109
|
+
"Realtime client received socket event."
|
|
72
110
|
);
|
|
73
|
-
|
|
74
|
-
socket.
|
|
75
|
-
socket.on("disconnect", onDisconnect);
|
|
76
|
-
socket.on("connect_error", onConnectError);
|
|
77
|
-
detach.push(() => {
|
|
78
|
-
socket.off?.("connect", onConnect);
|
|
79
|
-
socket.off?.("disconnect", onDisconnect);
|
|
80
|
-
socket.off?.("connect_error", onConnectError);
|
|
81
|
-
});
|
|
82
|
-
|
|
83
|
-
if (config.debugEnabled === true && typeof socket.onAny === "function") {
|
|
84
|
-
const onAny = (eventName, payload) =>
|
|
85
|
-
logger.debug(
|
|
86
|
-
{
|
|
87
|
-
event: String(eventName || ""),
|
|
88
|
-
payloadScope: payload?.scope || null,
|
|
89
|
-
payloadEntityId: payload?.entityId || null
|
|
90
|
-
},
|
|
91
|
-
"Realtime client received socket event."
|
|
92
|
-
);
|
|
93
|
-
socket.onAny(onAny);
|
|
94
|
-
detach.push(() => socket.offAny?.(onAny));
|
|
95
|
-
}
|
|
111
|
+
socket.onAny(onAny);
|
|
112
|
+
detach.push(() => socket.offAny(onAny));
|
|
96
113
|
}
|
|
97
114
|
|
|
98
|
-
vueApp
|
|
115
|
+
vueApp.provide(REALTIME_SOCKET_KEY, socket);
|
|
99
116
|
}
|
|
100
117
|
|
|
101
118
|
function dispose() {
|
|
@@ -109,15 +126,15 @@ function createRealtimeClient({ config, loggerInput } = {}) {
|
|
|
109
126
|
initialized = false;
|
|
110
127
|
}
|
|
111
128
|
|
|
112
|
-
|
|
113
|
-
|
|
129
|
+
/**
|
|
130
|
+
* The step to run before recovery reconnects after the server refused or dropped the socket, such as auth refreshing
|
|
131
|
+
* the session.
|
|
132
|
+
* @param {() => unknown} prepare
|
|
133
|
+
*/
|
|
114
134
|
function setBeforeReconnect(prepare) {
|
|
115
|
-
if (typeof prepare !== "function") {
|
|
116
|
-
throw new TypeError("setBeforeReconnect requires a function.");
|
|
117
|
-
}
|
|
118
135
|
beforeReconnect = prepare;
|
|
119
136
|
return () => {
|
|
120
|
-
if (beforeReconnect === prepare) beforeReconnect =
|
|
137
|
+
if (beforeReconnect === prepare) beforeReconnect = skipReconnectPreparation;
|
|
121
138
|
};
|
|
122
139
|
}
|
|
123
140
|
|
|
@@ -131,9 +148,12 @@ function createRealtimeClient({ config, loggerInput } = {}) {
|
|
|
131
148
|
});
|
|
132
149
|
}
|
|
133
150
|
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
151
|
+
/**
|
|
152
|
+
* The server names the client build it serves on every connection. Listening from the socket's creation keeps the first
|
|
153
|
+
* announcement even when it arrives before anything subscribes; a subscriber receives the latest one at once.
|
|
154
|
+
* @param {import("./types.js").RealtimeSocket} socket
|
|
155
|
+
* @param {ReturnType<typeof createProviderLogger>} logger
|
|
156
|
+
*/
|
|
137
157
|
function trackServedClientBuild(socket, logger) {
|
|
138
158
|
const listeners = new Set();
|
|
139
159
|
let buildId = "";
|
|
@@ -153,18 +173,16 @@ function trackServedClientBuild(socket, logger) {
|
|
|
153
173
|
}
|
|
154
174
|
}
|
|
155
175
|
|
|
156
|
-
socket.on
|
|
176
|
+
socket.on(SERVED_CLIENT_BUILD_EVENT, onServedClientBuild);
|
|
157
177
|
return Object.freeze({
|
|
178
|
+
/** @param {(buildId: string) => void} listener */
|
|
158
179
|
subscribe(listener) {
|
|
159
|
-
if (typeof listener !== "function") {
|
|
160
|
-
throw new TypeError("onServedClientBuild requires a listener function.");
|
|
161
|
-
}
|
|
162
180
|
listeners.add(listener);
|
|
163
181
|
if (buildId) notify(listener);
|
|
164
182
|
return () => listeners.delete(listener);
|
|
165
183
|
},
|
|
166
184
|
dispose() {
|
|
167
|
-
socket.off
|
|
185
|
+
socket.off(SERVED_CLIENT_BUILD_EVENT, onServedClientBuild);
|
|
168
186
|
listeners.clear();
|
|
169
187
|
}
|
|
170
188
|
});
|
|
@@ -184,18 +202,33 @@ const RealtimeClientProvider = defineProvider({
|
|
|
184
202
|
provides: {
|
|
185
203
|
realtime: "client.realtime"
|
|
186
204
|
},
|
|
205
|
+
/**
|
|
206
|
+
* @param {object} capabilities
|
|
207
|
+
* @param {import("@jskit-ai/kernel/client/types").ClientComponents} capabilities.components
|
|
208
|
+
* @param {import("@jskit-ai/kernel/client/types").ClientEnv} capabilities.env
|
|
209
|
+
* @param {import("@jskit-ai/kernel/client/types").ClientLogger} capabilities.logger
|
|
210
|
+
* @param {import("./types.js").RealtimeMobile | null} capabilities.mobile
|
|
211
|
+
*/
|
|
187
212
|
setup({ components, env, logger, mobile }) {
|
|
188
213
|
components.register("realtime.web.connection.indicator", RealtimeConnectionIndicator);
|
|
189
214
|
return {
|
|
190
215
|
realtime: createRealtimeClient({
|
|
191
216
|
config: resolveRealtimeClientConfig({ env, mobile }),
|
|
192
|
-
|
|
217
|
+
logger
|
|
193
218
|
})
|
|
194
219
|
};
|
|
195
220
|
},
|
|
221
|
+
/**
|
|
222
|
+
* @param {{ vueApp: import("@jskit-ai/kernel/client/types").ClientVueApp }} capabilities
|
|
223
|
+
* @param {{ outputs: { realtime: import("./types.js").RealtimeClient } }} lifecycle
|
|
224
|
+
*/
|
|
196
225
|
boot({ vueApp }, { outputs }) {
|
|
197
226
|
outputs.realtime.initialize(vueApp);
|
|
198
227
|
},
|
|
228
|
+
/**
|
|
229
|
+
* @param {unknown} _dependencies
|
|
230
|
+
* @param {{ outputs: { realtime: import("./types.js").RealtimeClient } }} lifecycle
|
|
231
|
+
*/
|
|
199
232
|
shutdown(_dependencies, { outputs }) {
|
|
200
233
|
outputs.realtime.dispose();
|
|
201
234
|
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { computed, defineComponent, h, onBeforeUnmount, onMounted, ref } from "vue";
|
|
2
|
-
import {
|
|
2
|
+
import { NO_REALTIME_SOCKET, useRealtimeSocket } from "../composables/useRealtimeEvent.js";
|
|
3
3
|
import { reconnectSocketIoClient } from "../runtime.js";
|
|
4
4
|
import { useJskitText } from "../i18n.js";
|
|
5
5
|
|
|
@@ -51,10 +51,9 @@ function resolveTooltipText({ realtimeAvailable, connected }) {
|
|
|
51
51
|
const RealtimeConnectionIndicator = defineComponent({
|
|
52
52
|
name: "RealtimeConnectionIndicator",
|
|
53
53
|
setup() {
|
|
54
|
-
const socket = useRealtimeSocket(
|
|
55
|
-
const connected = ref(Boolean(socket
|
|
56
|
-
const realtimeAvailable = socket !==
|
|
57
|
-
let detach = null;
|
|
54
|
+
const socket = useRealtimeSocket();
|
|
55
|
+
const connected = ref(Boolean(socket.connected));
|
|
56
|
+
const realtimeAvailable = socket !== NO_REALTIME_SOCKET;
|
|
58
57
|
|
|
59
58
|
const tooltipText = computed(() => {
|
|
60
59
|
return resolveTooltipText({
|
|
@@ -76,40 +75,21 @@ const RealtimeConnectionIndicator = defineComponent({
|
|
|
76
75
|
connected.value = Boolean(nextState);
|
|
77
76
|
}
|
|
78
77
|
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
return;
|
|
83
|
-
}
|
|
78
|
+
const onConnect = () => syncConnected(true);
|
|
79
|
+
const onDisconnect = () => syncConnected(false);
|
|
80
|
+
const onConnectError = () => syncConnected(false);
|
|
84
81
|
|
|
82
|
+
onMounted(() => {
|
|
85
83
|
syncConnected(socket.connected);
|
|
86
|
-
|
|
87
|
-
const onConnect = () => syncConnected(true);
|
|
88
|
-
const onDisconnect = () => syncConnected(false);
|
|
89
|
-
const onConnectError = () => syncConnected(false);
|
|
90
|
-
|
|
91
84
|
socket.on("connect", onConnect);
|
|
92
85
|
socket.on("disconnect", onDisconnect);
|
|
93
86
|
socket.on("connect_error", onConnectError);
|
|
94
|
-
|
|
95
|
-
detach = () => {
|
|
96
|
-
if (typeof socket.off === "function") {
|
|
97
|
-
socket.off("connect", onConnect);
|
|
98
|
-
socket.off("disconnect", onDisconnect);
|
|
99
|
-
socket.off("connect_error", onConnectError);
|
|
100
|
-
}
|
|
101
|
-
};
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
onMounted(() => {
|
|
105
|
-
bindSocketStatusListeners();
|
|
106
87
|
});
|
|
107
88
|
|
|
108
89
|
onBeforeUnmount(() => {
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
detach = null;
|
|
90
|
+
socket.off("connect", onConnect);
|
|
91
|
+
socket.off("disconnect", onDisconnect);
|
|
92
|
+
socket.off("connect_error", onConnectError);
|
|
113
93
|
});
|
|
114
94
|
|
|
115
95
|
return () =>
|
|
@@ -1,20 +1,30 @@
|
|
|
1
1
|
import { inject, onScopeDispose, ref, unref, watch } from "vue";
|
|
2
2
|
import { normalizeText } from "@jskit-ai/kernel/shared/support/normalize";
|
|
3
3
|
|
|
4
|
-
const
|
|
4
|
+
const REALTIME_SOCKET_KEY = "jskit.realtime.runtime.client.socket";
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* The socket of an application without the realtime provider: it is never connected, and nothing it is asked to do
|
|
8
|
+
* happens.
|
|
9
|
+
* @type {import("../types.js").RealtimeSocket}
|
|
10
|
+
*/
|
|
11
|
+
const NO_REALTIME_SOCKET = Object.freeze({
|
|
12
|
+
active: false,
|
|
13
|
+
connected: false,
|
|
14
|
+
connect() {},
|
|
15
|
+
disconnect() {},
|
|
16
|
+
emit() {},
|
|
17
|
+
timeout() {
|
|
18
|
+
return NO_REALTIME_SOCKET;
|
|
19
|
+
},
|
|
5
20
|
on() {},
|
|
6
21
|
off() {},
|
|
7
22
|
onAny() {},
|
|
8
23
|
offAny() {}
|
|
9
24
|
});
|
|
10
25
|
|
|
11
|
-
function
|
|
12
|
-
return
|
|
13
|
-
socket &&
|
|
14
|
-
typeof socket === "object" &&
|
|
15
|
-
typeof socket.on === "function" &&
|
|
16
|
-
typeof socket.off === "function"
|
|
17
|
-
);
|
|
26
|
+
function matchesEveryEvent() {
|
|
27
|
+
return true;
|
|
18
28
|
}
|
|
19
29
|
|
|
20
30
|
function resolveEnabled(value) {
|
|
@@ -29,35 +39,27 @@ function resolveEventName(value) {
|
|
|
29
39
|
return normalized || "*";
|
|
30
40
|
}
|
|
31
41
|
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
if (required) {
|
|
39
|
-
throw new Error("Realtime client socket is not available in Vue injection context.");
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
return EMPTY_REALTIME_SOCKET;
|
|
42
|
+
/**
|
|
43
|
+
* The application's socket, or `NO_REALTIME_SOCKET` in an application without the realtime provider.
|
|
44
|
+
* @returns {import("../types.js").RealtimeSocket}
|
|
45
|
+
*/
|
|
46
|
+
function useRealtimeSocket() {
|
|
47
|
+
return inject(REALTIME_SOCKET_KEY, NO_REALTIME_SOCKET);
|
|
43
48
|
}
|
|
44
49
|
|
|
50
|
+
/** @param {import("../types.js").RealtimeEventOptions} options */
|
|
45
51
|
function useRealtimeEvent({
|
|
46
52
|
event = "*",
|
|
47
53
|
enabled = true,
|
|
48
|
-
matches =
|
|
54
|
+
matches = matchesEveryEvent,
|
|
49
55
|
onEvent
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
throw new TypeError("useRealtimeEvent requires onEvent().");
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
const socket = useRealtimeSocket({ required: false });
|
|
56
|
+
}) {
|
|
57
|
+
const socket = useRealtimeSocket();
|
|
56
58
|
const active = ref(false);
|
|
57
59
|
let release = null;
|
|
58
60
|
|
|
59
61
|
function detach() {
|
|
60
|
-
if (
|
|
62
|
+
if (release) {
|
|
61
63
|
try {
|
|
62
64
|
release();
|
|
63
65
|
} catch {}
|
|
@@ -73,7 +75,7 @@ function useRealtimeEvent({
|
|
|
73
75
|
socket
|
|
74
76
|
});
|
|
75
77
|
|
|
76
|
-
if (
|
|
78
|
+
if (matches(context) !== true) {
|
|
77
79
|
return;
|
|
78
80
|
}
|
|
79
81
|
|
|
@@ -91,7 +93,7 @@ function useRealtimeEvent({
|
|
|
91
93
|
function attach() {
|
|
92
94
|
detach();
|
|
93
95
|
|
|
94
|
-
if (socket ===
|
|
96
|
+
if (socket === NO_REALTIME_SOCKET) {
|
|
95
97
|
return;
|
|
96
98
|
}
|
|
97
99
|
if (!resolveEnabled(enabled)) {
|
|
@@ -100,15 +102,10 @@ function useRealtimeEvent({
|
|
|
100
102
|
|
|
101
103
|
const eventName = resolveEventName(event);
|
|
102
104
|
if (eventName === "*") {
|
|
103
|
-
if (typeof socket.onAny !== "function") {
|
|
104
|
-
return;
|
|
105
|
-
}
|
|
106
105
|
const onAny = (nextEventName, payload) => runHandler(nextEventName, payload);
|
|
107
106
|
socket.onAny(onAny);
|
|
108
107
|
release = () => {
|
|
109
|
-
|
|
110
|
-
socket.offAny(onAny);
|
|
111
|
-
}
|
|
108
|
+
socket.offAny(onAny);
|
|
112
109
|
};
|
|
113
110
|
active.value = true;
|
|
114
111
|
return;
|
|
@@ -132,13 +129,14 @@ function useRealtimeEvent({
|
|
|
132
129
|
|
|
133
130
|
onScopeDispose(detach);
|
|
134
131
|
|
|
135
|
-
return
|
|
132
|
+
return {
|
|
136
133
|
active
|
|
137
|
-
}
|
|
134
|
+
};
|
|
138
135
|
}
|
|
139
136
|
|
|
140
137
|
export {
|
|
141
|
-
|
|
138
|
+
NO_REALTIME_SOCKET,
|
|
139
|
+
REALTIME_SOCKET_KEY,
|
|
142
140
|
useRealtimeSocket,
|
|
143
141
|
useRealtimeEvent
|
|
144
142
|
};
|
|
@@ -1,8 +1,12 @@
|
|
|
1
1
|
import { reconnectSocketIoClient } from "./runtime.js";
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
3
|
+
/**
|
|
4
|
+
* Reconnects a socket Socket.IO will not retry by itself; returns the function that stops.
|
|
5
|
+
* @param {import("socket.io-client").Socket} socket
|
|
6
|
+
* @param {object} recovery
|
|
7
|
+
* @param {() => unknown} recovery.prepareReconnect Runs before each recovery reconnect; auth uses it to refresh an expired session over HTTP so the handshake carries fresh cookies.
|
|
8
|
+
*/
|
|
9
|
+
function attachSocketConnectionRecovery(socket, { prepareReconnect }) {
|
|
6
10
|
let timer = null;
|
|
7
11
|
let delay = 1_000;
|
|
8
12
|
let recovering = false;
|
|
@@ -21,12 +25,10 @@ function attachSocketConnectionRecovery(socket, { prepareReconnect = null } = {}
|
|
|
21
25
|
}
|
|
22
26
|
|
|
23
27
|
async function retry() {
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
// The handshake then decides; a refusal schedules the next retry.
|
|
29
|
-
}
|
|
28
|
+
try {
|
|
29
|
+
await prepareReconnect();
|
|
30
|
+
} catch {
|
|
31
|
+
// The handshake then decides; a refusal schedules the next retry.
|
|
30
32
|
}
|
|
31
33
|
if (canRetry()) socket.connect();
|
|
32
34
|
}
|
package/src/client/runtime.js
CHANGED
|
@@ -1,21 +1,22 @@
|
|
|
1
1
|
import { io as connectSocketIoClient } from "socket.io-client";
|
|
2
|
-
import { normalizeText } from "@jskit-ai/kernel/shared/support/normalize";
|
|
2
|
+
import { normalizeObject, normalizeText } from "@jskit-ai/kernel/shared/support/normalize";
|
|
3
3
|
|
|
4
4
|
const SOCKET_IO_PATH = "/socket.io";
|
|
5
5
|
|
|
6
|
+
/**
|
|
7
|
+
* @param {object} client
|
|
8
|
+
* @param {string} [client.url]
|
|
9
|
+
* @param {Partial<import("socket.io-client").ManagerOptions & import("socket.io-client").SocketOptions>} [client.options]
|
|
10
|
+
* @param {typeof connectSocketIoClient} [client.connect]
|
|
11
|
+
*/
|
|
6
12
|
function createSocketIoClient({
|
|
7
13
|
url = "",
|
|
8
14
|
options = {},
|
|
9
15
|
connect = connectSocketIoClient
|
|
10
|
-
}
|
|
11
|
-
if (typeof connect !== "function") {
|
|
12
|
-
throw new Error("createSocketIoClient requires a valid socket.io client connect function.");
|
|
13
|
-
}
|
|
14
|
-
|
|
16
|
+
}) {
|
|
15
17
|
const normalizedUrl = normalizeText(url);
|
|
16
|
-
const source = options && typeof options === "object" && !Array.isArray(options) ? options : {};
|
|
17
18
|
const normalizedOptions = {
|
|
18
|
-
...
|
|
19
|
+
...normalizeObject(options),
|
|
19
20
|
path: SOCKET_IO_PATH
|
|
20
21
|
};
|
|
21
22
|
if (normalizedUrl) {
|
|
@@ -24,18 +25,18 @@ function createSocketIoClient({
|
|
|
24
25
|
return connect(normalizedOptions);
|
|
25
26
|
}
|
|
26
27
|
|
|
28
|
+
/** @param {import("./types.js").RealtimeSocket} socket */
|
|
27
29
|
function disconnectSocketIoClient(socket) {
|
|
28
|
-
if (!socket || typeof socket.disconnect !== "function") {
|
|
29
|
-
return;
|
|
30
|
-
}
|
|
31
30
|
socket.disconnect();
|
|
32
31
|
}
|
|
33
32
|
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
33
|
+
/**
|
|
34
|
+
* Socket.IO ignores connect() while its own reconnection waits out a backoff delay or an attempt left hanging by a
|
|
35
|
+
* sleeping laptop. Closing that pending attempt first makes connect() open a fresh transport immediately.
|
|
36
|
+
* @param {import("./types.js").RealtimeSocket} socket
|
|
37
|
+
*/
|
|
37
38
|
function reconnectSocketIoClient(socket) {
|
|
38
|
-
if (
|
|
39
|
+
if (socket.connected) {
|
|
39
40
|
return;
|
|
40
41
|
}
|
|
41
42
|
if (socket.active) {
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
// The values realtime's client hands out and receives across files, for its own modules and for the packages that use
|
|
2
|
+
// the application's socket or the `client.realtime` capability.
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* The application's socket as client code uses it: Socket.IO's own, or `NO_REALTIME_SOCKET` in an application without
|
|
6
|
+
* the realtime provider.
|
|
7
|
+
* @typedef {Pick<import("socket.io-client").Socket, "active" | "connected" | "connect" | "disconnect" | "emit" | "timeout" | "on" | "off" | "onAny" | "offAny">} RealtimeSocket
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* What a listener receives for one socket event.
|
|
12
|
+
* @typedef {object} RealtimeEventContext
|
|
13
|
+
* @property {string} event
|
|
14
|
+
* @property {any} payload
|
|
15
|
+
* @property {RealtimeSocket} socket
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* What `useRealtimeEvent()` listens for.
|
|
20
|
+
* @typedef {object} RealtimeEventOptions
|
|
21
|
+
* @property {import("vue").MaybeRef<string>} [event] One event's name; every event when it is `"*"` or empty.
|
|
22
|
+
* @property {import("vue").MaybeRef<boolean>} [enabled]
|
|
23
|
+
* @property {(context: RealtimeEventContext) => boolean} [matches] Only an event this returns true for is handled.
|
|
24
|
+
* @property {(context: RealtimeEventContext) => unknown} onEvent
|
|
25
|
+
*/
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* The part of the optional `client.mobile` capability realtime reads.
|
|
29
|
+
* @typedef {object} RealtimeMobile
|
|
30
|
+
* @property {{ available?: boolean }} [adapter]
|
|
31
|
+
*/
|
|
32
|
+
|
|
33
|
+
/** @typedef {ReturnType<typeof import("./RealtimeClientProvider.js").resolveRealtimeClientConfig>} RealtimeClientConfig */
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* The `client.realtime` capability.
|
|
37
|
+
* @typedef {object} RealtimeClient
|
|
38
|
+
* @property {RealtimeClientConfig} config
|
|
39
|
+
* @property {() => void} dispose
|
|
40
|
+
* @property {(vueApp: import("@jskit-ai/kernel/client/types").ClientVueApp) => void} initialize
|
|
41
|
+
* @property {(listener: (buildId: string) => void) => () => void} onServedClientBuild Calls the listener with each client build a server names, and at once with the latest one; returns the unsubscribe.
|
|
42
|
+
* @property {(prepare: () => unknown) => () => void} setBeforeReconnect Sets the step recovery runs before reconnecting; returns the function that removes it.
|
|
43
|
+
* @property {import("socket.io-client").Socket} socket
|
|
44
|
+
*/
|
|
45
|
+
|
|
46
|
+
export {};
|