@korajs/sync 0.5.0 → 0.6.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/index.cjs CHANGED
@@ -41,6 +41,7 @@ __export(index_exports, {
41
41
  JsonMessageSerializer: () => JsonMessageSerializer,
42
42
  KeyDerivationError: () => KeyDerivationError,
43
43
  NegotiatedMessageSerializer: () => NegotiatedMessageSerializer,
44
+ OFFLINE_SYNC_STATUS: () => OFFLINE_SYNC_STATUS,
44
45
  OutboundQueue: () => OutboundQueue,
45
46
  ProtobufMessageSerializer: () => ProtobufMessageSerializer,
46
47
  ReconnectionManager: () => ReconnectionManager,
@@ -53,6 +54,7 @@ __export(index_exports, {
53
54
  SyncEngine: () => SyncEngine,
54
55
  WebSocketTransport: () => WebSocketTransport,
55
56
  createDeltaCursorFromBatch: () => createDeltaCursorFromBatch,
57
+ createSyncStatusController: () => createSyncStatusController,
56
58
  decodeDeltaCursor: () => decodeDeltaCursor,
57
59
  decodeYjsUpdate: () => decodeYjsUpdate,
58
60
  dedupeQuerySubsets: () => dedupeQuerySubsets,
@@ -62,6 +64,7 @@ __export(index_exports, {
62
64
  encodeYjsUpdate: () => encodeYjsUpdate,
63
65
  filterOperationsByScope: () => filterOperationsByScope,
64
66
  generateSalt: () => generateSalt,
67
+ getRemoteAwarenessStates: () => getRemoteAwarenessStates,
65
68
  isAcknowledgmentMessage: () => isAcknowledgmentMessage,
66
69
  isAwarenessUpdateMessage: () => isAwarenessUpdateMessage,
67
70
  isClientSchemaVersionSupported: () => isClientSchemaVersionSupported,
@@ -77,6 +80,7 @@ __export(index_exports, {
77
80
  operationMatchesScope: () => operationMatchesScope,
78
81
  richtextDocKey: () => richtextDocKey,
79
82
  sliceOperationsAfterCursor: () => sliceOperationsAfterCursor,
83
+ subscribeRemoteAwarenessStates: () => subscribeRemoteAwarenessStates,
80
84
  versionVectorToWire: () => versionVectorToWire,
81
85
  wireToVersionVector: () => wireToVersionVector
82
86
  });
@@ -3885,6 +3889,158 @@ function fromBase642(str) {
3885
3889
  }
3886
3890
  return bytes;
3887
3891
  }
3892
+
3893
+ // src/reactivity/sync-status-controller.ts
3894
+ var OFFLINE_SYNC_STATUS = Object.freeze({
3895
+ status: "offline",
3896
+ pendingOperations: 0,
3897
+ lastSyncedAt: null,
3898
+ lastSuccessfulPush: null,
3899
+ lastSuccessfulPull: null,
3900
+ conflicts: 0
3901
+ });
3902
+ var SYNC_STATUS_EVENT_TYPES = [
3903
+ "sync:connected",
3904
+ "sync:disconnected",
3905
+ "sync:schema-mismatch",
3906
+ "sync:auth-failed",
3907
+ "sync:sent",
3908
+ "sync:received",
3909
+ "sync:acknowledged",
3910
+ "sync:apply-failed",
3911
+ "sync:diagnostics",
3912
+ "sync:initial-sync-progress"
3913
+ ];
3914
+ function createSyncStatusController(options) {
3915
+ const useLiveSnapshot = options.subscribeSyncStatus === null && options.events === null;
3916
+ let snapshot = OFFLINE_SYNC_STATUS;
3917
+ let serialized = JSON.stringify(OFFLINE_SYNC_STATUS);
3918
+ const listeners = /* @__PURE__ */ new Set();
3919
+ let cleanup = null;
3920
+ const resolveEngine = () => {
3921
+ return options.getSyncEngine?.() ?? options.syncEngine ?? null;
3922
+ };
3923
+ const notify = () => {
3924
+ for (const listener of listeners) {
3925
+ listener();
3926
+ }
3927
+ };
3928
+ const setSnapshot = (next) => {
3929
+ const nextSerialized = JSON.stringify(next);
3930
+ if (nextSerialized === serialized) {
3931
+ return;
3932
+ }
3933
+ serialized = nextSerialized;
3934
+ snapshot = next;
3935
+ notify();
3936
+ };
3937
+ const refresh = () => {
3938
+ const engine = resolveEngine();
3939
+ const next = engine ? engine.getStatus() : OFFLINE_SYNC_STATUS;
3940
+ setSnapshot(next);
3941
+ };
3942
+ const attach = () => {
3943
+ cleanup?.();
3944
+ if (options.subscribeSyncStatus) {
3945
+ cleanup = options.subscribeSyncStatus(setSnapshot);
3946
+ return;
3947
+ }
3948
+ if (!resolveEngine()) {
3949
+ setSnapshot(OFFLINE_SYNC_STATUS);
3950
+ cleanup = () => {
3951
+ };
3952
+ return;
3953
+ }
3954
+ if (options.events) {
3955
+ const unsubs = SYNC_STATUS_EVENT_TYPES.map((type) => options.events?.on(type, refresh));
3956
+ refresh();
3957
+ cleanup = () => {
3958
+ for (const unsub of unsubs) {
3959
+ unsub?.();
3960
+ }
3961
+ };
3962
+ return;
3963
+ }
3964
+ refresh();
3965
+ cleanup = () => {
3966
+ };
3967
+ };
3968
+ attach();
3969
+ return {
3970
+ getSnapshot() {
3971
+ if (useLiveSnapshot) {
3972
+ const engine = resolveEngine();
3973
+ return engine ? engine.getStatus() : OFFLINE_SYNC_STATUS;
3974
+ }
3975
+ return snapshot;
3976
+ },
3977
+ subscribe(listener) {
3978
+ listeners.add(listener);
3979
+ listener();
3980
+ return () => {
3981
+ listeners.delete(listener);
3982
+ };
3983
+ },
3984
+ refresh,
3985
+ destroy() {
3986
+ cleanup?.();
3987
+ cleanup = null;
3988
+ listeners.clear();
3989
+ }
3990
+ };
3991
+ }
3992
+
3993
+ // src/reactivity/collaborators-snapshot.ts
3994
+ var EMPTY_COLLABORATORS = [];
3995
+ function awarenessStatesEqual(left, right) {
3996
+ if (left.length !== right.length) {
3997
+ return false;
3998
+ }
3999
+ for (let index = 0; index < left.length; index++) {
4000
+ const a = left[index];
4001
+ const b = right[index];
4002
+ if (!a || !b) {
4003
+ return false;
4004
+ }
4005
+ if (a.user.name !== b.user.name || a.user.color !== b.user.color) {
4006
+ return false;
4007
+ }
4008
+ const aCursor = a.cursor;
4009
+ const bCursor = b.cursor;
4010
+ if (aCursor === void 0 && bCursor === void 0) {
4011
+ continue;
4012
+ }
4013
+ if (!aCursor || !bCursor) {
4014
+ return false;
4015
+ }
4016
+ if (aCursor.collection !== bCursor.collection || aCursor.recordId !== bCursor.recordId || aCursor.field !== bCursor.field || aCursor.anchor !== bCursor.anchor || aCursor.head !== bCursor.head) {
4017
+ return false;
4018
+ }
4019
+ }
4020
+ return true;
4021
+ }
4022
+ function getRemoteAwarenessStates(awareness) {
4023
+ const localClientId = awareness.clientId;
4024
+ const remoteStates = [];
4025
+ for (const [clientId, state] of awareness.getStates()) {
4026
+ if (clientId === localClientId) continue;
4027
+ remoteStates.push(state);
4028
+ }
4029
+ return remoteStates;
4030
+ }
4031
+ function subscribeRemoteAwarenessStates(awareness, listener) {
4032
+ let snapshot = EMPTY_COLLABORATORS;
4033
+ const emitIfChanged = () => {
4034
+ const next = getRemoteAwarenessStates(awareness);
4035
+ if (awarenessStatesEqual(snapshot, next)) {
4036
+ return;
4037
+ }
4038
+ snapshot = next;
4039
+ listener(snapshot);
4040
+ };
4041
+ emitIfChanged();
4042
+ return awareness.on("change", emitIfChanged);
4043
+ }
3888
4044
  // Annotate the CommonJS export names for ESM import in node:
3889
4045
  0 && (module.exports = {
3890
4046
  AwarenessManager,
@@ -3898,6 +4054,7 @@ function fromBase642(str) {
3898
4054
  JsonMessageSerializer,
3899
4055
  KeyDerivationError,
3900
4056
  NegotiatedMessageSerializer,
4057
+ OFFLINE_SYNC_STATUS,
3901
4058
  OutboundQueue,
3902
4059
  ProtobufMessageSerializer,
3903
4060
  ReconnectionManager,
@@ -3910,6 +4067,7 @@ function fromBase642(str) {
3910
4067
  SyncEngine,
3911
4068
  WebSocketTransport,
3912
4069
  createDeltaCursorFromBatch,
4070
+ createSyncStatusController,
3913
4071
  decodeDeltaCursor,
3914
4072
  decodeYjsUpdate,
3915
4073
  dedupeQuerySubsets,
@@ -3919,6 +4077,7 @@ function fromBase642(str) {
3919
4077
  encodeYjsUpdate,
3920
4078
  filterOperationsByScope,
3921
4079
  generateSalt,
4080
+ getRemoteAwarenessStates,
3922
4081
  isAcknowledgmentMessage,
3923
4082
  isAwarenessUpdateMessage,
3924
4083
  isClientSchemaVersionSupported,
@@ -3934,6 +4093,7 @@ function fromBase642(str) {
3934
4093
  operationMatchesScope,
3935
4094
  richtextDocKey,
3936
4095
  sliceOperationsAfterCursor,
4096
+ subscribeRemoteAwarenessStates,
3937
4097
  versionVectorToWire,
3938
4098
  wireToVersionVector
3939
4099
  });