mindcache 3.3.0 → 3.3.2

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.mjs CHANGED
@@ -183,6 +183,7 @@ var init_CloudAdapter = __esm({
183
183
  if (!config.baseUrl) {
184
184
  throw new Error("MindCache Cloud: baseUrl is required. Please provide the cloud API URL in your configuration.");
185
185
  }
186
+ this.setupNetworkDetection();
186
187
  }
187
188
  ws = null;
188
189
  mindcache = null;
@@ -190,8 +191,52 @@ var init_CloudAdapter = __esm({
190
191
  reconnectAttempts = 0;
191
192
  reconnectTimeout = null;
192
193
  _state = "disconnected";
194
+ _isOnline = true;
195
+ // Browser network status
193
196
  listeners = {};
194
197
  token = null;
198
+ handleOnline = null;
199
+ handleOffline = null;
200
+ /** Browser network status - instantly updated via navigator.onLine */
201
+ get isOnline() {
202
+ return this._isOnline;
203
+ }
204
+ setupNetworkDetection() {
205
+ if (typeof window === "undefined" || typeof navigator === "undefined") {
206
+ return;
207
+ }
208
+ this._isOnline = navigator.onLine;
209
+ this.handleOnline = () => {
210
+ console.log("\u2601\uFE0F CloudAdapter: Network is back online");
211
+ this._isOnline = true;
212
+ this.emit("network_online");
213
+ if (this._state === "disconnected" || this._state === "error") {
214
+ this.connect();
215
+ }
216
+ };
217
+ this.handleOffline = () => {
218
+ console.log("\u2601\uFE0F CloudAdapter: Network went offline");
219
+ this._isOnline = false;
220
+ this.emit("network_offline");
221
+ if (this._state === "connected" || this._state === "connecting") {
222
+ this._state = "disconnected";
223
+ this.emit("disconnected");
224
+ }
225
+ };
226
+ window.addEventListener("online", this.handleOnline);
227
+ window.addEventListener("offline", this.handleOffline);
228
+ }
229
+ cleanupNetworkDetection() {
230
+ if (typeof window === "undefined") {
231
+ return;
232
+ }
233
+ if (this.handleOnline) {
234
+ window.removeEventListener("online", this.handleOnline);
235
+ }
236
+ if (this.handleOffline) {
237
+ window.removeEventListener("offline", this.handleOffline);
238
+ }
239
+ }
195
240
  setToken(token) {
196
241
  this.token = token;
197
242
  }
@@ -285,6 +330,7 @@ var init_CloudAdapter = __esm({
285
330
  this.ws.close();
286
331
  this.ws = null;
287
332
  }
333
+ this.cleanupNetworkDetection();
288
334
  this._state = "disconnected";
289
335
  this.emit("disconnected");
290
336
  }
@@ -401,7 +447,7 @@ var MindCache = class {
401
447
  listeners = {};
402
448
  globalListeners = [];
403
449
  // Metadata
404
- version = "3.1.0";
450
+ version = "3.3.2";
405
451
  // Internal flag to prevent sync loops when receiving remote updates
406
452
  // (Less critical with Yjs but kept for API compat)
407
453
  _isRemoteUpdate = false;
@@ -461,8 +507,22 @@ var MindCache = class {
461
507
  constructor(options) {
462
508
  this.doc = new Y.Doc();
463
509
  this.rootMap = this.doc.getMap("mindcache");
464
- this.rootMap.observe((event) => {
465
- event.keysChanged.forEach((key) => {
510
+ this.rootMap.observeDeep((events) => {
511
+ const keysAffected = /* @__PURE__ */ new Set();
512
+ events.forEach((event) => {
513
+ if (event.target === this.rootMap) {
514
+ const mapEvent = event;
515
+ mapEvent.keysChanged.forEach((key) => keysAffected.add(key));
516
+ } else if (event.target.parent === this.rootMap) {
517
+ for (const [key, val] of this.rootMap) {
518
+ if (val === event.target) {
519
+ keysAffected.add(key);
520
+ break;
521
+ }
522
+ }
523
+ }
524
+ });
525
+ keysAffected.forEach((key) => {
466
526
  const entryMap = this.rootMap.get(key);
467
527
  if (entryMap) {
468
528
  const value = entryMap.get("value");
@@ -475,8 +535,6 @@ var MindCache = class {
475
535
  }
476
536
  }
477
537
  });
478
- });
479
- this.rootMap.observeDeep((_events) => {
480
538
  this.notifyGlobalListeners();
481
539
  });
482
540
  this.initGlobalUndoManager();
@@ -744,6 +802,19 @@ var MindCache = class {
744
802
  get isCloud() {
745
803
  return this._cloudConfig !== null;
746
804
  }
805
+ /**
806
+ * Browser network status. Returns true if online or in local-only mode.
807
+ * In cloud mode, this updates instantly when network status changes.
808
+ */
809
+ get isOnline() {
810
+ if (!this._cloudAdapter) {
811
+ if (typeof navigator !== "undefined") {
812
+ return navigator.onLine;
813
+ }
814
+ return true;
815
+ }
816
+ return this._cloudAdapter.isOnline;
817
+ }
747
818
  async waitForSync() {
748
819
  if (this._isLoaded) {
749
820
  return;