gemi 0.128.0 → 0.129.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/{chunk-0mp9tck6.js → chunk-0ryswty9.js} +20 -20
- package/dist/{chunk-0mp9tck6.js.map → chunk-0ryswty9.js.map} +5 -4
- package/dist/{chunk-wzfvwvz0.js → chunk-jj1ppnnk.js} +2 -2
- package/dist/{chunk-wzfvwvz0.js.map → chunk-jj1ppnnk.js.map} +1 -1
- package/dist/{chunk-tchte4dv.js → chunk-sbdm4enk.js} +2 -2
- package/dist/{chunk-tchte4dv.js.map → chunk-sbdm4enk.js.map} +1 -1
- package/dist/{chunk-df0mekds.js → chunk-wmkrb1qv.js} +1 -1
- package/dist/{chunk-qz0da20r.js → chunk-zw5z1k84.js} +1 -1
- package/dist/chunks/RealtimeClient-CqGve6h8.js +545 -0
- package/dist/chunks/RealtimeClient-CqGve6h8.js.map +1 -0
- package/dist/chunks/{ThemeProvider-BIrCSrFY.js → ThemeProvider-DaX0eyqs.js} +155 -539
- package/dist/chunks/ThemeProvider-DaX0eyqs.js.map +1 -0
- package/dist/chunks/live-DIUDdLrA.js +69 -0
- package/dist/chunks/live-DIUDdLrA.js.map +1 -0
- package/dist/chunks/subscriptionStream-CULcRbx0.js +166 -0
- package/dist/chunks/subscriptionStream-CULcRbx0.js.map +1 -0
- package/dist/client/index.d.ts +1 -1
- package/dist/client/index.d.ts.map +1 -1
- package/dist/client/index.js +118 -330
- package/dist/client/index.js.map +1 -1
- package/dist/client/init.d.ts +1 -1
- package/dist/client/init.d.ts.map +1 -1
- package/dist/client/realtime/FakeSocket.d.ts +1 -1
- package/dist/client/realtime/FakeSocket.d.ts.map +1 -1
- package/dist/client/realtime/RealtimeClient.d.ts +4 -20
- package/dist/client/realtime/RealtimeClient.d.ts.map +1 -1
- package/dist/client/realtime/live.d.ts +25 -0
- package/dist/client/realtime/live.d.ts.map +1 -0
- package/dist/client/realtime/loader.d.ts +59 -0
- package/dist/client/realtime/loader.d.ts.map +1 -0
- package/dist/client/realtime/useChannelSubscription.d.ts +1 -1
- package/dist/client/realtime/useChannelSubscription.d.ts.map +1 -1
- package/dist/client/subscriptionStream.d.ts +36 -0
- package/dist/client/subscriptionStream.d.ts.map +1 -0
- package/dist/client/useChannel.d.ts +1 -13
- package/dist/client/useChannel.d.ts.map +1 -1
- package/dist/client/useQuery.d.ts +1 -1
- package/dist/client/useQuery.d.ts.map +1 -1
- package/dist/client/useSubscription.d.ts +5 -10
- package/dist/client/useSubscription.d.ts.map +1 -1
- package/dist/facades/index.js +1 -1
- package/dist/http/index.js +1 -1
- package/dist/i18n/index.js +1 -1
- package/dist/kernel/index.js +1 -1
- package/dist/server/index.js +1 -1
- package/dist/services/broadcast/protocol.d.ts +1 -2
- package/dist/services/broadcast/protocol.d.ts.map +1 -1
- package/dist/services/broadcast/socketHeader.d.ts +9 -0
- package/dist/services/broadcast/socketHeader.d.ts.map +1 -0
- package/dist/services/index.js +2 -2
- package/dist/services/index.js.map +1 -1
- package/dist/testing/Page.d.ts.map +1 -1
- package/dist/testing/index.js +3 -1
- package/dist/testing/index.js.map +1 -1
- package/package.json +1 -1
- package/dist/chunks/ThemeProvider-BIrCSrFY.js.map +0 -1
- /package/dist/{chunk-df0mekds.js.map → chunk-wmkrb1qv.js.map} +0 -0
- /package/dist/{chunk-qz0da20r.js.map → chunk-zw5z1k84.js.map} +0 -0
|
@@ -752,544 +752,15 @@ function toVariantKey(search) {
|
|
|
752
752
|
return searchParams.toString();
|
|
753
753
|
}
|
|
754
754
|
//#endregion
|
|
755
|
-
//#region
|
|
756
|
-
/**
|
|
757
|
-
* The `gemi.v1` WebSocket protocol, shared by the server transport
|
|
758
|
-
* (`SocketHub`) and the browser client (`gemi/client`'s connection manager).
|
|
759
|
-
* Types and constants only: this module is imported by the browser bundle, so
|
|
760
|
-
* it must never import a server module.
|
|
761
|
-
*
|
|
762
|
-
* Every frame is a JSON text frame. Binary frames are refused.
|
|
763
|
-
*
|
|
764
|
-
* From the client:
|
|
765
|
-
*
|
|
766
|
-
* - `{op:"sub", id, ch, p?}`: subscribe `id` (chosen by the client, unique per
|
|
767
|
-
* socket) to the channel pattern `ch` with params `p`. The server builds
|
|
768
|
-
* the topic and authorizes it.
|
|
769
|
-
* - `{op:"unsub", id}`
|
|
770
|
-
* - `{op:"ping"}`: answered with `pong`. The client sends one every
|
|
771
|
-
* `heartbeatMs` and reconnects when it hears nothing for twice that.
|
|
772
|
-
*
|
|
773
|
-
* From the server:
|
|
774
|
-
*
|
|
775
|
-
* - `{op:"hello", socketId, tag, heartbeatMs}`: the first frame. `socketId`
|
|
776
|
-
* goes in the `X-Gemi-Socket` header of the client's own requests (for
|
|
777
|
-
* `Broadcast.toOthers`); it is never shown to anyone else. `tag` is what
|
|
778
|
-
* an event frame's `x` carries when it skips this socket.
|
|
779
|
-
* - `{op:"subscribed", id, t}`: `id` joined topic `t`. Sent only once this
|
|
780
|
-
* process receives the topic's events, so the resync the client runs on it
|
|
781
|
-
* cannot miss one.
|
|
782
|
-
* - `{op:"denied", id, code}`: `id` was refused, or later revoked.
|
|
783
|
-
* - `{op:"ev", t, ev, d?, x?}`: an event on topic `t`. Dropped by the socket
|
|
784
|
-
* whose `tag` is `x`.
|
|
785
|
-
* - `{op:"gap"}`: events may have been lost for this socket; resync all.
|
|
786
|
-
* - `{op:"pong"}`
|
|
787
|
-
* - `{op:"bye", code, retryAfter}`: the server is going away (a restart).
|
|
788
|
-
* Reconnect after `retryAfter` ms.
|
|
789
|
-
*/
|
|
790
|
-
/** The WebSocket subprotocol. */
|
|
791
|
-
var BROADCAST_PROTOCOL = "gemi.v1";
|
|
792
|
-
/** The request header a client sends its socket id in, for `toOthers`. */
|
|
793
|
-
var SOCKET_ID_HEADER = "x-gemi-socket";
|
|
794
|
-
/**
|
|
795
|
-
* Close codes the server uses. 1012 and 1013 are the standard "restart" and
|
|
796
|
-
* "try again later"; the 4xxx ones are gemi's own.
|
|
797
|
-
*/
|
|
798
|
-
var CloseCode = {
|
|
799
|
-
/** The client went away (tab closed, socket released). */
|
|
800
|
-
Normal: 1e3,
|
|
801
|
-
/** A frame that was not JSON text, or not a known op. */
|
|
802
|
-
UnsupportedData: 1003,
|
|
803
|
-
/** Too many frames, or a frame over the size limit. */
|
|
804
|
-
PolicyViolation: 1008,
|
|
805
|
-
/** The server is restarting; `bye` said when to come back. */
|
|
806
|
-
Restart: 1012,
|
|
807
|
-
/** The socket fell too far behind; reconnect and resync. */
|
|
808
|
-
TryAgainLater: 1013,
|
|
809
|
-
/** The socket's user was signed out or its access revoked. */
|
|
810
|
-
Revoked: 4001
|
|
811
|
-
};
|
|
812
|
-
/**
|
|
813
|
-
* Whether a refusal is final for this socket's session: resubscribing will
|
|
814
|
-
* get the same answer, so the client does not retry it on its own.
|
|
815
|
-
*/
|
|
816
|
-
function isPermanentDenial(code) {
|
|
817
|
-
return code === "unknown_channel" || code === "invalid_params" || code === "invalid";
|
|
818
|
-
}
|
|
819
|
-
//#endregion
|
|
820
|
-
//#region client/realtime/RealtimeClient.ts
|
|
821
|
-
/** The minimum gap between two resyncs of one subscription. */
|
|
822
|
-
var RESYNC_COALESCE_MS = 2e3;
|
|
823
|
-
/** How long the socket stays open after its last subscription goes. */
|
|
824
|
-
var IDLE_CLOSE_MS = 5e3;
|
|
825
|
-
/**
|
|
826
|
-
* How long a subscription outlives its last listener. A hook that remounts
|
|
827
|
-
* within it (StrictMode's double mount, a route change back, a list that
|
|
828
|
-
* re-renders its rows) picks the same subscription up again: no `unsub`, no
|
|
829
|
-
* new `sub`, nothing counted against the server's rate limit.
|
|
830
|
-
*/
|
|
831
|
-
var RELEASE_LINGER_MS = 1500;
|
|
832
|
-
/** The least wait before resending a `sub` denied with `rate_limited`. */
|
|
833
|
-
var RATE_LIMITED_RETRY_MS = 5e3;
|
|
834
|
-
var MIN_BACKOFF_MS = 1e3;
|
|
835
|
-
var MAX_BACKOFF_MS = 3e4;
|
|
836
|
-
var DEFAULT_HEARTBEAT_MS = 25e3;
|
|
837
|
-
var DEFAULT_HIDDEN_DISCONNECT_MS = 6e4;
|
|
755
|
+
//#region client/realtime/loader.ts
|
|
838
756
|
/** `pattern` and its params, canonical: two hooks on one channel share a `sub`. */
|
|
839
757
|
function channelKey(pattern, params) {
|
|
840
758
|
const sorted = Object.keys(params).sort().map((key) => [key, String(params[key])]);
|
|
841
759
|
return `${pattern}\u0000${JSON.stringify(sorted)}`;
|
|
842
760
|
}
|
|
843
|
-
/**
|
|
844
|
-
* The tab's one WebSocket to the app (`gemi.v1`), multiplexing every channel
|
|
845
|
-
* the mounted hooks subscribe to.
|
|
846
|
-
*
|
|
847
|
-
* Opened by the first subscription and closed a few seconds after the last
|
|
848
|
-
* one goes. Reconnects with full-jitter backoff (1 to 30 s), or after the
|
|
849
|
-
* server's `bye.retryAfter`, at once on `online`; resubscribes everything
|
|
850
|
-
* then. A tab hidden for `hiddenDisconnectMs` disconnects, and reconnects
|
|
851
|
-
* when shown. Every `subscribed` ack and every `gap` resyncs the
|
|
852
|
-
* subscription (coalesced to one per 2 s), because events are volatile: a
|
|
853
|
-
* client that may have missed one refetches over HTTP.
|
|
854
|
-
*/
|
|
855
|
-
var RealtimeClient = class {
|
|
856
|
-
options;
|
|
857
|
-
internals;
|
|
858
|
-
ws = null;
|
|
859
|
-
entries = /* @__PURE__ */ new Map();
|
|
860
|
-
byId = /* @__PURE__ */ new Map();
|
|
861
|
-
topics = /* @__PURE__ */ new Map();
|
|
862
|
-
nextId = 1;
|
|
863
|
-
attempt = 0;
|
|
864
|
-
tag = null;
|
|
865
|
-
socketIdValue = null;
|
|
866
|
-
heartbeatMs = DEFAULT_HEARTBEAT_MS;
|
|
867
|
-
reconnectTimer = null;
|
|
868
|
-
idleTimer = null;
|
|
869
|
-
hiddenTimer = null;
|
|
870
|
-
pingTimer = null;
|
|
871
|
-
livenessTimer = null;
|
|
872
|
-
retryAfter = null;
|
|
873
|
-
revoked = false;
|
|
874
|
-
pausedWhileHidden = false;
|
|
875
|
-
listening = false;
|
|
876
|
-
constructor(options = {}, internals = {}) {
|
|
877
|
-
this.options = withDefaults(options);
|
|
878
|
-
this.internals = internals;
|
|
879
|
-
}
|
|
880
|
-
configure(options) {
|
|
881
|
-
this.options = withDefaults({
|
|
882
|
-
...this.options,
|
|
883
|
-
...options
|
|
884
|
-
});
|
|
885
|
-
}
|
|
886
|
-
/** The id to send as `X-Gemi-Socket`, while the socket is open. */
|
|
887
|
-
get socketId() {
|
|
888
|
-
return this.socketIdValue;
|
|
889
|
-
}
|
|
890
|
-
subscribe(pattern, params, listener) {
|
|
891
|
-
const key = channelKey(pattern, params);
|
|
892
|
-
let entry = this.entries.get(key);
|
|
893
|
-
if (!entry) {
|
|
894
|
-
entry = {
|
|
895
|
-
key,
|
|
896
|
-
id: String(this.nextId++),
|
|
897
|
-
pattern,
|
|
898
|
-
params: { ...params },
|
|
899
|
-
listeners: /* @__PURE__ */ new Set(),
|
|
900
|
-
status: "closed",
|
|
901
|
-
code: null,
|
|
902
|
-
permanent: false,
|
|
903
|
-
topic: null,
|
|
904
|
-
lastResyncAt: 0,
|
|
905
|
-
resyncTimer: null,
|
|
906
|
-
lingerTimer: null,
|
|
907
|
-
retryTimer: null,
|
|
908
|
-
retries: 0
|
|
909
|
-
};
|
|
910
|
-
this.entries.set(key, entry);
|
|
911
|
-
this.byId.set(entry.id, entry);
|
|
912
|
-
if (this.isOpen()) this.sendSub(entry);
|
|
913
|
-
}
|
|
914
|
-
if (entry.lingerTimer) {
|
|
915
|
-
clearTimeout(entry.lingerTimer);
|
|
916
|
-
entry.lingerTimer = null;
|
|
917
|
-
}
|
|
918
|
-
entry.listeners.add(listener);
|
|
919
|
-
listener.onStatus?.(entry.status, entry.code);
|
|
920
|
-
this.cancelIdleClose();
|
|
921
|
-
this.listen();
|
|
922
|
-
if (!this.ws && !this.reconnectTimer && !this.pausedWhileHidden) this.connect();
|
|
923
|
-
let released = false;
|
|
924
|
-
return () => {
|
|
925
|
-
if (released) return;
|
|
926
|
-
released = true;
|
|
927
|
-
this.release(entry, listener);
|
|
928
|
-
};
|
|
929
|
-
}
|
|
930
|
-
release(entry, listener) {
|
|
931
|
-
entry.listeners.delete(listener);
|
|
932
|
-
if (entry.listeners.size > 0 || entry.lingerTimer) return;
|
|
933
|
-
entry.lingerTimer = setTimeout(() => {
|
|
934
|
-
entry.lingerTimer = null;
|
|
935
|
-
if (entry.listeners.size === 0) this.drop(entry);
|
|
936
|
-
}, RELEASE_LINGER_MS);
|
|
937
|
-
}
|
|
938
|
-
drop(entry) {
|
|
939
|
-
if (entry.resyncTimer) clearTimeout(entry.resyncTimer);
|
|
940
|
-
this.clearRetry(entry);
|
|
941
|
-
this.entries.delete(entry.key);
|
|
942
|
-
this.byId.delete(entry.id);
|
|
943
|
-
this.dropTopic(entry);
|
|
944
|
-
if (this.isOpen() && (entry.status === "open" || entry.status === "connecting")) this.send({
|
|
945
|
-
op: "unsub",
|
|
946
|
-
id: entry.id
|
|
947
|
-
});
|
|
948
|
-
if (this.entries.size === 0) this.scheduleIdleClose();
|
|
949
|
-
}
|
|
950
|
-
isOpen() {
|
|
951
|
-
return this.ws !== null && this.ws.readyState === 1;
|
|
952
|
-
}
|
|
953
|
-
connect() {
|
|
954
|
-
if (typeof window === "undefined") return;
|
|
955
|
-
if (this.entries.size === 0) return;
|
|
956
|
-
const Impl = this.internals.WebSocket ?? globalThis.WebSocket;
|
|
957
|
-
if (!Impl) return;
|
|
958
|
-
this.clearReconnect();
|
|
959
|
-
let ws;
|
|
960
|
-
try {
|
|
961
|
-
ws = new Impl(this.url(), [BROADCAST_PROTOCOL]);
|
|
962
|
-
} catch {
|
|
963
|
-
this.scheduleReconnect();
|
|
964
|
-
return;
|
|
965
|
-
}
|
|
966
|
-
this.ws = ws;
|
|
967
|
-
for (const entry of this.entries.values()) if (!entry.permanent) this.setStatus(entry, "connecting", null);
|
|
968
|
-
ws.onopen = () => {
|
|
969
|
-
if (this.ws !== ws) return;
|
|
970
|
-
this.touch();
|
|
971
|
-
for (const entry of this.entries.values()) {
|
|
972
|
-
if (entry.permanent) continue;
|
|
973
|
-
entry.retries = 0;
|
|
974
|
-
this.sendSub(entry);
|
|
975
|
-
}
|
|
976
|
-
};
|
|
977
|
-
ws.onmessage = (event) => {
|
|
978
|
-
if (this.ws !== ws) return;
|
|
979
|
-
this.touch();
|
|
980
|
-
let frame;
|
|
981
|
-
try {
|
|
982
|
-
frame = JSON.parse(String(event.data));
|
|
983
|
-
} catch {
|
|
984
|
-
return;
|
|
985
|
-
}
|
|
986
|
-
this.handle(frame);
|
|
987
|
-
};
|
|
988
|
-
ws.onclose = (event) => {
|
|
989
|
-
if (this.ws !== ws) return;
|
|
990
|
-
this.onClosed(event.code);
|
|
991
|
-
};
|
|
992
|
-
ws.onerror = () => {};
|
|
993
|
-
}
|
|
994
|
-
url() {
|
|
995
|
-
if (this.internals.url) return this.internals.url;
|
|
996
|
-
const { protocol, host } = window.location;
|
|
997
|
-
return `${protocol === "https:" ? "wss:" : "ws:"}//${host}${this.options.path}`;
|
|
998
|
-
}
|
|
999
|
-
sendSub(entry) {
|
|
1000
|
-
this.setStatus(entry, "connecting", null);
|
|
1001
|
-
const frame = {
|
|
1002
|
-
op: "sub",
|
|
1003
|
-
id: entry.id,
|
|
1004
|
-
ch: entry.pattern
|
|
1005
|
-
};
|
|
1006
|
-
if (Object.keys(entry.params).length > 0) frame.p = entry.params;
|
|
1007
|
-
this.send(frame);
|
|
1008
|
-
}
|
|
1009
|
-
send(frame) {
|
|
1010
|
-
if (!this.isOpen()) return;
|
|
1011
|
-
try {
|
|
1012
|
-
this.ws.send(JSON.stringify(frame));
|
|
1013
|
-
} catch {}
|
|
1014
|
-
}
|
|
1015
|
-
handle(frame) {
|
|
1016
|
-
switch (frame.op) {
|
|
1017
|
-
case "hello":
|
|
1018
|
-
this.socketIdValue = frame.socketId;
|
|
1019
|
-
this.tag = frame.tag;
|
|
1020
|
-
this.attempt = 0;
|
|
1021
|
-
this.revoked = false;
|
|
1022
|
-
if (frame.heartbeatMs > 0) this.heartbeatMs = frame.heartbeatMs;
|
|
1023
|
-
this.startHeartbeat();
|
|
1024
|
-
return;
|
|
1025
|
-
case "subscribed": {
|
|
1026
|
-
const entry = this.byId.get(frame.id);
|
|
1027
|
-
if (!entry) return;
|
|
1028
|
-
this.dropTopic(entry);
|
|
1029
|
-
entry.topic = frame.t;
|
|
1030
|
-
let ids = this.topics.get(frame.t);
|
|
1031
|
-
if (!ids) this.topics.set(frame.t, ids = /* @__PURE__ */ new Set());
|
|
1032
|
-
ids.add(entry.id);
|
|
1033
|
-
this.clearRetry(entry);
|
|
1034
|
-
entry.retries = 0;
|
|
1035
|
-
this.setStatus(entry, "open", null);
|
|
1036
|
-
this.resync(entry);
|
|
1037
|
-
return;
|
|
1038
|
-
}
|
|
1039
|
-
case "denied": {
|
|
1040
|
-
const entry = this.byId.get(frame.id);
|
|
1041
|
-
if (!entry) return;
|
|
1042
|
-
this.dropTopic(entry);
|
|
1043
|
-
entry.permanent = isPermanentDenial(frame.code);
|
|
1044
|
-
this.setStatus(entry, "denied", frame.code);
|
|
1045
|
-
this.scheduleRetry(entry, frame.code);
|
|
1046
|
-
return;
|
|
1047
|
-
}
|
|
1048
|
-
case "ev": {
|
|
1049
|
-
if (frame.x !== void 0 && frame.x === this.tag) return;
|
|
1050
|
-
const ids = this.topics.get(frame.t);
|
|
1051
|
-
if (!ids) return;
|
|
1052
|
-
for (const id of Array.from(ids)) {
|
|
1053
|
-
const entry = this.byId.get(id);
|
|
1054
|
-
if (!entry) continue;
|
|
1055
|
-
for (const listener of Array.from(entry.listeners)) try {
|
|
1056
|
-
listener.onEvent?.(frame.ev, frame.d);
|
|
1057
|
-
} catch (error) {
|
|
1058
|
-
reportListenerError(error);
|
|
1059
|
-
}
|
|
1060
|
-
}
|
|
1061
|
-
return;
|
|
1062
|
-
}
|
|
1063
|
-
case "gap":
|
|
1064
|
-
for (const entry of this.entries.values()) if (entry.status === "open") this.resync(entry);
|
|
1065
|
-
return;
|
|
1066
|
-
case "bye":
|
|
1067
|
-
this.retryAfter = Math.max(0, Number(frame.retryAfter) || 0);
|
|
1068
|
-
return;
|
|
1069
|
-
case "pong": return;
|
|
1070
|
-
}
|
|
1071
|
-
}
|
|
1072
|
-
onClosed(code) {
|
|
1073
|
-
this.ws = null;
|
|
1074
|
-
this.socketIdValue = null;
|
|
1075
|
-
this.tag = null;
|
|
1076
|
-
this.stopHeartbeat();
|
|
1077
|
-
this.topics.clear();
|
|
1078
|
-
for (const entry of this.entries.values()) {
|
|
1079
|
-
entry.topic = null;
|
|
1080
|
-
this.clearRetry(entry);
|
|
1081
|
-
if (!entry.permanent) this.setStatus(entry, "closed", null);
|
|
1082
|
-
}
|
|
1083
|
-
if (code === CloseCode.Revoked) this.revoked = true;
|
|
1084
|
-
if (this.entries.size > 0 && !this.pausedWhileHidden) this.scheduleReconnect();
|
|
1085
|
-
}
|
|
1086
|
-
scheduleReconnect() {
|
|
1087
|
-
this.clearReconnect();
|
|
1088
|
-
let delay;
|
|
1089
|
-
if (this.retryAfter !== null) {
|
|
1090
|
-
delay = this.retryAfter;
|
|
1091
|
-
this.retryAfter = null;
|
|
1092
|
-
} else if (this.revoked) {
|
|
1093
|
-
this.revoked = false;
|
|
1094
|
-
delay = Math.floor(this.random() * 500);
|
|
1095
|
-
} else delay = backoffDelay(this.attempt, this.random());
|
|
1096
|
-
this.attempt++;
|
|
1097
|
-
this.reconnectTimer = setTimeout(() => {
|
|
1098
|
-
this.reconnectTimer = null;
|
|
1099
|
-
this.connect();
|
|
1100
|
-
}, delay);
|
|
1101
|
-
}
|
|
1102
|
-
clearReconnect() {
|
|
1103
|
-
if (this.reconnectTimer) clearTimeout(this.reconnectTimer);
|
|
1104
|
-
this.reconnectTimer = null;
|
|
1105
|
-
}
|
|
1106
|
-
random() {
|
|
1107
|
-
return (this.internals.random ?? Math.random)();
|
|
1108
|
-
}
|
|
1109
|
-
startHeartbeat() {
|
|
1110
|
-
this.stopHeartbeat();
|
|
1111
|
-
this.pingTimer = setInterval(() => this.send({ op: "ping" }), this.heartbeatMs);
|
|
1112
|
-
this.touch();
|
|
1113
|
-
}
|
|
1114
|
-
stopHeartbeat() {
|
|
1115
|
-
if (this.pingTimer) clearInterval(this.pingTimer);
|
|
1116
|
-
if (this.livenessTimer) clearTimeout(this.livenessTimer);
|
|
1117
|
-
this.pingTimer = null;
|
|
1118
|
-
this.livenessTimer = null;
|
|
1119
|
-
}
|
|
1120
|
-
/** Anything from the server proves the socket alive. */
|
|
1121
|
-
touch() {
|
|
1122
|
-
if (this.livenessTimer) clearTimeout(this.livenessTimer);
|
|
1123
|
-
const ws = this.ws;
|
|
1124
|
-
this.livenessTimer = setTimeout(() => {
|
|
1125
|
-
if (this.ws !== ws || !ws) return;
|
|
1126
|
-
try {
|
|
1127
|
-
ws.close();
|
|
1128
|
-
} catch {}
|
|
1129
|
-
this.onClosed(1006);
|
|
1130
|
-
}, this.heartbeatMs * 2);
|
|
1131
|
-
}
|
|
1132
|
-
setStatus(entry, status, code) {
|
|
1133
|
-
if (entry.status === status && entry.code === code) return;
|
|
1134
|
-
entry.status = status;
|
|
1135
|
-
entry.code = code;
|
|
1136
|
-
for (const listener of Array.from(entry.listeners)) try {
|
|
1137
|
-
listener.onStatus?.(status, code);
|
|
1138
|
-
} catch (error) {
|
|
1139
|
-
reportListenerError(error);
|
|
1140
|
-
}
|
|
1141
|
-
}
|
|
1142
|
-
/** At most one resync per subscription per 2 s; a later one is deferred, not dropped. */
|
|
1143
|
-
resync(entry) {
|
|
1144
|
-
if (entry.resyncTimer) return;
|
|
1145
|
-
const now = Date.now();
|
|
1146
|
-
const wait = entry.lastResyncAt + RESYNC_COALESCE_MS - now;
|
|
1147
|
-
const fire = () => {
|
|
1148
|
-
entry.resyncTimer = null;
|
|
1149
|
-
entry.lastResyncAt = Date.now();
|
|
1150
|
-
for (const listener of Array.from(entry.listeners)) try {
|
|
1151
|
-
listener.onResync?.();
|
|
1152
|
-
} catch (error) {
|
|
1153
|
-
reportListenerError(error);
|
|
1154
|
-
}
|
|
1155
|
-
};
|
|
1156
|
-
if (entry.lastResyncAt === 0 || wait <= 0) fire();
|
|
1157
|
-
else entry.resyncTimer = setTimeout(fire, wait);
|
|
1158
|
-
}
|
|
1159
|
-
/**
|
|
1160
|
-
* A transient denial (`rate_limited`, `error`, `limit`) is retried on the
|
|
1161
|
-
* same socket, with backoff: a healthy socket may never reconnect. `denied`
|
|
1162
|
-
* and `revoked` are the channel's answer for this session, so they wait for
|
|
1163
|
-
* the next connection (a sign-in reconnects), and a permanent one is never
|
|
1164
|
-
* resent. `limit` gives up after `MAX_LIMIT_RETRIES` on one socket.
|
|
1165
|
-
*/
|
|
1166
|
-
scheduleRetry(entry, code) {
|
|
1167
|
-
if (code !== "rate_limited" && code !== "error" && code !== "limit") return;
|
|
1168
|
-
if (code === "limit" && entry.retries >= 5) return;
|
|
1169
|
-
this.clearRetry(entry);
|
|
1170
|
-
const delay = Math.max(code === "rate_limited" ? RATE_LIMITED_RETRY_MS : MIN_BACKOFF_MS, backoffDelay(entry.retries, this.random()));
|
|
1171
|
-
entry.retries++;
|
|
1172
|
-
entry.retryTimer = setTimeout(() => {
|
|
1173
|
-
entry.retryTimer = null;
|
|
1174
|
-
if (this.entries.get(entry.key) !== entry || entry.status !== "denied") return;
|
|
1175
|
-
if (this.isOpen()) this.sendSub(entry);
|
|
1176
|
-
}, delay);
|
|
1177
|
-
}
|
|
1178
|
-
clearRetry(entry) {
|
|
1179
|
-
if (entry.retryTimer) clearTimeout(entry.retryTimer);
|
|
1180
|
-
entry.retryTimer = null;
|
|
1181
|
-
}
|
|
1182
|
-
dropTopic(entry) {
|
|
1183
|
-
if (!entry.topic) return;
|
|
1184
|
-
const ids = this.topics.get(entry.topic);
|
|
1185
|
-
ids?.delete(entry.id);
|
|
1186
|
-
if (ids && ids.size === 0) this.topics.delete(entry.topic);
|
|
1187
|
-
entry.topic = null;
|
|
1188
|
-
}
|
|
1189
|
-
scheduleIdleClose() {
|
|
1190
|
-
this.cancelIdleClose();
|
|
1191
|
-
this.idleTimer = setTimeout(() => {
|
|
1192
|
-
this.idleTimer = null;
|
|
1193
|
-
if (this.entries.size > 0) return;
|
|
1194
|
-
this.clearReconnect();
|
|
1195
|
-
this.close();
|
|
1196
|
-
}, IDLE_CLOSE_MS);
|
|
1197
|
-
}
|
|
1198
|
-
cancelIdleClose() {
|
|
1199
|
-
if (this.idleTimer) clearTimeout(this.idleTimer);
|
|
1200
|
-
this.idleTimer = null;
|
|
1201
|
-
}
|
|
1202
|
-
/** Closes the socket without reconnecting. */
|
|
1203
|
-
close() {
|
|
1204
|
-
const ws = this.ws;
|
|
1205
|
-
if (!ws) return;
|
|
1206
|
-
this.ws = null;
|
|
1207
|
-
try {
|
|
1208
|
-
ws.close(CloseCode.Normal);
|
|
1209
|
-
} catch {}
|
|
1210
|
-
this.onClosed(CloseCode.Normal);
|
|
1211
|
-
}
|
|
1212
|
-
listen() {
|
|
1213
|
-
if (this.listening || typeof window === "undefined") return;
|
|
1214
|
-
this.listening = true;
|
|
1215
|
-
window.addEventListener("online", this.onOnline);
|
|
1216
|
-
if (typeof document !== "undefined") document.addEventListener("visibilitychange", this.onVisibility);
|
|
1217
|
-
}
|
|
1218
|
-
onOnline = () => {
|
|
1219
|
-
if (this.entries.size === 0 || this.ws || this.pausedWhileHidden) return;
|
|
1220
|
-
this.attempt = 0;
|
|
1221
|
-
this.connect();
|
|
1222
|
-
};
|
|
1223
|
-
onVisibility = () => {
|
|
1224
|
-
if (document.visibilityState === "hidden") {
|
|
1225
|
-
if (this.hiddenTimer) return;
|
|
1226
|
-
this.hiddenTimer = setTimeout(() => {
|
|
1227
|
-
this.hiddenTimer = null;
|
|
1228
|
-
if (document.visibilityState !== "hidden") return;
|
|
1229
|
-
this.pausedWhileHidden = true;
|
|
1230
|
-
this.clearReconnect();
|
|
1231
|
-
this.close();
|
|
1232
|
-
}, this.options.hiddenDisconnectMs);
|
|
1233
|
-
return;
|
|
1234
|
-
}
|
|
1235
|
-
if (this.hiddenTimer) clearTimeout(this.hiddenTimer);
|
|
1236
|
-
this.hiddenTimer = null;
|
|
1237
|
-
if (this.pausedWhileHidden) {
|
|
1238
|
-
this.pausedWhileHidden = false;
|
|
1239
|
-
this.attempt = 0;
|
|
1240
|
-
this.connect();
|
|
1241
|
-
}
|
|
1242
|
-
};
|
|
1243
|
-
/**
|
|
1244
|
-
* Drops the socket and opens a new one at once, when anything is
|
|
1245
|
-
* subscribed. A socket keeps the session it was opened with (the upgrade's
|
|
1246
|
-
* cookies), so signing in or out in this tab reconnects it: every channel
|
|
1247
|
-
* is then authorized as the new session, and the acks resync.
|
|
1248
|
-
*/
|
|
1249
|
-
reconnect() {
|
|
1250
|
-
this.close();
|
|
1251
|
-
this.clearReconnect();
|
|
1252
|
-
this.attempt = 0;
|
|
1253
|
-
if (this.entries.size > 0 && !this.pausedWhileHidden) this.connect();
|
|
1254
|
-
}
|
|
1255
|
-
/** For tests: drops the socket, timers and listeners. */
|
|
1256
|
-
dispose() {
|
|
1257
|
-
this.clearReconnect();
|
|
1258
|
-
this.cancelIdleClose();
|
|
1259
|
-
if (this.hiddenTimer) clearTimeout(this.hiddenTimer);
|
|
1260
|
-
for (const entry of this.entries.values()) {
|
|
1261
|
-
if (entry.resyncTimer) clearTimeout(entry.resyncTimer);
|
|
1262
|
-
if (entry.lingerTimer) clearTimeout(entry.lingerTimer);
|
|
1263
|
-
this.clearRetry(entry);
|
|
1264
|
-
}
|
|
1265
|
-
this.entries.clear();
|
|
1266
|
-
this.byId.clear();
|
|
1267
|
-
this.close();
|
|
1268
|
-
if (this.listening && typeof window !== "undefined") {
|
|
1269
|
-
window.removeEventListener("online", this.onOnline);
|
|
1270
|
-
document?.removeEventListener("visibilitychange", this.onVisibility);
|
|
1271
|
-
this.listening = false;
|
|
1272
|
-
}
|
|
1273
|
-
}
|
|
1274
|
-
};
|
|
1275
|
-
/** Full jitter, 1 to 30 s: `max(1 s, random × min(30 s, 1 s × 2^attempt))`. */
|
|
1276
|
-
function backoffDelay(attempt, random) {
|
|
1277
|
-
const cap = Math.min(MAX_BACKOFF_MS, MIN_BACKOFF_MS * 2 ** Math.min(attempt, 10));
|
|
1278
|
-
return Math.max(MIN_BACKOFF_MS, Math.floor(random * cap));
|
|
1279
|
-
}
|
|
1280
|
-
function withDefaults(options) {
|
|
1281
|
-
return {
|
|
1282
|
-
path: options.path ?? "/__gemi/socket",
|
|
1283
|
-
hiddenDisconnectMs: options.hiddenDisconnectMs ?? DEFAULT_HIDDEN_DISCONNECT_MS
|
|
1284
|
-
};
|
|
1285
|
-
}
|
|
1286
|
-
function reportListenerError(error) {
|
|
1287
|
-
setTimeout(() => {
|
|
1288
|
-
throw error;
|
|
1289
|
-
});
|
|
1290
|
-
}
|
|
1291
761
|
var configured = {};
|
|
1292
762
|
var instance = null;
|
|
763
|
+
var loading = null;
|
|
1293
764
|
/**
|
|
1294
765
|
* Sets the socket options (`init({ realtime })` calls it). Takes effect on
|
|
1295
766
|
* the next connection.
|
|
@@ -1301,23 +772,83 @@ function configureRealtime(options = {}) {
|
|
|
1301
772
|
};
|
|
1302
773
|
instance?.configure(configured);
|
|
1303
774
|
}
|
|
1304
|
-
/** The tab's client
|
|
1305
|
-
function
|
|
1306
|
-
if (typeof window === "undefined") return null;
|
|
1307
|
-
instance ??= new RealtimeClient(configured);
|
|
775
|
+
/** The tab's client if it is already loaded, else `null`. Never loads it. */
|
|
776
|
+
function loadedRealtimeClient() {
|
|
1308
777
|
return instance;
|
|
1309
778
|
}
|
|
1310
779
|
/**
|
|
780
|
+
* What a lazy `import()` resolved to, or a thrown error when it is missing.
|
|
781
|
+
* Vite's preload helper resolves the `import()` to `undefined` instead of
|
|
782
|
+
* rejecting when a `vite:preloadError` listener handled the failure (`init`'s
|
|
783
|
+
* chunk-load recovery does, as it starts a reload): treated as a failed load.
|
|
784
|
+
*/
|
|
785
|
+
function loadedModule(module) {
|
|
786
|
+
if (!module) throw new Error("[gemi] A lazily loaded chunk is unavailable.");
|
|
787
|
+
return module;
|
|
788
|
+
}
|
|
789
|
+
/** The lazily loaded modules. Replaceable in tests, to simulate a failed load. */
|
|
790
|
+
var lazyModules = {
|
|
791
|
+
client: () => import("./RealtimeClient-CqGve6h8.js"),
|
|
792
|
+
live: () => import("./live-DIUDdLrA.js").then((n) => n.t)
|
|
793
|
+
};
|
|
794
|
+
/**
|
|
795
|
+
* The tab's client, loading its module and creating it on first use. `null`
|
|
796
|
+
* on the server. A failed load (a chunk gone after a deploy, the network) is
|
|
797
|
+
* not kept, so a later subscription calls `import()` again; whether that
|
|
798
|
+
* fetches anew is up to the browser, and the usual recovery is `init`'s
|
|
799
|
+
* reload.
|
|
800
|
+
*/
|
|
801
|
+
function loadRealtimeClient() {
|
|
802
|
+
if (typeof window === "undefined") return null;
|
|
803
|
+
if (instance) return Promise.resolve(instance);
|
|
804
|
+
loading ??= lazyModules.client().then((module) => instance ??= new (loadedModule(module)).RealtimeClient(configured)).catch((error) => {
|
|
805
|
+
loading = null;
|
|
806
|
+
throw error;
|
|
807
|
+
});
|
|
808
|
+
return loading;
|
|
809
|
+
}
|
|
810
|
+
var liveModule = null;
|
|
811
|
+
/**
|
|
812
|
+
* Makes `./live` available without loading it: `<Page>` in `gemi/testing`
|
|
813
|
+
* passes it, so a live query on a provided socket subscribes during its
|
|
814
|
+
* effect, as it did before the module was split out.
|
|
815
|
+
*/
|
|
816
|
+
function provideLiveModule(module) {
|
|
817
|
+
liveModule = module;
|
|
818
|
+
}
|
|
819
|
+
/**
|
|
820
|
+
* Follows `useQuery`'s `live` channel until the returned function is called.
|
|
821
|
+
* `./live` and the socket client are loaded on first use, so `useQuery`
|
|
822
|
+
* carries only this; once both are loaded later queries subscribe at once.
|
|
823
|
+
* `setOpen` reports `false` until the channel is open.
|
|
824
|
+
*/
|
|
825
|
+
function followLive(provided, live, resource, setOpen) {
|
|
826
|
+
let cancelled = false;
|
|
827
|
+
let stop = null;
|
|
828
|
+
const start = (module, socket) => {
|
|
829
|
+
liveModule = module;
|
|
830
|
+
if (!cancelled && socket) stop = module.subscribeLive(socket, live, resource, setOpen);
|
|
831
|
+
};
|
|
832
|
+
const socket = provided ?? instance;
|
|
833
|
+
if (liveModule && socket) start(liveModule, socket);
|
|
834
|
+
else Promise.all([liveModule ?? lazyModules.live(), socket ?? loadRealtimeClient()]).then(([module, socket]) => start(loadedModule(module), socket)).catch(() => {});
|
|
835
|
+
return () => {
|
|
836
|
+
cancelled = true;
|
|
837
|
+
stop?.();
|
|
838
|
+
setOpen(false);
|
|
839
|
+
};
|
|
840
|
+
}
|
|
841
|
+
/**
|
|
1311
842
|
* Reconnects the tab's socket, if it has one, so its channels are authorized
|
|
1312
843
|
* as the current session. The auth hooks call it after signing in or out.
|
|
1313
|
-
* Never creates the client.
|
|
844
|
+
* Never loads or creates the client.
|
|
1314
845
|
*/
|
|
1315
846
|
function reconnectRealtime() {
|
|
1316
847
|
instance?.reconnect();
|
|
1317
848
|
}
|
|
1318
849
|
/**
|
|
1319
850
|
* The open socket's id, for the `X-Gemi-Socket` header, or `null`. Never
|
|
1320
|
-
* creates the client.
|
|
851
|
+
* loads or creates the client.
|
|
1321
852
|
*/
|
|
1322
853
|
function currentSocketId() {
|
|
1323
854
|
return instance?.socketId ?? null;
|
|
@@ -3887,6 +3418,91 @@ var ClientRouterProvider = (props) => {
|
|
|
3887
3418
|
});
|
|
3888
3419
|
};
|
|
3889
3420
|
//#endregion
|
|
3421
|
+
//#region client/realtime/useChannelSubscription.ts
|
|
3422
|
+
var CLOSED = {
|
|
3423
|
+
status: "closed",
|
|
3424
|
+
code: null
|
|
3425
|
+
};
|
|
3426
|
+
var IDLE = {
|
|
3427
|
+
status: "idle",
|
|
3428
|
+
code: null
|
|
3429
|
+
};
|
|
3430
|
+
/**
|
|
3431
|
+
* The one subscription primitive the channel hooks share. Subscribes only in
|
|
3432
|
+
* an effect, so a server render, a static page or an island that never
|
|
3433
|
+
* mounts opens no socket; on the server it reports `closed`. Handlers may
|
|
3434
|
+
* change between renders without resubscribing.
|
|
3435
|
+
*/
|
|
3436
|
+
function useChannelSubscription(pattern, params, handlers, enabled) {
|
|
3437
|
+
const active = enabled && pattern !== null;
|
|
3438
|
+
const [state, setState] = useState(active ? CLOSED : IDLE);
|
|
3439
|
+
const provided = useContext(RealtimeContext);
|
|
3440
|
+
const handlersRef = useRef(handlers);
|
|
3441
|
+
handlersRef.current = handlers;
|
|
3442
|
+
const paramsRef = useRef(params);
|
|
3443
|
+
paramsRef.current = params;
|
|
3444
|
+
useEffect(() => {
|
|
3445
|
+
if (!active || pattern === null) {
|
|
3446
|
+
setState(IDLE);
|
|
3447
|
+
return;
|
|
3448
|
+
}
|
|
3449
|
+
let release = null;
|
|
3450
|
+
let cancelled = false;
|
|
3451
|
+
const subscribe = (socket) => {
|
|
3452
|
+
if (cancelled) return;
|
|
3453
|
+
release = socket.subscribe(pattern, paramsRef.current, {
|
|
3454
|
+
onEvent: (event, data) => handlersRef.current.onEvent?.(event, data),
|
|
3455
|
+
onResync: () => handlersRef.current.onResync?.(),
|
|
3456
|
+
onStatus: (status, code) => setState((previous) => previous.status === status && previous.code === code ? previous : {
|
|
3457
|
+
status,
|
|
3458
|
+
code
|
|
3459
|
+
})
|
|
3460
|
+
});
|
|
3461
|
+
};
|
|
3462
|
+
const socket = provided ?? loadedRealtimeClient();
|
|
3463
|
+
if (socket) subscribe(socket);
|
|
3464
|
+
else {
|
|
3465
|
+
const pending = loadRealtimeClient();
|
|
3466
|
+
if (!pending) return;
|
|
3467
|
+
pending.then(subscribe, () => {});
|
|
3468
|
+
}
|
|
3469
|
+
return () => {
|
|
3470
|
+
cancelled = true;
|
|
3471
|
+
release?.();
|
|
3472
|
+
setState(CLOSED);
|
|
3473
|
+
};
|
|
3474
|
+
}, [
|
|
3475
|
+
provided,
|
|
3476
|
+
pattern === null ? null : channelKey(pattern, params),
|
|
3477
|
+
active
|
|
3478
|
+
]);
|
|
3479
|
+
if (typeof window === "undefined") return active ? CLOSED : IDLE;
|
|
3480
|
+
return state;
|
|
3481
|
+
}
|
|
3482
|
+
/**
|
|
3483
|
+
* `fn`, run at most once per `ms`: the first call starts the window, and the
|
|
3484
|
+
* calls inside it fold into the one run at its end. Stable across renders;
|
|
3485
|
+
* a pending run is dropped on unmount.
|
|
3486
|
+
*/
|
|
3487
|
+
function useCoalesced(fn, ms = 150) {
|
|
3488
|
+
const fnRef = useRef(fn);
|
|
3489
|
+
fnRef.current = fn;
|
|
3490
|
+
const timer = useRef(null);
|
|
3491
|
+
useEffect(() => () => {
|
|
3492
|
+
if (timer.current) clearTimeout(timer.current);
|
|
3493
|
+
timer.current = null;
|
|
3494
|
+
}, []);
|
|
3495
|
+
const coalesced = useRef(null);
|
|
3496
|
+
coalesced.current ??= () => {
|
|
3497
|
+
if (timer.current) return;
|
|
3498
|
+
timer.current = setTimeout(() => {
|
|
3499
|
+
timer.current = null;
|
|
3500
|
+
fnRef.current();
|
|
3501
|
+
}, ms);
|
|
3502
|
+
};
|
|
3503
|
+
return coalesced.current;
|
|
3504
|
+
}
|
|
3505
|
+
//#endregion
|
|
3890
3506
|
//#region client/RouteTransitionProvider.tsx
|
|
3891
3507
|
var RouteTransitionContext = createContext({
|
|
3892
3508
|
isTransitioning: false,
|
|
@@ -4013,6 +3629,6 @@ function useTheme() {
|
|
|
4013
3629
|
return context;
|
|
4014
3630
|
}
|
|
4015
3631
|
//#endregion
|
|
4016
|
-
export {
|
|
3632
|
+
export { DEFAULT_STALE_TIME as $, ServerDataContext as A, configureRealtime as B, readRoutePayload as C, useNavigate as D, useSearchParams as E, ProgressManager as F, reconnectRealtime as G, followLive as H, Action as I, QueryConfigContext as J, toVariantKey as K, createMemoryHistory as L, useLocation as M, domainUrl as N, I18nContext as O, isAbsoluteUrl as P, pickDefined as Q, RealtimeContext as R, setActiveLocale as S, useRoute as T, loadedModule as U, currentSocketId as V, provideLiveModule as W, QueryManagerProvider as X, QueryManagerContext as Y, USER_QUERY_CONFIG_KEYS as Z, isChunkLoadError as _, useRouteTransition as a, Subject as at, loadDictionaryForRender as b, ClientRouterContext as c, ComponentsProvider as d, isRetryableQueryError as et, initialViewModulesReady as f, configureChunkLoadRecovery as g, CHUNK_RELOAD_MARKER as h, RouteTransitionProvider as i, QueryError as it, ServerDataProvider as j, I18nProvider as k, ClientRouterProvider as l, subscribeViewModules as m, useTheme as n, retryDelayFor as nt, useChannelSubscription as o, loadViewModule as p, applyParams as q, m as r, shouldRetryQuery as rt, useCoalesced as s, ThemeProvider as t, parseRetryAfter as tt, ComponentsContext as u, recoverFromChunkLoadError as v, routeDataUrl as w, registerDictionary as x, loadDictionary as y, channelKey as z };
|
|
4017
3633
|
|
|
4018
|
-
//# sourceMappingURL=ThemeProvider-
|
|
3634
|
+
//# sourceMappingURL=ThemeProvider-DaX0eyqs.js.map
|