@loro-dev/flock-wasm 0.1.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.
Files changed (42) hide show
  1. package/LICENSE +661 -0
  2. package/README.md +26 -0
  3. package/base64/event-batcher.d.ts +37 -0
  4. package/base64/event-batcher.js +204 -0
  5. package/base64/flock_wasm.js +1158 -0
  6. package/base64/index.d.ts +268 -0
  7. package/base64/index.js +1012 -0
  8. package/base64/wasm.d.ts +37 -0
  9. package/base64/wasm.js +15 -0
  10. package/bundler/event-batcher.d.ts +37 -0
  11. package/bundler/event-batcher.js +204 -0
  12. package/bundler/flock_wasm.d.ts +313 -0
  13. package/bundler/flock_wasm.js +5 -0
  14. package/bundler/flock_wasm_bg.js +1119 -0
  15. package/bundler/flock_wasm_bg.wasm +0 -0
  16. package/bundler/flock_wasm_bg.wasm.d.ts +40 -0
  17. package/bundler/index.d.ts +268 -0
  18. package/bundler/index.js +1012 -0
  19. package/bundler/wasm.d.ts +37 -0
  20. package/bundler/wasm.js +4 -0
  21. package/nodejs/event-batcher.d.ts +37 -0
  22. package/nodejs/event-batcher.js +208 -0
  23. package/nodejs/flock_wasm.d.ts +313 -0
  24. package/nodejs/flock_wasm.js +1126 -0
  25. package/nodejs/flock_wasm_bg.wasm +0 -0
  26. package/nodejs/flock_wasm_bg.wasm.d.ts +40 -0
  27. package/nodejs/index.d.ts +268 -0
  28. package/nodejs/index.js +1018 -0
  29. package/nodejs/package.json +1 -0
  30. package/nodejs/wasm.d.ts +37 -0
  31. package/nodejs/wasm.js +2 -0
  32. package/package.json +50 -0
  33. package/web/event-batcher.d.ts +37 -0
  34. package/web/event-batcher.js +204 -0
  35. package/web/flock_wasm.d.ts +377 -0
  36. package/web/flock_wasm.js +1158 -0
  37. package/web/flock_wasm_bg.wasm +0 -0
  38. package/web/flock_wasm_bg.wasm.d.ts +40 -0
  39. package/web/index.d.ts +268 -0
  40. package/web/index.js +1012 -0
  41. package/web/wasm.d.ts +37 -0
  42. package/web/wasm.js +4 -0
@@ -0,0 +1,268 @@
1
+ type MaybePromise<T> = T | Promise<T>;
2
+ type ExportOptions = {
3
+ from?: VersionVector;
4
+ hooks?: ExportHooks;
5
+ pruneTombstonesBefore?: number;
6
+ peerId?: string;
7
+ };
8
+ type ImportOptions = {
9
+ bundle: ExportBundle;
10
+ hooks?: ImportHooks;
11
+ };
12
+ export type VersionVectorEntry = {
13
+ physicalTime: number;
14
+ logicalCounter: number;
15
+ };
16
+ export interface VersionVector {
17
+ [peer: string]: VersionVectorEntry | undefined;
18
+ }
19
+ export declare function encodeVersionVector(vector: VersionVector): Uint8Array;
20
+ export declare function decodeVersionVector(bytes: Uint8Array): VersionVector;
21
+ export type Value = string | number | boolean | null | Array<Value> | {
22
+ [key: string]: Value;
23
+ };
24
+ export type KeyPart = Value;
25
+ export type MetadataMap = Record<string, unknown>;
26
+ export type ExportRecord = {
27
+ c: string;
28
+ d?: Value;
29
+ m?: MetadataMap;
30
+ };
31
+ export type ExportBundle = {
32
+ version: number;
33
+ entries: Record<string, ExportRecord>;
34
+ };
35
+ export type EntryClock = {
36
+ physicalTime: number;
37
+ logicalCounter: number;
38
+ peerId: string;
39
+ };
40
+ export type EntryInfo = {
41
+ data?: Value;
42
+ metadata: MetadataMap;
43
+ clock: EntryClock;
44
+ };
45
+ export type ExportPayload = {
46
+ data?: Value;
47
+ metadata?: MetadataMap;
48
+ };
49
+ export type ExportHookContext = {
50
+ key: KeyPart[];
51
+ clock: EntryClock;
52
+ raw: ExportRecord;
53
+ };
54
+ export type ExportHooks = {
55
+ transform?: (context: ExportHookContext, payload: ExportPayload) => MaybePromise<ExportPayload | void>;
56
+ };
57
+ export type ImportPayload = ExportPayload;
58
+ export type ImportHookContext = ExportHookContext;
59
+ export type ImportAccept = {
60
+ accept: true;
61
+ };
62
+ export type ImportSkip = {
63
+ accept: false;
64
+ reason: string;
65
+ };
66
+ export type ImportDecision = ImportAccept | ImportSkip | ImportPayload | void;
67
+ export type ImportHooks = {
68
+ preprocess?: (context: ImportHookContext, payload: ImportPayload) => MaybePromise<ImportDecision>;
69
+ };
70
+ export type ImportReport = {
71
+ accepted: number;
72
+ skipped: Array<{
73
+ key: KeyPart[];
74
+ reason: string;
75
+ }>;
76
+ };
77
+ export type PutPayload = ExportPayload;
78
+ export type PutHookContext = {
79
+ key: KeyPart[];
80
+ now?: number;
81
+ };
82
+ export type PutHooks = {
83
+ transform?: (context: PutHookContext, payload: PutPayload) => MaybePromise<PutPayload | void>;
84
+ };
85
+ export type PutWithMetaOptions = {
86
+ metadata?: MetadataMap;
87
+ now?: number;
88
+ hooks?: PutHooks;
89
+ };
90
+ export type ScanBound = {
91
+ kind: "inclusive";
92
+ key: KeyPart[];
93
+ } | {
94
+ kind: "exclusive";
95
+ key: KeyPart[];
96
+ } | {
97
+ kind: "unbounded";
98
+ };
99
+ export type ScanOptions = {
100
+ start?: ScanBound;
101
+ end?: ScanBound;
102
+ prefix?: KeyPart[];
103
+ };
104
+ export type ScanRow = {
105
+ key: KeyPart[];
106
+ raw: ExportRecord;
107
+ value?: Value;
108
+ };
109
+ export type EventPayload = ExportPayload;
110
+ export type Event = {
111
+ key: KeyPart[];
112
+ value?: Value;
113
+ metadata?: MetadataMap;
114
+ payload: EventPayload;
115
+ };
116
+ export type EventBatch = {
117
+ source: string;
118
+ events: Event[];
119
+ };
120
+ export declare class Flock {
121
+ private inner;
122
+ private listeners;
123
+ private nativeSubscriberId;
124
+ private eventBatcher;
125
+ constructor(peerId?: string);
126
+ private static fromInner;
127
+ static fromJson(bundle: ExportBundle, peerId: string): Flock;
128
+ static fromFile(bytes: Uint8Array, peerId?: string): Flock;
129
+ checkInvariants(): void;
130
+ setPeerId(peerId: string): void;
131
+ private putWithMetaInternal;
132
+ private putWithMetaWithHooks;
133
+ /**
134
+ * Put a value into the flock. If the given entry already exists, this insert will be skipped.
135
+ * @param key
136
+ * @param value
137
+ * @param now
138
+ */
139
+ put(key: KeyPart[], value: Value, now?: number): void;
140
+ putWithMeta(key: KeyPart[], value: Value, options?: PutWithMetaOptions): void | Promise<void>;
141
+ set(key: KeyPart[], value: Value, now?: number): void;
142
+ /**
143
+ * Delete a value from the flock. If the given entry does not exist, this delete will be skipped.
144
+ * @param key
145
+ * @param now
146
+ */
147
+ delete(key: KeyPart[], now?: number): void;
148
+ get(key: KeyPart[]): Value | undefined;
149
+ /**
150
+ * Returns the full entry payload (data, metadata, and clock) for a key.
151
+ *
152
+ * Unlike `get`, this distinguishes between a missing key (`undefined`) and a
153
+ * tombstone (returns the clock and metadata with `data` omitted). Metadata is
154
+ * cloned and defaults to `{}` when absent.
155
+ */
156
+ getEntry(key: KeyPart[]): EntryInfo | undefined;
157
+ merge(other: Flock): void;
158
+ /**
159
+ * Returns the exclusive version vector, which only includes peers that have
160
+ * at least one entry in the current state. This is consistent with the state
161
+ * after export and re-import.
162
+ *
163
+ * Use this version when sending to other peers for incremental sync.
164
+ */
165
+ version(): VersionVector;
166
+ /**
167
+ * Returns the inclusive version vector, which includes all peers ever seen,
168
+ * even if their entries have been overridden by other peers.
169
+ *
170
+ * Use this version when checking if you have received all data from another peer.
171
+ */
172
+ inclusiveVersion(): VersionVector;
173
+ private exportJsonInternal;
174
+ private exportJsonWithHooks;
175
+ exportJson(): ExportBundle;
176
+ exportJson(from: VersionVector): ExportBundle;
177
+ exportJson(from: VersionVector, pruneTombstonesBefore: number): ExportBundle;
178
+ exportJson(options: ExportOptions): Promise<ExportBundle>;
179
+ private importJsonInternal;
180
+ private importJsonWithHooks;
181
+ importJson(bundle: ExportBundle): ImportReport;
182
+ importJson(options: ImportOptions): Promise<ImportReport>;
183
+ importJsonStr(bundle: string): ImportReport;
184
+ getMaxPhysicalTime(): number;
185
+ peerId(): string;
186
+ kvToJson(): ExportBundle;
187
+ putMvr(key: KeyPart[], value: Value, now?: number): void;
188
+ getMvr(key: KeyPart[]): Value[];
189
+ exportFile(): Uint8Array;
190
+ scan(options?: ScanOptions): ScanRow[];
191
+ private ensureNativeSubscription;
192
+ private handleBatch;
193
+ private deliverBatch;
194
+ subscribe(listener: (batch: EventBatch) => void): () => void;
195
+ /**
196
+ * Enable auto-debounce mode. Events will be accumulated and emitted after
197
+ * the specified timeout of inactivity. Each new operation resets the timer.
198
+ *
199
+ * Use `commit()` to force immediate emission of pending events.
200
+ * Use `disableAutoDebounceCommit()` to disable and emit pending events.
201
+ *
202
+ * @param timeout - Debounce timeout in milliseconds
203
+ * @param options - Optional configuration object with maxDebounceTime (default: 10000ms)
204
+ * @throws Error if called while a transaction is active
205
+ * @throws Error if autoDebounceCommit is already active
206
+ *
207
+ * @example
208
+ * ```ts
209
+ * flock.autoDebounceCommit(100);
210
+ * flock.put(["a"], 1);
211
+ * flock.put(["b"], 2);
212
+ * // No events emitted yet...
213
+ * // After 100ms of inactivity, subscribers receive single EventBatch
214
+ * // If operations keep coming, commit happens after maxDebounceTime (10s default)
215
+ * ```
216
+ */
217
+ autoDebounceCommit(timeout: number, options?: {
218
+ maxDebounceTime?: number;
219
+ }): void;
220
+ /**
221
+ * Disable auto-debounce mode and emit any pending events immediately.
222
+ * No-op if autoDebounceCommit is not active.
223
+ */
224
+ disableAutoDebounceCommit(): void;
225
+ /**
226
+ * Force immediate emission of any pending debounced events.
227
+ * Does not disable auto-debounce mode - new operations will continue to be debounced.
228
+ * No-op if autoDebounceCommit is not active or no events are pending.
229
+ */
230
+ commit(): void;
231
+ /**
232
+ * Check if auto-debounce mode is currently active.
233
+ */
234
+ isAutoDebounceActive(): boolean;
235
+ /**
236
+ * Execute operations within a transaction. All put/delete operations inside
237
+ * the callback will be batched and emitted as a single EventBatch when the
238
+ * transaction commits successfully.
239
+ *
240
+ * If the callback throws an error, the transaction is rolled back and no
241
+ * events are emitted. Note: Data changes are NOT rolled back - only event
242
+ * emission is affected.
243
+ *
244
+ * The callback must be synchronous. For async operations, use FlockSQLite.
245
+ *
246
+ * @param callback - Synchronous function containing put/delete operations
247
+ * @returns The return value of the callback
248
+ * @throws Error if nested transaction attempted
249
+ * @throws Error if import is called during the transaction (auto-commits first)
250
+ * @throws Error if called while autoDebounceCommit is active
251
+ *
252
+ * @example
253
+ * ```ts
254
+ * flock.txn(() => {
255
+ * flock.put(["a"], 1);
256
+ * flock.put(["b"], 2);
257
+ * flock.put(["c"], 3);
258
+ * });
259
+ * // Subscribers receive a single EventBatch with 3 events
260
+ * ```
261
+ */
262
+ txn<T>(callback: () => T): T;
263
+ /**
264
+ * Check if a transaction is currently active.
265
+ */
266
+ isInTxn(): boolean;
267
+ }
268
+ export {};