@abraca/dabra 2.95.0 → 2.96.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.
|
@@ -1098,6 +1098,128 @@ const Kind = {
|
|
|
1098
1098
|
*/
|
|
1099
1099
|
const SERVER_ROOT_ID = "00000000-0000-0000-0000-000000000000";
|
|
1100
1100
|
|
|
1101
|
+
//#endregion
|
|
1102
|
+
//#region packages/provider/src/LifecycleGate.ts
|
|
1103
|
+
/** How long the app must stay hidden before we act on it. */
|
|
1104
|
+
const HIDE_GRACE_MS = 1e4;
|
|
1105
|
+
/** The browser's own visibility, or a permanently-visible stub off-DOM. */
|
|
1106
|
+
function defaultSource() {
|
|
1107
|
+
const doc = globalThis.document;
|
|
1108
|
+
if (!doc || typeof doc.addEventListener !== "function") return {
|
|
1109
|
+
get: () => "visible",
|
|
1110
|
+
subscribe: () => () => {}
|
|
1111
|
+
};
|
|
1112
|
+
return {
|
|
1113
|
+
get: () => doc.visibilityState === "hidden" ? "hidden" : "visible",
|
|
1114
|
+
subscribe: (onChange) => {
|
|
1115
|
+
doc.addEventListener("visibilitychange", onChange);
|
|
1116
|
+
return () => doc.removeEventListener("visibilitychange", onChange);
|
|
1117
|
+
}
|
|
1118
|
+
};
|
|
1119
|
+
}
|
|
1120
|
+
var LifecycleGate = class {
|
|
1121
|
+
constructor(source = defaultSource(), graceMs = HIDE_GRACE_MS) {
|
|
1122
|
+
this.graceMs = graceMs;
|
|
1123
|
+
this.unsubscribe = null;
|
|
1124
|
+
this.hideTimer = null;
|
|
1125
|
+
this._backgrounded = false;
|
|
1126
|
+
this.listeners = /* @__PURE__ */ new Set();
|
|
1127
|
+
this.source = source;
|
|
1128
|
+
this.unsubscribe = this.source.subscribe(() => this.onVisibilityChange());
|
|
1129
|
+
}
|
|
1130
|
+
/**
|
|
1131
|
+
* True when the app has been hidden for longer than the grace period.
|
|
1132
|
+
*
|
|
1133
|
+
* Deliberately NOT the same as `document.hidden`: a tab that was hidden two
|
|
1134
|
+
* seconds ago is still, for our purposes, in use.
|
|
1135
|
+
*/
|
|
1136
|
+
get backgrounded() {
|
|
1137
|
+
return this._backgrounded;
|
|
1138
|
+
}
|
|
1139
|
+
/** Subscribe to background/foreground transitions. Returns an unsubscribe fn. */
|
|
1140
|
+
onChange(listener) {
|
|
1141
|
+
this.listeners.add(listener);
|
|
1142
|
+
return () => this.listeners.delete(listener);
|
|
1143
|
+
}
|
|
1144
|
+
/** Swap the visibility source. Used by tests to drive state deterministically. */
|
|
1145
|
+
setSource(source) {
|
|
1146
|
+
this.unsubscribe?.();
|
|
1147
|
+
this.source = source;
|
|
1148
|
+
this.unsubscribe = this.source.subscribe(() => this.onVisibilityChange());
|
|
1149
|
+
this.onVisibilityChange();
|
|
1150
|
+
}
|
|
1151
|
+
onVisibilityChange() {
|
|
1152
|
+
if (this.source.get() === "hidden") {
|
|
1153
|
+
if (this._backgrounded || this.hideTimer) return;
|
|
1154
|
+
this.hideTimer = setTimeout(() => {
|
|
1155
|
+
this.hideTimer = null;
|
|
1156
|
+
this.setBackgrounded(true);
|
|
1157
|
+
}, this.graceMs);
|
|
1158
|
+
return;
|
|
1159
|
+
}
|
|
1160
|
+
if (this.hideTimer) {
|
|
1161
|
+
clearTimeout(this.hideTimer);
|
|
1162
|
+
this.hideTimer = null;
|
|
1163
|
+
}
|
|
1164
|
+
this.setBackgrounded(false);
|
|
1165
|
+
}
|
|
1166
|
+
setBackgrounded(next) {
|
|
1167
|
+
if (this._backgrounded === next) return;
|
|
1168
|
+
this._backgrounded = next;
|
|
1169
|
+
for (const listener of [...this.listeners]) try {
|
|
1170
|
+
listener(next);
|
|
1171
|
+
} catch {}
|
|
1172
|
+
}
|
|
1173
|
+
destroy() {
|
|
1174
|
+
this.unsubscribe?.();
|
|
1175
|
+
this.unsubscribe = null;
|
|
1176
|
+
if (this.hideTimer) {
|
|
1177
|
+
clearTimeout(this.hideTimer);
|
|
1178
|
+
this.hideTimer = null;
|
|
1179
|
+
}
|
|
1180
|
+
this.listeners.clear();
|
|
1181
|
+
}
|
|
1182
|
+
};
|
|
1183
|
+
/**
|
|
1184
|
+
* The process-wide gate.
|
|
1185
|
+
*
|
|
1186
|
+
* Visibility is a property of the tab, not of any one provider or socket, so a
|
|
1187
|
+
* single shared instance is both correct and avoids paying for one
|
|
1188
|
+
* `visibilitychange` listener per document.
|
|
1189
|
+
*/
|
|
1190
|
+
let shared = null;
|
|
1191
|
+
function getLifecycleGate() {
|
|
1192
|
+
if (!shared) shared = new LifecycleGate();
|
|
1193
|
+
return shared;
|
|
1194
|
+
}
|
|
1195
|
+
/**
|
|
1196
|
+
* Replace the shared gate. Tests use this to install a controllable source;
|
|
1197
|
+
* passing `null` restores the default browser-backed gate on next access.
|
|
1198
|
+
*/
|
|
1199
|
+
function setLifecycleGate(gate) {
|
|
1200
|
+
if (shared && shared !== gate) shared.destroy();
|
|
1201
|
+
shared = gate;
|
|
1202
|
+
}
|
|
1203
|
+
/** A visibility source a test (or an embedder) can drive by hand. */
|
|
1204
|
+
var ManualVisibilitySource = class {
|
|
1205
|
+
constructor() {
|
|
1206
|
+
this.state = "visible";
|
|
1207
|
+
this.listeners = /* @__PURE__ */ new Set();
|
|
1208
|
+
}
|
|
1209
|
+
get() {
|
|
1210
|
+
return this.state;
|
|
1211
|
+
}
|
|
1212
|
+
subscribe(onChange) {
|
|
1213
|
+
this.listeners.add(onChange);
|
|
1214
|
+
return () => this.listeners.delete(onChange);
|
|
1215
|
+
}
|
|
1216
|
+
set(state) {
|
|
1217
|
+
if (this.state === state) return;
|
|
1218
|
+
this.state = state;
|
|
1219
|
+
for (const listener of [...this.listeners]) listener();
|
|
1220
|
+
}
|
|
1221
|
+
};
|
|
1222
|
+
|
|
1101
1223
|
//#endregion
|
|
1102
1224
|
//#region packages/provider/src/IncomingMessage.ts
|
|
1103
1225
|
var IncomingMessage = class {
|
|
@@ -1205,6 +1327,7 @@ var AbracadabraWS = class extends EventEmitter {
|
|
|
1205
1327
|
this.connectionAttempt = null;
|
|
1206
1328
|
this.onlineListener = null;
|
|
1207
1329
|
this.offlineListener = null;
|
|
1330
|
+
this.unsubscribeLifecycle = null;
|
|
1208
1331
|
this.receivedOnOpenPayload = void 0;
|
|
1209
1332
|
this.closeTries = 0;
|
|
1210
1333
|
this.lastRefreshToken = null;
|
|
@@ -1224,6 +1347,9 @@ var AbracadabraWS = class extends EventEmitter {
|
|
|
1224
1347
|
this.on("close", this.onClose.bind(this));
|
|
1225
1348
|
this.on("message", this.onMessage.bind(this));
|
|
1226
1349
|
this.intervals.connectionChecker = setInterval(this.checkConnection.bind(this), this.configuration.messageReconnectTimeout / 10);
|
|
1350
|
+
this.unsubscribeLifecycle = getLifecycleGate().onChange((backgrounded) => {
|
|
1351
|
+
if (!backgrounded) this.checkConnection();
|
|
1352
|
+
});
|
|
1227
1353
|
if (typeof window !== "undefined" && typeof window.addEventListener === "function") {
|
|
1228
1354
|
this.onlineListener = this.handleOnline.bind(this);
|
|
1229
1355
|
this.offlineListener = this.handleOffline.bind(this);
|
|
@@ -1407,6 +1533,7 @@ var AbracadabraWS = class extends EventEmitter {
|
|
|
1407
1533
|
this.connectionAttempt = null;
|
|
1408
1534
|
}
|
|
1409
1535
|
checkConnection() {
|
|
1536
|
+
if (getLifecycleGate().backgrounded) return;
|
|
1410
1537
|
if (this.status !== WebSocketStatus.Connected) return;
|
|
1411
1538
|
if (!this.lastMessageReceived) return;
|
|
1412
1539
|
if (this.configuration.messageReconnectTimeout >= getUnixTime() - this.lastMessageReceived) return;
|
|
@@ -1532,6 +1659,8 @@ var AbracadabraWS = class extends EventEmitter {
|
|
|
1532
1659
|
}
|
|
1533
1660
|
this.onlineListener = null;
|
|
1534
1661
|
this.offlineListener = null;
|
|
1662
|
+
this.unsubscribeLifecycle?.();
|
|
1663
|
+
this.unsubscribeLifecycle = null;
|
|
1535
1664
|
clearInterval(this.intervals.connectionChecker);
|
|
1536
1665
|
this.stopConnectionAttempt();
|
|
1537
1666
|
this.disconnect();
|
|
@@ -1758,19 +1887,21 @@ var MessageReceiver = class {
|
|
|
1758
1887
|
provider.reportWriteRejected("server did not apply the update (permission or validation)", "validation");
|
|
1759
1888
|
}
|
|
1760
1889
|
applyAwarenessMessage(provider) {
|
|
1761
|
-
|
|
1890
|
+
const awareness = provider.peekAwareness();
|
|
1891
|
+
if (!awareness) return;
|
|
1762
1892
|
const { message } = this;
|
|
1763
|
-
applyAwarenessUpdate(
|
|
1893
|
+
applyAwarenessUpdate(awareness, message.readVarUint8Array(), provider);
|
|
1764
1894
|
}
|
|
1765
1895
|
applyAuthMessage(provider) {
|
|
1766
1896
|
const { message } = this;
|
|
1767
1897
|
readAuthMessage(message.decoder, provider.sendToken.bind(provider), provider.permissionDeniedHandler.bind(provider), provider.authenticatedHandler.bind(provider));
|
|
1768
1898
|
}
|
|
1769
1899
|
applyQueryAwarenessMessage(provider) {
|
|
1770
|
-
|
|
1900
|
+
const awareness = provider.peekAwareness();
|
|
1901
|
+
if (!awareness) return;
|
|
1771
1902
|
const { message } = this;
|
|
1772
1903
|
message.writeVarUint(MessageType.Awareness);
|
|
1773
|
-
message.writeVarUint8Array(encodeAwarenessUpdate(
|
|
1904
|
+
message.writeVarUint8Array(encodeAwarenessUpdate(awareness, Array.from(awareness.getStates().keys())));
|
|
1774
1905
|
}
|
|
1775
1906
|
};
|
|
1776
1907
|
|
|
@@ -2538,12 +2669,20 @@ var AbracadabraBaseProvider = class AbracadabraBaseProvider extends EventEmitter
|
|
|
2538
2669
|
this.forwardDisconnect = (e) => this.emit("disconnect", e);
|
|
2539
2670
|
this.forwardDestroy = () => this.emit("destroy");
|
|
2540
2671
|
this.forwardRateLimited = () => this.emit("rateLimited");
|
|
2672
|
+
this._awarenessDeferred = false;
|
|
2673
|
+
this._awarenessExplicitlyNull = false;
|
|
2541
2674
|
this.lastWriteRejection = null;
|
|
2542
2675
|
this.writeRejectionCount = 0;
|
|
2543
2676
|
this.setConfiguration(configuration);
|
|
2544
2677
|
this.configuration.document = configuration.document ? configuration.document : new Y.Doc();
|
|
2545
|
-
|
|
2546
|
-
|
|
2678
|
+
if (configuration.awareness !== void 0) {
|
|
2679
|
+
this.configuration.awareness = configuration.awareness;
|
|
2680
|
+
this._awarenessExplicitlyNull = configuration.awareness === null;
|
|
2681
|
+
} else if (configuration.lazyAwareness) {
|
|
2682
|
+
this.configuration.awareness = null;
|
|
2683
|
+
this._awarenessDeferred = true;
|
|
2684
|
+
} else this.configuration.awareness = new Awareness(this.document);
|
|
2685
|
+
if (!this._awarenessDeferred) this._initAwareness();
|
|
2547
2686
|
this.on("open", this.configuration.onOpen);
|
|
2548
2687
|
this.on("reconnected", this.configuration.onReconnected);
|
|
2549
2688
|
this.on("message", this.configuration.onMessage);
|
|
@@ -2559,14 +2698,7 @@ var AbracadabraBaseProvider = class AbracadabraBaseProvider extends EventEmitter
|
|
|
2559
2698
|
this.on("authenticationFailed", this.configuration.onAuthenticationFailed);
|
|
2560
2699
|
this.on("writeRejected", this.configuration.onWriteRejected);
|
|
2561
2700
|
this.on("rateLimited", this.configuration.onRateLimited);
|
|
2562
|
-
this.awareness?.on("update", () => {
|
|
2563
|
-
this.emit("awarenessUpdate", { states: awarenessStatesToArray(this.awareness.getStates()) });
|
|
2564
|
-
});
|
|
2565
|
-
this.awareness?.on("change", () => {
|
|
2566
|
-
this.emit("awarenessChange", { states: awarenessStatesToArray(this.awareness.getStates()) });
|
|
2567
|
-
});
|
|
2568
2701
|
this.document.on("update", this.boundDocumentUpdateHandler);
|
|
2569
|
-
this.awareness?.on("update", this.boundAwarenessUpdateHandler);
|
|
2570
2702
|
this.registerEventListeners();
|
|
2571
2703
|
if (this.configuration.forceSyncInterval && typeof this.configuration.forceSyncInterval === "number") this.intervals.forceSync = setInterval(this.forceSync.bind(this), this.configuration.forceSyncInterval);
|
|
2572
2704
|
if (this.manageSocket) this.attach();
|
|
@@ -2587,9 +2719,68 @@ var AbracadabraBaseProvider = class AbracadabraBaseProvider extends EventEmitter
|
|
|
2587
2719
|
get isAttached() {
|
|
2588
2720
|
return this._isAttached;
|
|
2589
2721
|
}
|
|
2722
|
+
/**
|
|
2723
|
+
* The presence instance for this document, built on demand.
|
|
2724
|
+
*
|
|
2725
|
+
* Reading this materializes a deferred instance (see `lazyAwareness`), which
|
|
2726
|
+
* is why every consumer — TipTap's CollaborationCaret, presence UI, app code
|
|
2727
|
+
* — keeps working unchanged: touching `.awareness` is itself the signal that
|
|
2728
|
+
* this document needs presence.
|
|
2729
|
+
*
|
|
2730
|
+
* Provider INTERNALS must use `peekAwareness()` instead, or the lazy path
|
|
2731
|
+
* materializes everything it was meant to avoid.
|
|
2732
|
+
*/
|
|
2590
2733
|
get awareness() {
|
|
2734
|
+
if (this._awarenessExplicitlyNull) return null;
|
|
2735
|
+
if (this._awarenessDeferred) this._materializeAwareness();
|
|
2591
2736
|
return this.configuration.awareness;
|
|
2592
2737
|
}
|
|
2738
|
+
/**
|
|
2739
|
+
* The Awareness instance if one exists, without ever creating one.
|
|
2740
|
+
*
|
|
2741
|
+
* @internal Used by provider internals and `MessageReceiver`. A deferred
|
|
2742
|
+
* document has no local presence, so there is nothing for an inbound
|
|
2743
|
+
* awareness frame to be applied to and nothing to answer a peer's query with
|
|
2744
|
+
* — and materializing on a peer's frame would hand every idle background
|
|
2745
|
+
* document a heartbeat as soon as anyone else joined the space.
|
|
2746
|
+
*/
|
|
2747
|
+
peekAwareness() {
|
|
2748
|
+
return this._awarenessDeferred ? null : this.configuration.awareness;
|
|
2749
|
+
}
|
|
2750
|
+
/**
|
|
2751
|
+
* Build the deferred Awareness instance and bring it fully up to date:
|
|
2752
|
+
* seeded, wired, published, and caught up with the peers already present.
|
|
2753
|
+
*/
|
|
2754
|
+
_materializeAwareness() {
|
|
2755
|
+
if (!this._awarenessDeferred) return;
|
|
2756
|
+
this._awarenessDeferred = false;
|
|
2757
|
+
this.configuration.awareness = new Awareness(this.document);
|
|
2758
|
+
this._initAwareness();
|
|
2759
|
+
if (this._isAttached) {
|
|
2760
|
+
this.send(AwarenessMessage, {
|
|
2761
|
+
awareness: this.configuration.awareness,
|
|
2762
|
+
clients: [this.document.clientID],
|
|
2763
|
+
documentName: this.configuration.name
|
|
2764
|
+
});
|
|
2765
|
+
this.send(QueryAwarenessMessage, { documentName: this.configuration.name });
|
|
2766
|
+
}
|
|
2767
|
+
}
|
|
2768
|
+
/**
|
|
2769
|
+
* Seed and wire an Awareness instance. Shared by the eager constructor path
|
|
2770
|
+
* and the lazy materialization path so the two cannot drift.
|
|
2771
|
+
*/
|
|
2772
|
+
_initAwareness() {
|
|
2773
|
+
const awareness = this.configuration.awareness;
|
|
2774
|
+
if (!awareness) return;
|
|
2775
|
+
if (awareness.getLocalState() === null) awareness.setLocalState({});
|
|
2776
|
+
awareness.on("update", () => {
|
|
2777
|
+
this.emit("awarenessUpdate", { states: awarenessStatesToArray(awareness.getStates()) });
|
|
2778
|
+
});
|
|
2779
|
+
awareness.on("change", () => {
|
|
2780
|
+
this.emit("awarenessChange", { states: awarenessStatesToArray(awareness.getStates()) });
|
|
2781
|
+
});
|
|
2782
|
+
awareness.on("update", this.boundAwarenessUpdateHandler);
|
|
2783
|
+
}
|
|
2593
2784
|
get hasUnsyncedChanges() {
|
|
2594
2785
|
return this.unsyncedChanges > 0;
|
|
2595
2786
|
}
|
|
@@ -2614,7 +2805,8 @@ var AbracadabraBaseProvider = class AbracadabraBaseProvider extends EventEmitter
|
|
|
2614
2805
|
});
|
|
2615
2806
|
}
|
|
2616
2807
|
pageHide() {
|
|
2617
|
-
|
|
2808
|
+
const awareness = this.peekAwareness();
|
|
2809
|
+
if (awareness) removeAwarenessStates(awareness, [this.document.clientID], "page hide");
|
|
2618
2810
|
}
|
|
2619
2811
|
registerEventListeners() {
|
|
2620
2812
|
if (typeof window === "undefined" || !("addEventListener" in window)) return;
|
|
@@ -2652,7 +2844,7 @@ var AbracadabraBaseProvider = class AbracadabraBaseProvider extends EventEmitter
|
|
|
2652
2844
|
if (!this.isAuthenticated) return;
|
|
2653
2845
|
const changedClients = added.concat(updated).concat(removed);
|
|
2654
2846
|
this.send(AwarenessMessage, {
|
|
2655
|
-
awareness: this.
|
|
2847
|
+
awareness: this.peekAwareness(),
|
|
2656
2848
|
clients: changedClients,
|
|
2657
2849
|
documentName: this.configuration.name
|
|
2658
2850
|
});
|
|
@@ -2746,12 +2938,13 @@ var AbracadabraBaseProvider = class AbracadabraBaseProvider extends EventEmitter
|
|
|
2746
2938
|
document: this.document,
|
|
2747
2939
|
documentName: this.configuration.name
|
|
2748
2940
|
});
|
|
2749
|
-
|
|
2750
|
-
|
|
2941
|
+
const openAwareness = this.peekAwareness();
|
|
2942
|
+
if (openAwareness && openAwareness.getLocalState() !== null) this.send(AwarenessMessage, {
|
|
2943
|
+
awareness: openAwareness,
|
|
2751
2944
|
clients: [this.document.clientID],
|
|
2752
2945
|
documentName: this.configuration.name
|
|
2753
2946
|
});
|
|
2754
|
-
if (
|
|
2947
|
+
if (openAwareness) this.send(QueryAwarenessMessage, { documentName: this.configuration.name });
|
|
2755
2948
|
}
|
|
2756
2949
|
send(message, args) {
|
|
2757
2950
|
if (!this._isAttached) return;
|
|
@@ -2774,16 +2967,18 @@ var AbracadabraBaseProvider = class AbracadabraBaseProvider extends EventEmitter
|
|
|
2774
2967
|
this.synced = false;
|
|
2775
2968
|
this._hasEverAuthenticated = false;
|
|
2776
2969
|
this.clearResubscribe();
|
|
2777
|
-
|
|
2970
|
+
const closeAwareness = this.peekAwareness();
|
|
2971
|
+
if (closeAwareness) removeAwarenessStates(closeAwareness, Array.from(closeAwareness.getStates().keys()).filter((client) => client !== this.document.clientID), this);
|
|
2778
2972
|
}
|
|
2779
2973
|
destroy() {
|
|
2780
2974
|
this.emit("destroy");
|
|
2781
2975
|
if (this._rpc) this._rpc.destroy();
|
|
2782
2976
|
if (this.intervals.forceSync) clearInterval(this.intervals.forceSync);
|
|
2783
|
-
|
|
2784
|
-
|
|
2785
|
-
this.
|
|
2786
|
-
this.
|
|
2977
|
+
const ownAwareness = this.peekAwareness();
|
|
2978
|
+
if (ownAwareness) {
|
|
2979
|
+
removeAwarenessStates(ownAwareness, [this.document.clientID], "provider destroy");
|
|
2980
|
+
ownAwareness.off("update", this.boundAwarenessUpdateHandler);
|
|
2981
|
+
ownAwareness.destroy();
|
|
2787
2982
|
}
|
|
2788
2983
|
this.document.off("update", this.boundDocumentUpdateHandler);
|
|
2789
2984
|
this.removeAllListeners();
|
|
@@ -2929,6 +3124,34 @@ function txPromise$2(store, request) {
|
|
|
2929
3124
|
request.onerror = () => reject(request.error);
|
|
2930
3125
|
});
|
|
2931
3126
|
}
|
|
3127
|
+
/**
|
|
3128
|
+
* Resolve when the whole transaction commits.
|
|
3129
|
+
*
|
|
3130
|
+
* `txPromise` waits on a single request, which is the wrong unit once a
|
|
3131
|
+
* transaction carries a batch: the last `add` succeeding does not mean the
|
|
3132
|
+
* transaction committed, and reporting durability before commit is exactly the
|
|
3133
|
+
* lie the offline store must not tell.
|
|
3134
|
+
*/
|
|
3135
|
+
function txComplete(tx) {
|
|
3136
|
+
return new Promise((resolve, reject) => {
|
|
3137
|
+
tx.oncomplete = () => resolve();
|
|
3138
|
+
tx.onerror = () => reject(tx.error);
|
|
3139
|
+
tx.onabort = () => reject(tx.error ?? /* @__PURE__ */ new Error("transaction aborted"));
|
|
3140
|
+
});
|
|
3141
|
+
}
|
|
3142
|
+
/**
|
|
3143
|
+
* How long local edits are held before being written as one transaction.
|
|
3144
|
+
*
|
|
3145
|
+
* Yjs emits one update per keystroke, and the store used to open one IndexedDB
|
|
3146
|
+
* transaction for each — measured at 1.02 transactions per keystroke over 200
|
|
3147
|
+
* keystrokes. Coalescing trades a bounded window of durability for roughly an
|
|
3148
|
+
* order of magnitude fewer transactions. The window is small on purpose: these
|
|
3149
|
+
* edits have already gone to the server on the wire (that send is NOT batched),
|
|
3150
|
+
* so the exposure is an offline client that dies within the window.
|
|
3151
|
+
*/
|
|
3152
|
+
const WRITE_COALESCE_MS = 150;
|
|
3153
|
+
/** Flush early once a burst reaches this many updates, regardless of the timer. */
|
|
3154
|
+
const MAX_WRITE_BATCH = 128;
|
|
2932
3155
|
var OfflineStore = class {
|
|
2933
3156
|
/**
|
|
2934
3157
|
* @param docId The document UUID.
|
|
@@ -2943,6 +3166,9 @@ var OfflineStore = class {
|
|
|
2943
3166
|
this.db = null;
|
|
2944
3167
|
this.dbPromise = null;
|
|
2945
3168
|
this._destroyed = false;
|
|
3169
|
+
this.writeBuffer = [];
|
|
3170
|
+
this.writeWaiters = [];
|
|
3171
|
+
this.flushTimer = null;
|
|
2946
3172
|
this.storeKey = serverOrigin ? `${serverOrigin}/${docId}` : docId;
|
|
2947
3173
|
}
|
|
2948
3174
|
getDb() {
|
|
@@ -2953,13 +3179,91 @@ var OfflineStore = class {
|
|
|
2953
3179
|
});
|
|
2954
3180
|
return this.dbPromise;
|
|
2955
3181
|
}
|
|
2956
|
-
|
|
2957
|
-
|
|
2958
|
-
|
|
2959
|
-
|
|
2960
|
-
|
|
3182
|
+
/**
|
|
3183
|
+
* Queue a local update for persistence.
|
|
3184
|
+
*
|
|
3185
|
+
* The returned promise settles when the update has actually been committed,
|
|
3186
|
+
* not when it was queued — so `await persistUpdate(u)` still means "on disk",
|
|
3187
|
+
* exactly as it did when every call opened its own transaction.
|
|
3188
|
+
*/
|
|
3189
|
+
persistUpdate(update) {
|
|
3190
|
+
if (this._destroyed || !idbAvailable$4()) return Promise.resolve();
|
|
3191
|
+
this.writeBuffer.push(update);
|
|
3192
|
+
const settled = new Promise((resolve, reject) => {
|
|
3193
|
+
this.writeWaiters.push({
|
|
3194
|
+
resolve,
|
|
3195
|
+
reject
|
|
3196
|
+
});
|
|
3197
|
+
});
|
|
3198
|
+
if (this.writeBuffer.length >= MAX_WRITE_BATCH) this.flushWrites();
|
|
3199
|
+
else if (this.flushTimer === null) this.flushTimer = setTimeout(() => {
|
|
3200
|
+
this.flushWrites();
|
|
3201
|
+
}, WRITE_COALESCE_MS);
|
|
3202
|
+
return settled;
|
|
3203
|
+
}
|
|
3204
|
+
/**
|
|
3205
|
+
* Commit every buffered update as ONE transaction.
|
|
3206
|
+
*
|
|
3207
|
+
* Safe to call at any time, including with an empty buffer — every read path
|
|
3208
|
+
* calls it first so a caller can never observe a store that is missing writes
|
|
3209
|
+
* still sitting in memory.
|
|
3210
|
+
*/
|
|
3211
|
+
async flushWrites() {
|
|
3212
|
+
if (this.flushTimer !== null) {
|
|
3213
|
+
clearTimeout(this.flushTimer);
|
|
3214
|
+
this.flushTimer = null;
|
|
3215
|
+
}
|
|
3216
|
+
const batch = this.writeBuffer;
|
|
3217
|
+
const waiters = this.writeWaiters;
|
|
3218
|
+
this.writeBuffer = [];
|
|
3219
|
+
this.writeWaiters = [];
|
|
3220
|
+
if (batch.length === 0) {
|
|
3221
|
+
for (const w of waiters) w.resolve();
|
|
3222
|
+
return;
|
|
3223
|
+
}
|
|
3224
|
+
try {
|
|
3225
|
+
const db = await this.getDb();
|
|
3226
|
+
if (db) {
|
|
3227
|
+
const tx = db.transaction("updates", "readwrite");
|
|
3228
|
+
const store = tx.objectStore("updates");
|
|
3229
|
+
for (const update of batch) store.add(update);
|
|
3230
|
+
await txComplete(tx);
|
|
3231
|
+
}
|
|
3232
|
+
for (const w of waiters) w.resolve();
|
|
3233
|
+
} catch (err) {
|
|
3234
|
+
for (const w of waiters) w.reject(err);
|
|
3235
|
+
throw err;
|
|
3236
|
+
}
|
|
3237
|
+
}
|
|
3238
|
+
/**
|
|
3239
|
+
* Issue the buffered writes synchronously, without awaiting them.
|
|
3240
|
+
*
|
|
3241
|
+
* For teardown paths that cannot await: `pagehide` (the page may not survive
|
|
3242
|
+
* a microtask) and `destroy()`. IndexedDB will not close a connection until
|
|
3243
|
+
* its running transactions finish, so writes issued here still land.
|
|
3244
|
+
*/
|
|
3245
|
+
flushWritesSync() {
|
|
3246
|
+
if (this.flushTimer !== null) {
|
|
3247
|
+
clearTimeout(this.flushTimer);
|
|
3248
|
+
this.flushTimer = null;
|
|
3249
|
+
}
|
|
3250
|
+
const batch = this.writeBuffer;
|
|
3251
|
+
const waiters = this.writeWaiters;
|
|
3252
|
+
this.writeBuffer = [];
|
|
3253
|
+
this.writeWaiters = [];
|
|
3254
|
+
if (batch.length === 0) {
|
|
3255
|
+
for (const w of waiters) w.resolve();
|
|
3256
|
+
return;
|
|
3257
|
+
}
|
|
3258
|
+
const db = this.db;
|
|
3259
|
+
if (db) try {
|
|
3260
|
+
const store = db.transaction("updates", "readwrite").objectStore("updates");
|
|
3261
|
+
for (const update of batch) store.add(update);
|
|
3262
|
+
} catch {}
|
|
3263
|
+
for (const w of waiters) w.resolve();
|
|
2961
3264
|
}
|
|
2962
3265
|
async getPendingUpdates() {
|
|
3266
|
+
await this.flushWrites().catch(() => null);
|
|
2963
3267
|
const db = await this.getDb();
|
|
2964
3268
|
if (!db) return [];
|
|
2965
3269
|
return new Promise((resolve, reject) => {
|
|
@@ -2969,6 +3273,13 @@ var OfflineStore = class {
|
|
|
2969
3273
|
});
|
|
2970
3274
|
}
|
|
2971
3275
|
async clearPendingUpdates() {
|
|
3276
|
+
if (this.flushTimer !== null) {
|
|
3277
|
+
clearTimeout(this.flushTimer);
|
|
3278
|
+
this.flushTimer = null;
|
|
3279
|
+
}
|
|
3280
|
+
this.writeBuffer = [];
|
|
3281
|
+
for (const w of this.writeWaiters) w.resolve();
|
|
3282
|
+
this.writeWaiters = [];
|
|
2972
3283
|
const db = await this.getDb();
|
|
2973
3284
|
if (!db) return;
|
|
2974
3285
|
const tx = db.transaction("updates", "readwrite");
|
|
@@ -3067,6 +3378,7 @@ var OfflineStore = class {
|
|
|
3067
3378
|
})));
|
|
3068
3379
|
}
|
|
3069
3380
|
destroy() {
|
|
3381
|
+
this.flushWritesSync();
|
|
3070
3382
|
this._destroyed = true;
|
|
3071
3383
|
const db = this.db;
|
|
3072
3384
|
this.db = null;
|
|
@@ -3172,8 +3484,13 @@ var AbracadabraProvider = class AbracadabraProvider extends AbracadabraBaseProvi
|
|
|
3172
3484
|
}) ?? null;
|
|
3173
3485
|
this.on("synced", () => this.flushPendingUpdates());
|
|
3174
3486
|
this.restorePermissionSnapshot();
|
|
3175
|
-
|
|
3487
|
+
let readyTimer = null;
|
|
3488
|
+
this.ready = Promise.race([this._initFromOfflineStore(), new Promise((resolve) => {
|
|
3489
|
+
readyTimer = setTimeout(resolve, 5e3);
|
|
3490
|
+
})]).catch((err) => {
|
|
3176
3491
|
this.emit("error", { message: `Offline store init failed: ${err?.message ?? err}` });
|
|
3492
|
+
}).finally(() => {
|
|
3493
|
+
if (readyTimer !== null) clearTimeout(readyTimer);
|
|
3177
3494
|
});
|
|
3178
3495
|
}
|
|
3179
3496
|
/**
|
|
@@ -3418,6 +3735,7 @@ var AbracadabraProvider = class AbracadabraProvider extends AbracadabraBaseProvi
|
|
|
3418
3735
|
document: childDoc,
|
|
3419
3736
|
websocketProvider: this.configuration.websocketProvider,
|
|
3420
3737
|
token: this.configuration.token,
|
|
3738
|
+
lazyAwareness: true,
|
|
3421
3739
|
subdocLoading: this.subdocLoading,
|
|
3422
3740
|
disableOfflineStore: this.abracadabraConfig.disableOfflineStore,
|
|
3423
3741
|
client: this._client ?? void 0,
|
|
@@ -3496,6 +3814,19 @@ var AbracadabraProvider = class AbracadabraProvider extends AbracadabraBaseProvi
|
|
|
3496
3814
|
return this.childProviders;
|
|
3497
3815
|
}
|
|
3498
3816
|
/**
|
|
3817
|
+
* Land buffered offline writes before the page goes away.
|
|
3818
|
+
*
|
|
3819
|
+
* `OfflineStore` coalesces local updates into one transaction per ~150 ms
|
|
3820
|
+
* window (see WRITE_COALESCE_MS). That window is the only moment an edit
|
|
3821
|
+
* exists in memory but not on disk, and a closing tab is exactly when it
|
|
3822
|
+
* would be lost — so pagehide flushes it synchronously. `super.pageHide()`
|
|
3823
|
+
* still clears our awareness state for peers.
|
|
3824
|
+
*/
|
|
3825
|
+
pageHide() {
|
|
3826
|
+
super.pageHide();
|
|
3827
|
+
this.offlineStore?.flushWritesSync();
|
|
3828
|
+
}
|
|
3829
|
+
/**
|
|
3499
3830
|
* Override to persist every local update to IndexedDB before sending it
|
|
3500
3831
|
* over the wire, ensuring no work is lost during connection outages.
|
|
3501
3832
|
*
|
|
@@ -3543,6 +3874,7 @@ var AbracadabraProvider = class AbracadabraProvider extends AbracadabraBaseProvi
|
|
|
3543
3874
|
update,
|
|
3544
3875
|
documentName: this.configuration.name
|
|
3545
3876
|
});
|
|
3877
|
+
await this.persistSnapshot();
|
|
3546
3878
|
await store.clearPendingUpdates();
|
|
3547
3879
|
}
|
|
3548
3880
|
const pendingSubdocs = await store.getPendingSubdocs();
|
|
@@ -17424,10 +17756,24 @@ var BackgroundSyncManager = class extends EventEmitter {
|
|
|
17424
17756
|
* @returns Cleanup function to stop the periodic sync.
|
|
17425
17757
|
*/
|
|
17426
17758
|
startPeriodicSync(intervalMs = 300 * 1e3) {
|
|
17759
|
+
let missedWhileHidden = false;
|
|
17427
17760
|
const handle = setInterval(() => {
|
|
17428
|
-
if (
|
|
17761
|
+
if (this._destroyed) return;
|
|
17762
|
+
if (getLifecycleGate().backgrounded) {
|
|
17763
|
+
missedWhileHidden = true;
|
|
17764
|
+
return;
|
|
17765
|
+
}
|
|
17766
|
+
this.syncAll().catch(() => null);
|
|
17429
17767
|
}, intervalMs);
|
|
17430
|
-
|
|
17768
|
+
const unsubscribe = getLifecycleGate().onChange((backgrounded) => {
|
|
17769
|
+
if (backgrounded || !missedWhileHidden || this._destroyed) return;
|
|
17770
|
+
missedWhileHidden = false;
|
|
17771
|
+
this.syncAll().catch(() => null);
|
|
17772
|
+
});
|
|
17773
|
+
return () => {
|
|
17774
|
+
clearInterval(handle);
|
|
17775
|
+
unsubscribe();
|
|
17776
|
+
};
|
|
17431
17777
|
}
|
|
17432
17778
|
/**
|
|
17433
17779
|
* Clear all offline document data and sync state.
|
|
@@ -22432,5 +22778,339 @@ var DocumentManager = class {
|
|
|
22432
22778
|
};
|
|
22433
22779
|
|
|
22434
22780
|
//#endregion
|
|
22435
|
-
|
|
22781
|
+
//#region packages/provider/src/PerfCounters.ts
|
|
22782
|
+
let installed = false;
|
|
22783
|
+
let startedAt = 0;
|
|
22784
|
+
const liveTimers = /* @__PURE__ */ new Map();
|
|
22785
|
+
const groupCreated = /* @__PURE__ */ new Map();
|
|
22786
|
+
const groupFired = /* @__PURE__ */ new Map();
|
|
22787
|
+
let intervalsCreated = 0;
|
|
22788
|
+
let timeoutsCreated = 0;
|
|
22789
|
+
let timersFired = 0;
|
|
22790
|
+
let rafRequested = 0;
|
|
22791
|
+
let rafFired = 0;
|
|
22792
|
+
let idbTransactions = 0;
|
|
22793
|
+
let idbDatabasesOpened = 0;
|
|
22794
|
+
const idbByStore = /* @__PURE__ */ new Map();
|
|
22795
|
+
let framesSent = 0;
|
|
22796
|
+
let bytesSent = 0;
|
|
22797
|
+
let framesReceived = 0;
|
|
22798
|
+
let bytesReceived = 0;
|
|
22799
|
+
const sentByType = /* @__PURE__ */ new Map();
|
|
22800
|
+
let origSetInterval = null;
|
|
22801
|
+
let origClearInterval = null;
|
|
22802
|
+
let origSetTimeout = null;
|
|
22803
|
+
let origClearTimeout = null;
|
|
22804
|
+
let origRaf = null;
|
|
22805
|
+
let origIdbTransaction = null;
|
|
22806
|
+
let origIdbOpen = null;
|
|
22807
|
+
const socketRestores = [];
|
|
22808
|
+
/** The heartbeat every y-protocols Awareness instance starts: outdatedTimeout / 10. */
|
|
22809
|
+
const AWARENESS_HEARTBEAT_MS = 3e3;
|
|
22810
|
+
/**
|
|
22811
|
+
* Best-effort "who created this timer". Walks the stack past this module and
|
|
22812
|
+
* past the Node/browser internals, and reports the first frame that looks like
|
|
22813
|
+
* real code. Deliberately coarse: a file name is enough to tell an Awareness
|
|
22814
|
+
* heartbeat apart from a reconnect backoff, and anything finer would be noise.
|
|
22815
|
+
*/
|
|
22816
|
+
function captureOrigin() {
|
|
22817
|
+
const stack = (/* @__PURE__ */ new Error()).stack;
|
|
22818
|
+
if (!stack) return "unknown";
|
|
22819
|
+
const lines = stack.split("\n").slice(1);
|
|
22820
|
+
for (const line of lines) {
|
|
22821
|
+
if (line.includes("PerfCounters")) continue;
|
|
22822
|
+
if (line.includes("node:internal")) continue;
|
|
22823
|
+
const nm = line.match(/node_modules\/(?:\.pnpm\/[^/]+\/node_modules\/)?(.+?):\d+:\d+/);
|
|
22824
|
+
if (nm?.[1]) return nm[1];
|
|
22825
|
+
const src = line.match(/([^/\\()]+\.(?:ts|mts|js|mjs)):\d+:\d+/);
|
|
22826
|
+
if (src?.[1]) return src[1];
|
|
22827
|
+
}
|
|
22828
|
+
return "unknown";
|
|
22829
|
+
}
|
|
22830
|
+
function groupKey(origin, delayMs) {
|
|
22831
|
+
return `${origin}@${delayMs}`;
|
|
22832
|
+
}
|
|
22833
|
+
function bump(map, key, by = 1) {
|
|
22834
|
+
map.set(key, (map.get(key) ?? 0) + by);
|
|
22835
|
+
}
|
|
22836
|
+
/**
|
|
22837
|
+
* Begin counting. Idempotent — a second call is a no-op rather than a
|
|
22838
|
+
* double-wrap, which would double every number and be very hard to notice.
|
|
22839
|
+
*/
|
|
22840
|
+
function installPerfCounters() {
|
|
22841
|
+
if (installed) return;
|
|
22842
|
+
installed = true;
|
|
22843
|
+
startedAt = Date.now();
|
|
22844
|
+
const g = globalThis;
|
|
22845
|
+
origSetInterval = g.setInterval;
|
|
22846
|
+
origClearInterval = g.clearInterval;
|
|
22847
|
+
origSetTimeout = g.setTimeout;
|
|
22848
|
+
origClearTimeout = g.clearTimeout;
|
|
22849
|
+
g.setInterval = function patchedSetInterval(handler, delay, ...args) {
|
|
22850
|
+
const origin = captureOrigin();
|
|
22851
|
+
const delayMs = delay ?? 0;
|
|
22852
|
+
const key = groupKey(origin, delayMs);
|
|
22853
|
+
const wrapped = typeof handler === "function" ? (...cbArgs) => {
|
|
22854
|
+
timersFired++;
|
|
22855
|
+
bump(groupFired, key);
|
|
22856
|
+
return handler(...cbArgs);
|
|
22857
|
+
} : handler;
|
|
22858
|
+
const id = origSetInterval.call(g, wrapped, delay, ...args);
|
|
22859
|
+
intervalsCreated++;
|
|
22860
|
+
bump(groupCreated, key);
|
|
22861
|
+
liveTimers.set(id, {
|
|
22862
|
+
origin,
|
|
22863
|
+
delayMs,
|
|
22864
|
+
kind: "interval"
|
|
22865
|
+
});
|
|
22866
|
+
return id;
|
|
22867
|
+
};
|
|
22868
|
+
g.clearInterval = function patchedClearInterval(id) {
|
|
22869
|
+
liveTimers.delete(id);
|
|
22870
|
+
return origClearInterval.call(g, id);
|
|
22871
|
+
};
|
|
22872
|
+
g.setTimeout = function patchedSetTimeout(handler, delay, ...args) {
|
|
22873
|
+
const origin = captureOrigin();
|
|
22874
|
+
const delayMs = delay ?? 0;
|
|
22875
|
+
const key = groupKey(origin, delayMs);
|
|
22876
|
+
let id;
|
|
22877
|
+
const wrapped = typeof handler === "function" ? (...cbArgs) => {
|
|
22878
|
+
timersFired++;
|
|
22879
|
+
bump(groupFired, key);
|
|
22880
|
+
liveTimers.delete(id);
|
|
22881
|
+
return handler(...cbArgs);
|
|
22882
|
+
} : handler;
|
|
22883
|
+
id = origSetTimeout.call(g, wrapped, delay, ...args);
|
|
22884
|
+
timeoutsCreated++;
|
|
22885
|
+
bump(groupCreated, key);
|
|
22886
|
+
liveTimers.set(id, {
|
|
22887
|
+
origin,
|
|
22888
|
+
delayMs,
|
|
22889
|
+
kind: "timeout"
|
|
22890
|
+
});
|
|
22891
|
+
return id;
|
|
22892
|
+
};
|
|
22893
|
+
g.clearTimeout = function patchedClearTimeout(id) {
|
|
22894
|
+
liveTimers.delete(id);
|
|
22895
|
+
return origClearTimeout.call(g, id);
|
|
22896
|
+
};
|
|
22897
|
+
if (typeof g.requestAnimationFrame === "function") {
|
|
22898
|
+
origRaf = g.requestAnimationFrame;
|
|
22899
|
+
g.requestAnimationFrame = function patchedRaf(cb) {
|
|
22900
|
+
rafRequested++;
|
|
22901
|
+
return origRaf.call(g, (t) => {
|
|
22902
|
+
rafFired++;
|
|
22903
|
+
return cb(t);
|
|
22904
|
+
});
|
|
22905
|
+
};
|
|
22906
|
+
}
|
|
22907
|
+
if (typeof g.IDBDatabase === "function") {
|
|
22908
|
+
origIdbTransaction = g.IDBDatabase.prototype.transaction;
|
|
22909
|
+
g.IDBDatabase.prototype.transaction = function patchedTransaction(storeNames, ...rest) {
|
|
22910
|
+
idbTransactions++;
|
|
22911
|
+
const names = Array.isArray(storeNames) ? storeNames : [storeNames];
|
|
22912
|
+
for (const n of names) bump(idbByStore, String(n));
|
|
22913
|
+
return origIdbTransaction.call(this, storeNames, ...rest);
|
|
22914
|
+
};
|
|
22915
|
+
}
|
|
22916
|
+
if (g.indexedDB && typeof g.indexedDB.open === "function") {
|
|
22917
|
+
origIdbOpen = g.indexedDB.open.bind(g.indexedDB);
|
|
22918
|
+
g.indexedDB.open = function patchedOpen(...args) {
|
|
22919
|
+
idbDatabasesOpened++;
|
|
22920
|
+
return origIdbOpen(...args);
|
|
22921
|
+
};
|
|
22922
|
+
}
|
|
22923
|
+
}
|
|
22924
|
+
/** Restore every patched global. Safe to call when not installed. */
|
|
22925
|
+
function uninstall() {
|
|
22926
|
+
if (!installed) return;
|
|
22927
|
+
const g = globalThis;
|
|
22928
|
+
if (origSetInterval) g.setInterval = origSetInterval;
|
|
22929
|
+
if (origClearInterval) g.clearInterval = origClearInterval;
|
|
22930
|
+
if (origSetTimeout) g.setTimeout = origSetTimeout;
|
|
22931
|
+
if (origClearTimeout) g.clearTimeout = origClearTimeout;
|
|
22932
|
+
if (origRaf) g.requestAnimationFrame = origRaf;
|
|
22933
|
+
if (origIdbTransaction && typeof g.IDBDatabase === "function") g.IDBDatabase.prototype.transaction = origIdbTransaction;
|
|
22934
|
+
if (origIdbOpen && g.indexedDB) g.indexedDB.open = origIdbOpen;
|
|
22935
|
+
for (const restore of socketRestores.splice(0)) restore();
|
|
22936
|
+
origSetInterval = null;
|
|
22937
|
+
origClearInterval = null;
|
|
22938
|
+
origSetTimeout = null;
|
|
22939
|
+
origClearTimeout = null;
|
|
22940
|
+
origRaf = null;
|
|
22941
|
+
origIdbTransaction = null;
|
|
22942
|
+
origIdbOpen = null;
|
|
22943
|
+
installed = false;
|
|
22944
|
+
reset();
|
|
22945
|
+
}
|
|
22946
|
+
/** Zero every counter. Live-timer bookkeeping is preserved — it is a census. */
|
|
22947
|
+
function reset() {
|
|
22948
|
+
startedAt = Date.now();
|
|
22949
|
+
groupCreated.clear();
|
|
22950
|
+
groupFired.clear();
|
|
22951
|
+
intervalsCreated = 0;
|
|
22952
|
+
timeoutsCreated = 0;
|
|
22953
|
+
timersFired = 0;
|
|
22954
|
+
rafRequested = 0;
|
|
22955
|
+
rafFired = 0;
|
|
22956
|
+
idbTransactions = 0;
|
|
22957
|
+
idbDatabasesOpened = 0;
|
|
22958
|
+
idbByStore.clear();
|
|
22959
|
+
framesSent = 0;
|
|
22960
|
+
bytesSent = 0;
|
|
22961
|
+
framesReceived = 0;
|
|
22962
|
+
bytesReceived = 0;
|
|
22963
|
+
sentByType.clear();
|
|
22964
|
+
}
|
|
22965
|
+
/**
|
|
22966
|
+
* Count frames on one AbracadabraWS. Returns a restore function; the restore is
|
|
22967
|
+
* also run by `uninstall()`.
|
|
22968
|
+
*
|
|
22969
|
+
* Wrapping the instance (rather than a global) is deliberate: in Node the
|
|
22970
|
+
* provider runs on the `ws` package, so patching `globalThis.WebSocket` would
|
|
22971
|
+
* silently measure nothing at all.
|
|
22972
|
+
*/
|
|
22973
|
+
function instrumentSocket(socket) {
|
|
22974
|
+
const originalSend = socket.send.bind(socket);
|
|
22975
|
+
socket.send = (message) => {
|
|
22976
|
+
framesSent++;
|
|
22977
|
+
const bytes = byteLength(message);
|
|
22978
|
+
bytesSent += bytes;
|
|
22979
|
+
const type = readMessageType(message);
|
|
22980
|
+
if (type !== null) bump(sentByType, type);
|
|
22981
|
+
return originalSend(message);
|
|
22982
|
+
};
|
|
22983
|
+
const onMessage = (payload) => {
|
|
22984
|
+
framesReceived++;
|
|
22985
|
+
bytesReceived += byteLength(payload?.event?.data ?? payload?.data ?? payload);
|
|
22986
|
+
};
|
|
22987
|
+
socket.on("message", onMessage);
|
|
22988
|
+
const restore = () => {
|
|
22989
|
+
socket.send = originalSend;
|
|
22990
|
+
try {
|
|
22991
|
+
socket.off("message", onMessage);
|
|
22992
|
+
} catch {}
|
|
22993
|
+
};
|
|
22994
|
+
socketRestores.push(restore);
|
|
22995
|
+
return restore;
|
|
22996
|
+
}
|
|
22997
|
+
function byteLength(value) {
|
|
22998
|
+
if (!value) return 0;
|
|
22999
|
+
if (value instanceof Uint8Array) return value.byteLength;
|
|
23000
|
+
if (value instanceof ArrayBuffer) return value.byteLength;
|
|
23001
|
+
if (typeof value === "string") return value.length;
|
|
23002
|
+
const encoded = value?.toUint8Array?.() ?? value?.message;
|
|
23003
|
+
if (encoded instanceof Uint8Array) return encoded.byteLength;
|
|
23004
|
+
return 0;
|
|
23005
|
+
}
|
|
23006
|
+
/**
|
|
23007
|
+
* The Hocuspocus V2 frame puts a varstring document name first, then the
|
|
23008
|
+
* message type. We only need a stable label, so report the type byte as-is
|
|
23009
|
+
* rather than duplicating the wire decoder here.
|
|
23010
|
+
*/
|
|
23011
|
+
function readMessageType(message) {
|
|
23012
|
+
const encoded = message?.toUint8Array?.() ?? message?.message ?? (message instanceof Uint8Array ? message : null);
|
|
23013
|
+
if (!(encoded instanceof Uint8Array) || encoded.length === 0) return null;
|
|
23014
|
+
let offset = 0;
|
|
23015
|
+
let nameLen = 0;
|
|
23016
|
+
let shift = 0;
|
|
23017
|
+
while (offset < encoded.length) {
|
|
23018
|
+
const byte = encoded[offset++];
|
|
23019
|
+
nameLen |= (byte & 127) << shift;
|
|
23020
|
+
if ((byte & 128) === 0) break;
|
|
23021
|
+
shift += 7;
|
|
23022
|
+
}
|
|
23023
|
+
const typeIndex = offset + nameLen;
|
|
23024
|
+
if (typeIndex >= encoded.length) return null;
|
|
23025
|
+
return `type:${encoded[typeIndex]}`;
|
|
23026
|
+
}
|
|
23027
|
+
function snapshot() {
|
|
23028
|
+
const elapsedMs = Math.max(1, Date.now() - startedAt);
|
|
23029
|
+
const liveByKey = /* @__PURE__ */ new Map();
|
|
23030
|
+
let intervalsLive = 0;
|
|
23031
|
+
let timeoutsLive = 0;
|
|
23032
|
+
let awarenessLive = 0;
|
|
23033
|
+
for (const rec of liveTimers.values()) {
|
|
23034
|
+
bump(liveByKey, groupKey(rec.origin, rec.delayMs));
|
|
23035
|
+
if (rec.kind === "interval") {
|
|
23036
|
+
intervalsLive++;
|
|
23037
|
+
if (isAwarenessHeartbeat(rec)) awarenessLive++;
|
|
23038
|
+
} else timeoutsLive++;
|
|
23039
|
+
}
|
|
23040
|
+
const groups = [...new Set([
|
|
23041
|
+
...groupCreated.keys(),
|
|
23042
|
+
...groupFired.keys(),
|
|
23043
|
+
...liveByKey.keys()
|
|
23044
|
+
])].map((key) => {
|
|
23045
|
+
const at = key.lastIndexOf("@");
|
|
23046
|
+
return {
|
|
23047
|
+
origin: key.slice(0, at),
|
|
23048
|
+
delayMs: Number(key.slice(at + 1)),
|
|
23049
|
+
created: groupCreated.get(key) ?? 0,
|
|
23050
|
+
live: liveByKey.get(key) ?? 0,
|
|
23051
|
+
fired: groupFired.get(key) ?? 0
|
|
23052
|
+
};
|
|
23053
|
+
}).sort((a, b) => b.live - a.live || b.fired - a.fired);
|
|
23054
|
+
let heartbeatFires = 0;
|
|
23055
|
+
for (const group of groups) if (group.delayMs === AWARENESS_HEARTBEAT_MS && isAwarenessOrigin(group.origin)) heartbeatFires += group.fired;
|
|
23056
|
+
return {
|
|
23057
|
+
elapsedMs,
|
|
23058
|
+
timers: {
|
|
23059
|
+
intervalsCreated,
|
|
23060
|
+
intervalsLive,
|
|
23061
|
+
timeoutsCreated,
|
|
23062
|
+
timeoutsLive,
|
|
23063
|
+
fired: timersFired,
|
|
23064
|
+
firedPerSecond: Number((timersFired / elapsedMs * 1e3).toFixed(2)),
|
|
23065
|
+
groups
|
|
23066
|
+
},
|
|
23067
|
+
animationFrames: {
|
|
23068
|
+
requested: rafRequested,
|
|
23069
|
+
fired: rafFired
|
|
23070
|
+
},
|
|
23071
|
+
awareness: {
|
|
23072
|
+
liveInstances: awarenessLive,
|
|
23073
|
+
heartbeatFires
|
|
23074
|
+
},
|
|
23075
|
+
indexedDb: {
|
|
23076
|
+
transactions: idbTransactions,
|
|
23077
|
+
byStore: Object.fromEntries(idbByStore),
|
|
23078
|
+
databasesOpened: idbDatabasesOpened
|
|
23079
|
+
},
|
|
23080
|
+
sockets: {
|
|
23081
|
+
framesSent,
|
|
23082
|
+
bytesSent,
|
|
23083
|
+
framesReceived,
|
|
23084
|
+
bytesReceived,
|
|
23085
|
+
sentByType: Object.fromEntries(sentByType)
|
|
23086
|
+
}
|
|
23087
|
+
};
|
|
23088
|
+
}
|
|
23089
|
+
function isAwarenessOrigin(origin) {
|
|
23090
|
+
return origin.includes("awareness");
|
|
23091
|
+
}
|
|
23092
|
+
function isAwarenessHeartbeat(rec) {
|
|
23093
|
+
return rec.delayMs === AWARENESS_HEARTBEAT_MS && isAwarenessOrigin(rec.origin);
|
|
23094
|
+
}
|
|
23095
|
+
/** Field-by-field difference of two snapshots, for before/after comparisons. */
|
|
23096
|
+
function diff(before, after) {
|
|
23097
|
+
return {
|
|
23098
|
+
elapsedMs: after.elapsedMs - before.elapsedMs,
|
|
23099
|
+
timersFired: after.timers.fired - before.timers.fired,
|
|
23100
|
+
intervalsCreated: after.timers.intervalsCreated - before.timers.intervalsCreated,
|
|
23101
|
+
intervalsLive: after.timers.intervalsLive - before.timers.intervalsLive,
|
|
23102
|
+
awarenessLive: after.awareness.liveInstances - before.awareness.liveInstances,
|
|
23103
|
+
heartbeatFires: after.awareness.heartbeatFires - before.awareness.heartbeatFires,
|
|
23104
|
+
framesSent: after.sockets.framesSent - before.sockets.framesSent,
|
|
23105
|
+
bytesSent: after.sockets.bytesSent - before.sockets.bytesSent,
|
|
23106
|
+
idbTransactions: after.indexedDb.transactions - before.indexedDb.transactions
|
|
23107
|
+
};
|
|
23108
|
+
}
|
|
23109
|
+
/** Whether counting is currently active. */
|
|
23110
|
+
function isInstalled() {
|
|
23111
|
+
return installed;
|
|
23112
|
+
}
|
|
23113
|
+
|
|
23114
|
+
//#endregion
|
|
23115
|
+
export { AbracadabraBaseProvider, AbracadabraClient, AbracadabraProvider, AbracadabraWS, AbracadabraWebRTC, AuthMessageType, AwarenessError, BackgroundSyncManager, BackgroundSyncPersistence, BroadcastChannelSync, CHANNEL_NAMES, ChannelKeyResolver, ChatClient, ConnectionTimeout, ContentManager, CryptoIdentityKeystore, DEFAULT_FILE_CHUNK_SIZE, DEFAULT_ICE_SERVERS, DataChannelRouter, DevicePairingChannel, DeviceRegistrationService, DocKeyManager, DocumentCache, DocumentManager, E2EAbracadabraProvider, E2EEChannel, E2EOfflineStore, EncryptedChatClient, EncryptedYMap, EncryptedYText, FileBlobStore, FileTransferChannel, FileTransferHandle, Forbidden, GEO_TYPE_META_SCHEMAS, HIDE_GRACE_MS, HocuspocusProvider, HocuspocusProviderWebsocket, IdentityDocProvider, KEY_EXCHANGE_CHANNEL, Kind, LifecycleGate, LocalStorageDeviceSessionStorage, ManualSignaling, ManualVisibilitySource, MessageTooBig, MessageType, MetaManager, MetaValidationError, NotificationsClient, OfflineStore, PAGE_TYPES, PeerConnection, QUERY_PREFIX, QueryClient, QueryError, RPC_PREFIX, ResetConnection, RpcClient, RpcError, SERVER_ROOT_ID, SearchIndex, SignalingSocket, SubdocMessage, TYPE_ALIASES, TokenManager, TreeManager, TypedDocTypeMismatchError, Unauthorized, WebSocketStatus, WsReadyStates, YjsDataChannel, addServer, attachUpdatedAtObserver, awarenessStatesToArray, wordlist as bip39Wordlist, buildBlockquoteElement, buildBlocksFromMarkdown, buildBulletListElement, buildCodeBlockElement, buildHeadingElement, buildHorizontalRuleElement, buildOrderedListElement, buildParagraphElement, buildTaskListElement, collectMediaFileBlocks, decryptChatContent, decryptField, defaultKeyFileFor, deriveDmDocId, deriveIdentityDocId, deriveSeedWrappingKey, deterministicKeypair, dropCachesIfServerReplaced, encryptChatContent, encryptField, filenameToLabel, foldRecords, forgetServerIdentity, generateMnemonic, getLifecycleGate, installPerfCounters, isEncryptedContent, isPlaceholderLabel, isServerScopedStore, loadOrCreateKeyFile, loadServerRegistry, loginOrRegisterWithKey, loginOrRegisterWithPolicy, makeChallengeSigner, makeEncryptedYMap, makeEncryptedYText, makeEntryMap, migrateLegacyDefault, mnemonicToEd25519Seed, mnemonicToKeyPair, normalizeRootId, openSyncedProvider, parseFrontmatter, patchEntry, isInstalled as perfCountersInstalled, diff as perfDiff, instrumentSocket as perfInstrumentSocket, snapshot as perfSnapshot, pickEntrySpace, populateYDocFromMarkdown, readAuthMessage, readBlocksFromFragment, readEntries, reconcileDocCover, recordFromYAny, removeServer, reset as resetPerfCounters, resolvePageType, resolveServer, saveServerRegistry, serverRegistryPath, setDefault, setLifecycleGate, signChallengeWithSeed, toPlain, uninstall as uninstallPerfCounters, unwrapSeed, validateMnemonic, waitForSync, withTimeout, wrapSeed, writeAuthenticated, writeAuthentication, writePermissionDenied, writeTokenSyncRequest, yjsToMarkdown };
|
|
22436
23116
|
//# sourceMappingURL=abracadabra-provider.esm.js.map
|