@loro-dev/flock 4.2.0 → 4.3.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 +2 -2
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.mts +31 -0
- package/dist/index.d.ts +31 -0
- package/dist/index.mjs +2 -2
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
- package/src/_moon_flock.d.ts +8 -0
- package/src/_moon_flock.ts +188 -111
- package/src/index.ts +54 -0
package/src/index.ts
CHANGED
|
@@ -23,6 +23,10 @@ import {
|
|
|
23
23
|
check_consistency_ffi,
|
|
24
24
|
check_invariants_ffi,
|
|
25
25
|
from_json_ffi,
|
|
26
|
+
txn_begin_ffi,
|
|
27
|
+
txn_commit_ffi,
|
|
28
|
+
txn_rollback_ffi,
|
|
29
|
+
is_in_txn_ffi,
|
|
26
30
|
} from "./_moon_flock";
|
|
27
31
|
|
|
28
32
|
type RawVersionVector = Record<string, [number, number]>;
|
|
@@ -1172,4 +1176,54 @@ export class Flock {
|
|
|
1172
1176
|
}
|
|
1173
1177
|
return unsubscribe as () => void;
|
|
1174
1178
|
}
|
|
1179
|
+
|
|
1180
|
+
/**
|
|
1181
|
+
* Execute operations within a transaction. All put/delete operations inside
|
|
1182
|
+
* the callback will be batched and emitted as a single EventBatch when the
|
|
1183
|
+
* transaction commits successfully.
|
|
1184
|
+
*
|
|
1185
|
+
* If the callback throws an error, the transaction is rolled back and no
|
|
1186
|
+
* events are emitted. Note: Data changes are NOT rolled back - only event
|
|
1187
|
+
* emission is affected.
|
|
1188
|
+
*
|
|
1189
|
+
* The callback must be synchronous. For async operations, use FlockSQLite.
|
|
1190
|
+
*
|
|
1191
|
+
* @param callback - Synchronous function containing put/delete operations
|
|
1192
|
+
* @returns The return value of the callback
|
|
1193
|
+
* @throws Error if nested transaction attempted
|
|
1194
|
+
* @throws Error if import is called during the transaction (auto-commits first)
|
|
1195
|
+
*
|
|
1196
|
+
* @example
|
|
1197
|
+
* ```ts
|
|
1198
|
+
* flock.txn(() => {
|
|
1199
|
+
* flock.put(["a"], 1);
|
|
1200
|
+
* flock.put(["b"], 2);
|
|
1201
|
+
* flock.put(["c"], 3);
|
|
1202
|
+
* });
|
|
1203
|
+
* // Subscribers receive a single EventBatch with 3 events
|
|
1204
|
+
* ```
|
|
1205
|
+
*/
|
|
1206
|
+
txn<T>(callback: () => T): T {
|
|
1207
|
+
txn_begin_ffi(this.inner);
|
|
1208
|
+
try {
|
|
1209
|
+
const result = callback();
|
|
1210
|
+
txn_commit_ffi(this.inner);
|
|
1211
|
+
return result;
|
|
1212
|
+
} catch (e) {
|
|
1213
|
+
// Only rollback if transaction is still active.
|
|
1214
|
+
// import_json auto-commits the transaction before throwing,
|
|
1215
|
+
// so we must check before attempting rollback.
|
|
1216
|
+
if (is_in_txn_ffi(this.inner)) {
|
|
1217
|
+
txn_rollback_ffi(this.inner);
|
|
1218
|
+
}
|
|
1219
|
+
throw e;
|
|
1220
|
+
}
|
|
1221
|
+
}
|
|
1222
|
+
|
|
1223
|
+
/**
|
|
1224
|
+
* Check if a transaction is currently active.
|
|
1225
|
+
*/
|
|
1226
|
+
isInTxn(): boolean {
|
|
1227
|
+
return Boolean(is_in_txn_ffi(this.inner));
|
|
1228
|
+
}
|
|
1175
1229
|
}
|