@shaxpir/duiduidui-models 1.9.2 → 1.9.4

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.
@@ -27,9 +27,27 @@ export interface ShareSyncOptions {
27
27
  }
28
28
  export declare class ShareSyncFactory {
29
29
  private static INSTANCE;
30
+ private static TEST_INSTANCE;
30
31
  static initialize(encryption: Encryption, options: ShareSyncOptions): void;
32
+ static isInitialized(): boolean;
31
33
  static get(): ShareSync;
32
34
  static shutdown(): void;
35
+ /**
36
+ * Set a test instance that will be returned by get() instead of the real instance.
37
+ * This allows tests to inject mock/configured ShareSync instances.
38
+ *
39
+ * @param instance The ShareSync instance to use for testing
40
+ */
41
+ static setTestInstance(instance: ShareSync): void;
42
+ /**
43
+ * Clear the test instance, reverting to normal behavior.
44
+ */
45
+ static clearTestInstance(): void;
46
+ /**
47
+ * Reset all state for testing. Clears both production and test instances.
48
+ * Does NOT call shutdown() to avoid side effects - just clears the references.
49
+ */
50
+ static resetForTesting(): void;
33
51
  }
34
52
  export declare class ShareSync {
35
53
  private _debug;
@@ -64,7 +64,14 @@ class ShareSyncFactory {
64
64
  static initialize(encryption, options) {
65
65
  ShareSyncFactory.INSTANCE = new ShareSync(encryption, options);
66
66
  }
67
+ static isInitialized() {
68
+ return ShareSyncFactory.INSTANCE !== null || ShareSyncFactory.TEST_INSTANCE !== null;
69
+ }
67
70
  static get() {
71
+ // Test instance takes precedence
72
+ if (ShareSyncFactory.TEST_INSTANCE !== null) {
73
+ return ShareSyncFactory.TEST_INSTANCE;
74
+ }
68
75
  return ShareSyncFactory.INSTANCE;
69
76
  }
70
77
  static shutdown() {
@@ -73,9 +80,37 @@ class ShareSyncFactory {
73
80
  ShareSyncFactory.INSTANCE = null;
74
81
  }
75
82
  }
83
+ // ============================================================
84
+ // Test helper methods
85
+ // ============================================================
86
+ /**
87
+ * Set a test instance that will be returned by get() instead of the real instance.
88
+ * This allows tests to inject mock/configured ShareSync instances.
89
+ *
90
+ * @param instance The ShareSync instance to use for testing
91
+ */
92
+ static setTestInstance(instance) {
93
+ ShareSyncFactory.TEST_INSTANCE = instance;
94
+ }
95
+ /**
96
+ * Clear the test instance, reverting to normal behavior.
97
+ */
98
+ static clearTestInstance() {
99
+ ShareSyncFactory.TEST_INSTANCE = null;
100
+ }
101
+ /**
102
+ * Reset all state for testing. Clears both production and test instances.
103
+ * Does NOT call shutdown() to avoid side effects - just clears the references.
104
+ */
105
+ static resetForTesting() {
106
+ ShareSyncFactory.INSTANCE = null;
107
+ ShareSyncFactory.TEST_INSTANCE = null;
108
+ }
76
109
  }
77
110
  exports.ShareSyncFactory = ShareSyncFactory;
78
111
  ShareSyncFactory.INSTANCE = null;
112
+ // For tests: allow injecting a custom instance
113
+ ShareSyncFactory.TEST_INSTANCE = null;
79
114
  class ShareSync {
80
115
  constructor(encryption, options) {
81
116
  this._modelCache = new Map();
@@ -157,14 +192,12 @@ class ShareSync {
157
192
  }
158
193
  createConnectionWithDurableStore(socket, options) {
159
194
  const shareSync = this;
160
- // Create a wrapper callback that ensures it's called exactly once
161
- // The callback should fire when DurableStore is ready, regardless of socket state
162
- // This enables offline-first operation where local data is available immediately
195
+ // Create a wrapper callback that ensures it's called even when offline
163
196
  let readyCallbackInvoked = false;
164
197
  const wrappedCallback = options.onReadyCallback ? function () {
165
198
  if (!readyCallbackInvoked) {
166
199
  readyCallbackInvoked = true;
167
- shareSync._debug && console.log('[ShareSync] DurableStore ready, invoking onReadyCallback (socket open:', shareSync._socketIsOpen, ')');
200
+ shareSync._debug && console.log('[ShareSync] DurableStore ready, invoking onReadyCallback');
168
201
  options.onReadyCallback();
169
202
  }
170
203
  } : undefined;
@@ -185,16 +218,15 @@ class ShareSync {
185
218
  onReadyCallback: wrappedCallback
186
219
  }
187
220
  });
188
- // The DurableStore's 'ready' event fires when local storage initialization completes.
189
- // This happens regardless of socket/network state, enabling offline-first operation.
190
- // Add a safety timeout in case storage initialization hangs (should never happen).
191
- if (wrappedCallback) {
221
+ // Add a fallback timeout to ensure the callback fires even if the DurableStore
222
+ // ready event doesn't properly trigger when offline
223
+ if (wrappedCallback && !shareSync._socketIsOpen) {
192
224
  setTimeout(function () {
193
225
  if (!readyCallbackInvoked) {
194
- console.warn('[ShareSync] DurableStore ready timeout - forcing callback after 5s');
226
+ console.warn('[ShareSync] Socket offline - forcing onReadyCallback after timeout');
195
227
  wrappedCallback();
196
228
  }
197
- }, 5000); // 5 second safety timeout
229
+ }, 100); // Small delay to allow normal initialization first
198
230
  }
199
231
  return connection;
200
232
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@shaxpir/duiduidui-models",
3
- "version": "1.9.2",
3
+ "version": "1.9.4",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "https://github.com/shaxpir/duiduidui-models"