@loro-dev/flock 4.2.0 → 4.4.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.d.mts CHANGED
@@ -120,6 +120,10 @@ type EventBatch = {
120
120
  };
121
121
  declare class Flock {
122
122
  private inner;
123
+ private listeners;
124
+ private nativeUnsubscribe;
125
+ /** Debounce state for autoDebounceCommit */
126
+ private debounceState;
123
127
  constructor(peerId?: string);
124
128
  private static fromInner;
125
129
  static fromJson(bundle: ExportBundle, peerId: string): Flock;
@@ -186,7 +190,82 @@ declare class Flock {
186
190
  putMvr(key: KeyPart[], value: Value, now?: number): void;
187
191
  getMvr(key: KeyPart[]): Value[];
188
192
  scan(options?: ScanOptions): ScanRow[];
193
+ private ensureNativeSubscription;
194
+ private handleBatch;
195
+ private emitBatch;
196
+ private resetDebounceTimer;
189
197
  subscribe(listener: (batch: EventBatch) => void): () => void;
198
+ /**
199
+ * Enable auto-debounce mode. Events will be accumulated and emitted after
200
+ * the specified timeout of inactivity. Each new operation resets the timer.
201
+ *
202
+ * Use `commit()` to force immediate emission of pending events.
203
+ * Use `disableAutoDebounceCommit()` to disable and emit pending events.
204
+ *
205
+ * @param timeout - Debounce timeout in milliseconds
206
+ * @param options - Optional configuration object with maxDebounceTime (default: 10000ms)
207
+ * @throws Error if called while a transaction is active
208
+ * @throws Error if autoDebounceCommit is already active
209
+ *
210
+ * @example
211
+ * ```ts
212
+ * flock.autoDebounceCommit(100);
213
+ * flock.put(["a"], 1);
214
+ * flock.put(["b"], 2);
215
+ * // No events emitted yet...
216
+ * // After 100ms of inactivity, subscribers receive single EventBatch
217
+ * // If operations keep coming, commit happens after maxDebounceTime (10s default)
218
+ * ```
219
+ */
220
+ autoDebounceCommit(timeout: number, options?: {
221
+ maxDebounceTime?: number;
222
+ }): void;
223
+ /**
224
+ * Disable auto-debounce mode and emit any pending events immediately.
225
+ * No-op if autoDebounceCommit is not active.
226
+ */
227
+ disableAutoDebounceCommit(): void;
228
+ /**
229
+ * Force immediate emission of any pending debounced events.
230
+ * Does not disable auto-debounce mode - new operations will continue to be debounced.
231
+ * No-op if autoDebounceCommit is not active or no events are pending.
232
+ */
233
+ commit(): void;
234
+ /**
235
+ * Check if auto-debounce mode is currently active.
236
+ */
237
+ isAutoDebounceActive(): boolean;
238
+ /**
239
+ * Execute operations within a transaction. All put/delete operations inside
240
+ * the callback will be batched and emitted as a single EventBatch when the
241
+ * transaction commits successfully.
242
+ *
243
+ * If the callback throws an error, the transaction is rolled back and no
244
+ * events are emitted. Note: Data changes are NOT rolled back - only event
245
+ * emission is affected.
246
+ *
247
+ * The callback must be synchronous. For async operations, use FlockSQLite.
248
+ *
249
+ * @param callback - Synchronous function containing put/delete operations
250
+ * @returns The return value of the callback
251
+ * @throws Error if nested transaction attempted
252
+ * @throws Error if import is called during the transaction (auto-commits first)
253
+ *
254
+ * @example
255
+ * ```ts
256
+ * flock.txn(() => {
257
+ * flock.put(["a"], 1);
258
+ * flock.put(["b"], 2);
259
+ * flock.put(["c"], 3);
260
+ * });
261
+ * // Subscribers receive a single EventBatch with 3 events
262
+ * ```
263
+ */
264
+ txn<T>(callback: () => T): T;
265
+ /**
266
+ * Check if a transaction is currently active.
267
+ */
268
+ isInTxn(): boolean;
190
269
  }
191
270
  //#endregion
192
271
  export { EntryClock, EntryInfo, Event, EventBatch, EventPayload, ExportBundle, ExportHookContext, ExportHooks, ExportPayload, ExportRecord, Flock, ImportAccept, ImportDecision, ImportHookContext, ImportHooks, ImportPayload, ImportReport, ImportSkip, KeyPart, MetadataMap, PutHookContext, PutHooks, PutPayload, PutWithMetaOptions, ScanBound, ScanOptions, ScanRow, Value, VersionVector, VersionVectorEntry, decodeVersionVector, encodeVersionVector };
package/dist/index.d.ts CHANGED
@@ -120,6 +120,10 @@ type EventBatch = {
120
120
  };
121
121
  declare class Flock {
122
122
  private inner;
123
+ private listeners;
124
+ private nativeUnsubscribe;
125
+ /** Debounce state for autoDebounceCommit */
126
+ private debounceState;
123
127
  constructor(peerId?: string);
124
128
  private static fromInner;
125
129
  static fromJson(bundle: ExportBundle, peerId: string): Flock;
@@ -186,7 +190,82 @@ declare class Flock {
186
190
  putMvr(key: KeyPart[], value: Value, now?: number): void;
187
191
  getMvr(key: KeyPart[]): Value[];
188
192
  scan(options?: ScanOptions): ScanRow[];
193
+ private ensureNativeSubscription;
194
+ private handleBatch;
195
+ private emitBatch;
196
+ private resetDebounceTimer;
189
197
  subscribe(listener: (batch: EventBatch) => void): () => void;
198
+ /**
199
+ * Enable auto-debounce mode. Events will be accumulated and emitted after
200
+ * the specified timeout of inactivity. Each new operation resets the timer.
201
+ *
202
+ * Use `commit()` to force immediate emission of pending events.
203
+ * Use `disableAutoDebounceCommit()` to disable and emit pending events.
204
+ *
205
+ * @param timeout - Debounce timeout in milliseconds
206
+ * @param options - Optional configuration object with maxDebounceTime (default: 10000ms)
207
+ * @throws Error if called while a transaction is active
208
+ * @throws Error if autoDebounceCommit is already active
209
+ *
210
+ * @example
211
+ * ```ts
212
+ * flock.autoDebounceCommit(100);
213
+ * flock.put(["a"], 1);
214
+ * flock.put(["b"], 2);
215
+ * // No events emitted yet...
216
+ * // After 100ms of inactivity, subscribers receive single EventBatch
217
+ * // If operations keep coming, commit happens after maxDebounceTime (10s default)
218
+ * ```
219
+ */
220
+ autoDebounceCommit(timeout: number, options?: {
221
+ maxDebounceTime?: number;
222
+ }): void;
223
+ /**
224
+ * Disable auto-debounce mode and emit any pending events immediately.
225
+ * No-op if autoDebounceCommit is not active.
226
+ */
227
+ disableAutoDebounceCommit(): void;
228
+ /**
229
+ * Force immediate emission of any pending debounced events.
230
+ * Does not disable auto-debounce mode - new operations will continue to be debounced.
231
+ * No-op if autoDebounceCommit is not active or no events are pending.
232
+ */
233
+ commit(): void;
234
+ /**
235
+ * Check if auto-debounce mode is currently active.
236
+ */
237
+ isAutoDebounceActive(): boolean;
238
+ /**
239
+ * Execute operations within a transaction. All put/delete operations inside
240
+ * the callback will be batched and emitted as a single EventBatch when the
241
+ * transaction commits successfully.
242
+ *
243
+ * If the callback throws an error, the transaction is rolled back and no
244
+ * events are emitted. Note: Data changes are NOT rolled back - only event
245
+ * emission is affected.
246
+ *
247
+ * The callback must be synchronous. For async operations, use FlockSQLite.
248
+ *
249
+ * @param callback - Synchronous function containing put/delete operations
250
+ * @returns The return value of the callback
251
+ * @throws Error if nested transaction attempted
252
+ * @throws Error if import is called during the transaction (auto-commits first)
253
+ *
254
+ * @example
255
+ * ```ts
256
+ * flock.txn(() => {
257
+ * flock.put(["a"], 1);
258
+ * flock.put(["b"], 2);
259
+ * flock.put(["c"], 3);
260
+ * });
261
+ * // Subscribers receive a single EventBatch with 3 events
262
+ * ```
263
+ */
264
+ txn<T>(callback: () => T): T;
265
+ /**
266
+ * Check if a transaction is currently active.
267
+ */
268
+ isInTxn(): boolean;
190
269
  }
191
270
  //#endregion
192
271
  export { EntryClock, EntryInfo, Event, EventBatch, EventPayload, ExportBundle, ExportHookContext, ExportHooks, ExportPayload, ExportRecord, Flock, ImportAccept, ImportDecision, ImportHookContext, ImportHooks, ImportPayload, ImportReport, ImportSkip, KeyPart, MetadataMap, PutHookContext, PutHooks, PutPayload, PutWithMetaOptions, ScanBound, ScanOptions, ScanRow, Value, VersionVector, VersionVectorEntry, decodeVersionVector, encodeVersionVector };