@loro-dev/flock-sqlite 0.5.0 → 0.6.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 +5 -1
- package/dist/index.d.ts +5 -1
- package/dist/index.mjs +2 -2
- package/dist/index.mjs.map +1 -1
- package/package.json +2 -2
- package/src/index.ts +32 -3
package/dist/index.d.mts
CHANGED
|
@@ -259,6 +259,7 @@ declare class FlockSQLite {
|
|
|
259
259
|
* Import operations will automatically call `commit()` before proceeding.
|
|
260
260
|
*
|
|
261
261
|
* @param timeout - Debounce timeout in milliseconds
|
|
262
|
+
* @param options - Optional configuration object with maxDebounceTime (default: 10000ms)
|
|
262
263
|
* @throws Error if called while a transaction is active
|
|
263
264
|
* @throws Error if autoDebounceCommit is already active
|
|
264
265
|
*
|
|
@@ -269,9 +270,12 @@ declare class FlockSQLite {
|
|
|
269
270
|
* await flock.put(["b"], 2);
|
|
270
271
|
* // No events emitted yet...
|
|
271
272
|
* // After 100ms of inactivity, subscribers receive single EventBatch
|
|
273
|
+
* // If operations keep coming, commit happens after maxDebounceTime (10s default)
|
|
272
274
|
* ```
|
|
273
275
|
*/
|
|
274
|
-
autoDebounceCommit(timeout: number
|
|
276
|
+
autoDebounceCommit(timeout: number, options?: {
|
|
277
|
+
maxDebounceTime?: number;
|
|
278
|
+
}): void;
|
|
275
279
|
/**
|
|
276
280
|
* Disable auto-debounce mode and emit any pending events immediately.
|
|
277
281
|
* No-op if autoDebounceCommit is not active.
|
package/dist/index.d.ts
CHANGED
|
@@ -259,6 +259,7 @@ declare class FlockSQLite {
|
|
|
259
259
|
* Import operations will automatically call `commit()` before proceeding.
|
|
260
260
|
*
|
|
261
261
|
* @param timeout - Debounce timeout in milliseconds
|
|
262
|
+
* @param options - Optional configuration object with maxDebounceTime (default: 10000ms)
|
|
262
263
|
* @throws Error if called while a transaction is active
|
|
263
264
|
* @throws Error if autoDebounceCommit is already active
|
|
264
265
|
*
|
|
@@ -269,9 +270,12 @@ declare class FlockSQLite {
|
|
|
269
270
|
* await flock.put(["b"], 2);
|
|
270
271
|
* // No events emitted yet...
|
|
271
272
|
* // After 100ms of inactivity, subscribers receive single EventBatch
|
|
273
|
+
* // If operations keep coming, commit happens after maxDebounceTime (10s default)
|
|
272
274
|
* ```
|
|
273
275
|
*/
|
|
274
|
-
autoDebounceCommit(timeout: number
|
|
276
|
+
autoDebounceCommit(timeout: number, options?: {
|
|
277
|
+
maxDebounceTime?: number;
|
|
278
|
+
}): void;
|
|
275
279
|
/**
|
|
276
280
|
* Disable auto-debounce mode and emit any pending events immediately.
|
|
277
281
|
* No-op if autoDebounceCommit is not active.
|
package/dist/index.mjs
CHANGED
|
@@ -23,9 +23,9 @@ import{openStore as e}from"@loro-dev/unisqlite";const t=new TextEncoder;function
|
|
|
23
23
|
metadata=excluded.metadata,
|
|
24
24
|
physical=excluded.physical,
|
|
25
25
|
logical=excluded.logical,
|
|
26
|
-
peer=excluded.peer`,[t,r,i,c.physicalTime,c.logicalCounter,c.peerId]),a=!0}),o&&this.bumpVersion(o),a){let t={key:e.key.slice(),payload:n,source:e.source};e.eventSink?e.eventSink.push(t):this.txnEventSink?this.txnEventSink.push(t):this.debounceState?(this.debounceState.pendingEvents.push(t),this.resetDebounceTimer()):this.emitEvents(e.source,[t])}return a}resetDebounceTimer(){this.debounceState!==void 0&&(this.debounceState.timerId!==void 0&&clearTimeout(this.debounceState.timerId),this.debounceState.timerId=setTimeout(()=>{this.commit()},this.debounceState.timeout))}emitEvents(e,t){if(this.listeners.size===0||t.length===0)return;let n={source:e,events:t.map(e=>({key:W(e.key),value:e.payload.data===void 0?void 0:W(e.payload.data),metadata:G(e.payload.metadata),payload:K(e.payload)}))};this.listeners.forEach(e=>{e(n)})}async put(e,t,n){await this.applyOperation({key:e,payload:{data:W(t)},now:n,skipSameValue:!0,source:`local`})}async putWithMeta(e,t,n={}){let r={data:W(t)};n.metadata&&(r.metadata=G(n.metadata));let i=n.hooks?.transform;if(i){let t=K(r),a=q(r,await i({key:e.slice(),now:n.now},t)??t);if(a.data===void 0)throw TypeError(`putWithMeta requires a data value`);await this.applyOperation({key:e,payload:a,now:n.now,skipSameValue:!0,source:`local`});return}await this.applyOperation({key:e,payload:r,now:n.now,skipSameValue:!0,source:`local`})}async delete(e,t){await this.applyOperation({key:e,payload:{},now:t,skipSameValue:!0,source:`local`})}async forcePut(e,t,n){await this.applyOperation({key:e,payload:{data:W(t)},now:n,skipSameValue:!1,source:`local`})}async forcePutWithMeta(e,t,n={}){let r={data:W(t)};n.metadata&&(r.metadata=G(n.metadata));let i=n.hooks?.transform;if(i){let t=K(r),a=q(r,await i({key:e.slice(),now:n.now},t)??t);if(a.data===void 0)throw TypeError(`forcePutWithMeta requires a data value`);await this.applyOperation({key:e,payload:a,now:n.now,skipSameValue:!1,source:`local`});return}await this.applyOperation({key:e,payload:r,now:n.now,skipSameValue:!1,source:`local`})}async forceDelete(e,t){await this.applyOperation({key:e,payload:{},now:t,skipSameValue:!1,source:`local`})}async set(e,t,n){await this.put(e,t,n)}async setPeerId(e){let t=rr(e);await this.db.exec(`DELETE FROM ${this.tables.meta}`),await this.db.run(`INSERT INTO ${this.tables.meta}(peer_id) VALUES (?)`,[t]),this.peerIdValue=t}async get(e){let t=H(e),n=(await this.db.query(`SELECT data FROM ${this.tables.kv} WHERE key = ? LIMIT 1`,[t]))[0];if(n)return Q(n.data)}async getEntry(e){let t;try{t=H(e)}catch{return}let n=(await this.db.query(`SELECT data, metadata, physical, logical, peer FROM ${this.tables.kv} WHERE key = ? LIMIT 1`,[t]))[0];if(!n)return;let r=xr(n.physical,n.logical,n.peer);if(!r)return;let i=ir(br(n.metadata)),a=Q(n.data),o={metadata:i,clock:r};return a!==void 0&&(o.data=a),o}async getMvr(e){let t=await this.scan({prefix:e}),n=[];for(let r of t)r.raw.d===!0&&r.key.length===e.length+1&&n.push(r.key[r.key.length-1]);return n}async putMvr(e,t,n){if(t===null||typeof t==`object`)throw TypeError(`putMvr only accepts scalar values`);let r=await this.scan({prefix:e});for(let e of r)e.raw.d===!0&&await this.delete(e.key,n);let i=e.slice();i.push(t),await this.put(i,!0,n)}buildScanBounds(e){let t,n,r,i=e=>{if(!t){t=e;return}let n=Yn(e.value,t.value);n>0?t=e:n===0&&(t={value:t.value,inclusive:t.inclusive&&e.inclusive})},a=e=>{if(!n){n=e;return}let t=Yn(e.value,n.value);t<0?n=e:t===0&&(n={value:n.value,inclusive:n.inclusive&&e.inclusive})};try{if(e.prefix){let t=H(e.prefix);r=t,i({value:t,inclusive:!0});let n=Xn(t);n&&a({value:n,inclusive:!1})}e.start&&e.start.kind!==`unbounded`&&i({value:H(e.start.key),inclusive:e.start.kind===`inclusive`}),e.end&&e.end.kind!==`unbounded`&&a({value:H(e.end.key),inclusive:e.end.kind===`inclusive`})}catch{return{where:[],params:[],empty:!0}}let o=[],s=[];return t&&(o.push(`key ${t.inclusive?`>=`:`>`} ?`),s.push(t.value)),n&&(o.push(`key ${n.inclusive?`<=`:`<`} ?`),s.push(n.value)),{where:o,params:s,postFilter:r?(e=>t=>Tr(t,e))(r):void 0}}async scan(e={}){let t=this.buildScanBounds(e);if(t.empty)return[];let n=t.where.length>0?`WHERE ${t.where.join(` AND `)}`:``,r=await this.db.query(`SELECT key, data, metadata, physical, logical, peer FROM ${this.tables.kv} ${n} ORDER BY key ASC`,t.params),i=[];for(let e of r){let n=e.key;if(t.postFilter&&!t.postFilter(n))continue;let r=Jn(n),a={physicalTime:Number(e.physical??0),logicalCounter:Number(e.logical??0),peerId:String(e.peer??``)},o={},s=Q(e.data);s!==void 0&&(o.data=s);let c=br(e.metadata);c!==void 0&&(o.metadata=c);let l=Z(a,o);i.push({key:r,raw:l,value:o.data})}return i}async version(){let e=await this.db.query(`SELECT peer, physical, logical FROM (
|
|
26
|
+
peer=excluded.peer`,[t,r,i,c.physicalTime,c.logicalCounter,c.peerId]),a=!0}),o&&this.bumpVersion(o),a){let t={key:e.key.slice(),payload:n,source:e.source};e.eventSink?e.eventSink.push(t):this.txnEventSink?this.txnEventSink.push(t):this.debounceState?(this.debounceState.pendingEvents.push(t),this.resetDebounceTimer()):this.emitEvents(e.source,[t])}return a}resetDebounceTimer(){this.debounceState!==void 0&&(this.debounceState.timerId!==void 0&&clearTimeout(this.debounceState.timerId),this.debounceState.timerId=setTimeout(()=>{this.commit()},this.debounceState.timeout),this.debounceState.maxTimerId===void 0&&this.debounceState.pendingEvents.length===1&&(this.debounceState.maxTimerId=setTimeout(()=>{this.commit()},this.debounceState.maxDebounceTime)))}emitEvents(e,t){if(this.listeners.size===0||t.length===0)return;let n={source:e,events:t.map(e=>({key:W(e.key),value:e.payload.data===void 0?void 0:W(e.payload.data),metadata:G(e.payload.metadata),payload:K(e.payload)}))};this.listeners.forEach(e=>{e(n)})}async put(e,t,n){await this.applyOperation({key:e,payload:{data:W(t)},now:n,skipSameValue:!0,source:`local`})}async putWithMeta(e,t,n={}){let r={data:W(t)};n.metadata&&(r.metadata=G(n.metadata));let i=n.hooks?.transform;if(i){let t=K(r),a=q(r,await i({key:e.slice(),now:n.now},t)??t);if(a.data===void 0)throw TypeError(`putWithMeta requires a data value`);await this.applyOperation({key:e,payload:a,now:n.now,skipSameValue:!0,source:`local`});return}await this.applyOperation({key:e,payload:r,now:n.now,skipSameValue:!0,source:`local`})}async delete(e,t){await this.applyOperation({key:e,payload:{},now:t,skipSameValue:!0,source:`local`})}async forcePut(e,t,n){await this.applyOperation({key:e,payload:{data:W(t)},now:n,skipSameValue:!1,source:`local`})}async forcePutWithMeta(e,t,n={}){let r={data:W(t)};n.metadata&&(r.metadata=G(n.metadata));let i=n.hooks?.transform;if(i){let t=K(r),a=q(r,await i({key:e.slice(),now:n.now},t)??t);if(a.data===void 0)throw TypeError(`forcePutWithMeta requires a data value`);await this.applyOperation({key:e,payload:a,now:n.now,skipSameValue:!1,source:`local`});return}await this.applyOperation({key:e,payload:r,now:n.now,skipSameValue:!1,source:`local`})}async forceDelete(e,t){await this.applyOperation({key:e,payload:{},now:t,skipSameValue:!1,source:`local`})}async set(e,t,n){await this.put(e,t,n)}async setPeerId(e){let t=rr(e);await this.db.exec(`DELETE FROM ${this.tables.meta}`),await this.db.run(`INSERT INTO ${this.tables.meta}(peer_id) VALUES (?)`,[t]),this.peerIdValue=t}async get(e){let t=H(e),n=(await this.db.query(`SELECT data FROM ${this.tables.kv} WHERE key = ? LIMIT 1`,[t]))[0];if(n)return Q(n.data)}async getEntry(e){let t;try{t=H(e)}catch{return}let n=(await this.db.query(`SELECT data, metadata, physical, logical, peer FROM ${this.tables.kv} WHERE key = ? LIMIT 1`,[t]))[0];if(!n)return;let r=xr(n.physical,n.logical,n.peer);if(!r)return;let i=ir(br(n.metadata)),a=Q(n.data),o={metadata:i,clock:r};return a!==void 0&&(o.data=a),o}async getMvr(e){let t=await this.scan({prefix:e}),n=[];for(let r of t)r.raw.d===!0&&r.key.length===e.length+1&&n.push(r.key[r.key.length-1]);return n}async putMvr(e,t,n){if(t===null||typeof t==`object`)throw TypeError(`putMvr only accepts scalar values`);let r=await this.scan({prefix:e});for(let e of r)e.raw.d===!0&&await this.delete(e.key,n);let i=e.slice();i.push(t),await this.put(i,!0,n)}buildScanBounds(e){let t,n,r,i=e=>{if(!t){t=e;return}let n=Yn(e.value,t.value);n>0?t=e:n===0&&(t={value:t.value,inclusive:t.inclusive&&e.inclusive})},a=e=>{if(!n){n=e;return}let t=Yn(e.value,n.value);t<0?n=e:t===0&&(n={value:n.value,inclusive:n.inclusive&&e.inclusive})};try{if(e.prefix){let t=H(e.prefix);r=t,i({value:t,inclusive:!0});let n=Xn(t);n&&a({value:n,inclusive:!1})}e.start&&e.start.kind!==`unbounded`&&i({value:H(e.start.key),inclusive:e.start.kind===`inclusive`}),e.end&&e.end.kind!==`unbounded`&&a({value:H(e.end.key),inclusive:e.end.kind===`inclusive`})}catch{return{where:[],params:[],empty:!0}}let o=[],s=[];return t&&(o.push(`key ${t.inclusive?`>=`:`>`} ?`),s.push(t.value)),n&&(o.push(`key ${n.inclusive?`<=`:`<`} ?`),s.push(n.value)),{where:o,params:s,postFilter:r?(e=>t=>Tr(t,e))(r):void 0}}async scan(e={}){let t=this.buildScanBounds(e);if(t.empty)return[];let n=t.where.length>0?`WHERE ${t.where.join(` AND `)}`:``,r=await this.db.query(`SELECT key, data, metadata, physical, logical, peer FROM ${this.tables.kv} ${n} ORDER BY key ASC`,t.params),i=[];for(let e of r){let n=e.key;if(t.postFilter&&!t.postFilter(n))continue;let r=Jn(n),a={physicalTime:Number(e.physical??0),logicalCounter:Number(e.logical??0),peerId:String(e.peer??``)},o={},s=Q(e.data);s!==void 0&&(o.data=s);let c=br(e.metadata);c!==void 0&&(o.metadata=c);let l=Z(a,o);i.push({key:r,raw:l,value:o.data})}return i}async version(){let e=await this.db.query(`SELECT peer, physical, logical FROM (
|
|
27
27
|
SELECT peer, physical, logical,
|
|
28
28
|
ROW_NUMBER() OVER (PARTITION BY peer ORDER BY physical DESC, logical DESC) as rn
|
|
29
29
|
FROM ${this.tables.kv}
|
|
30
|
-
) WHERE rn = 1`),t={};for(let n of e)t[n.peer]={physicalTime:n.physical,logicalCounter:n.logical};return t}inclusiveVersion(){let e={};for(let[t,n]of this.vv.entries())e[t]={...n};return e}peerId(){return this.peerIdValue}getMaxPhysicalTime(){return this.maxHlc.physicalTime}async exportInternal(e,t,n){let r=new Map;if(e)for(let[t,n]of Object.entries(e)){let e=wr(n);e&&r.set(t,e)}let i={},a=n?[n]:Array.from(this.vv.keys()),o=[];for(let e of a){let t=this.vv.get(e),n=r.get(e);t&&n&&(t.physicalTime<n.physicalTime||t.physicalTime===n.physicalTime&&t.logicalCounter<=n.logicalCounter)||o.push({peer:e,fromEntry:n})}if(n&&o.every(e=>e.peer!==n)&&o.push({peer:n,fromEntry:r.get(n)}),o.length===0)return{version:0,entries:i};for(let e of o){let n=[e.peer],r=`WHERE peer = ?`;e.fromEntry&&(r+=` AND physical >= ?`,n.push(e.fromEntry.physicalTime));let a=await this.db.query(`SELECT key, data, metadata, physical, logical, peer FROM ${this.tables.kv} ${r} ORDER BY physical, logical, key`,n);for(let n of a){let r={physicalTime:Number(n.physical??0),logicalCounter:Number(n.logical??0),peerId:String(n.peer??``)},a=e.fromEntry;if(a&&(r.physicalTime<a.physicalTime||r.physicalTime===a.physicalTime&&r.logicalCounter<=a.logicalCounter)||t!==void 0&&Number.isFinite(t)&&n.data===null&&r.physicalTime<t)continue;let o={},s=Q(n.data);s!==void 0&&(o.data=s);let c=br(n.metadata);c!==void 0&&(o.metadata=c);let l=Jn(n.key);i[Zn(l)]=Z(r,o)}}return{version:0,entries:i}}async exportWithHooks(e){let t=await this.exportInternal(e.from,e.pruneTombstonesBefore,e.peerId),n=e.hooks?.transform;if(!n)return t;let r={version:t.version,entries:{}};for(let[e,i]of Object.entries(t.entries)){let t=$(i.c),a={key:JSON.parse(e),clock:t,raw:{...i}},o={};i.d!==void 0&&(o.data=W(i.d));let s=G(i.m);s!==void 0&&(o.metadata=s);let c=K(o),l=q(o,await n(a,c)??c);r.entries[e]=Z(t,l)}return r}exportJson(e,t){return vr(e)?this.exportWithHooks(e):this.exportInternal(e,t)}async importInternal(e){if(this.txnEventSink!==void 0){let e=this.txnEventSink;throw this.txnEventSink=void 0,e.length>0&&this.emitEvents(`local`,e),Error(`import called during transaction - transaction was auto-committed`)}if(this.debounceState!==void 0&&this.commit(),e.version!==0)throw TypeError(`Unsupported bundle version`);let t=0,n=[],r=[];for(let[i,a]of Object.entries(e.entries)){let e;try{let t=JSON.parse(i);e=Array.isArray(t)?t:[]}catch{n.push({key:[],reason:`invalid key`});continue}let o=$(a.c),s={};a.d!==void 0&&(s.data=W(a.d));let c=G(a.m);c!==void 0&&(s.metadata=c),t+=1,await this.applyOperation({key:e,payload:s,clock:o,skipSameValue:!1,source:`import`,eventSink:r})}return r.length>0&&this.emitEvents(`import`,r),{accepted:t,skipped:n}}async importJson(e){if(yr(e)){let t=e.hooks?.preprocess,n=t?{version:e.bundle.version,entries:{...e.bundle.entries}}:e.bundle,r=[];if(t)for(let[e,i]of Object.entries(n.entries)){let a=JSON.parse(e),o=$(i.c),s={};i.d!==void 0&&(s.data=W(i.d));let c=G(i.m);c!==void 0&&(s.metadata=c);let l=_r(await t({key:a,clock:o,raw:i},K(s)));l.accept?n.entries[e]=Z(o,s):(r.push({key:a,reason:l.reason??`rejected`}),delete n.entries[e])}let i=await this.importInternal(n);return{accepted:i.accepted,skipped:r.concat(i.skipped)}}return this.importInternal(e)}async importJsonStr(e){let t=JSON.parse(e);return this.importJson(t)}async digest(){return u(await Er(await this.db.query(`SELECT key, data, metadata, physical, logical, peer FROM ${this.tables.kv} ORDER BY key ASC`)))}async kvToJson(){return this.exportInternal()}async merge(e){let t=await e.exportJson();await this.importJson(t)}static async checkConsistency(e,t){let[n,r]=await Promise.all([e.digest(),t.digest()]);return n===r}checkInvariants(){}subscribe(e){return this.listeners.add(e),()=>{this.listeners.delete(e)}}async txn(e){if(this.txnEventSink!==void 0)throw Error(`Nested transactions are not supported`);if(this.debounceState!==void 0)throw Error(`Cannot start transaction while autoDebounceCommit is active`);let t=[];this.txnEventSink=t;try{let n=await e();return t.length>0&&this.emitEvents(`local`,t),n}finally{this.txnEventSink=void 0}}isInTxn(){return this.txnEventSink!==void 0}autoDebounceCommit(e){if(this.txnEventSink!==void 0)throw Error(`Cannot enable autoDebounceCommit while transaction is active`);if(this.debounceState!==void 0)throw Error(`autoDebounceCommit is already active`);this.debounceState={timeout:e,timerId:void 0,pendingEvents:[]}}disableAutoDebounceCommit(){if(this.debounceState===void 0)return;let{timerId:e,pendingEvents:
|
|
30
|
+
) WHERE rn = 1`),t={};for(let n of e)t[n.peer]={physicalTime:n.physical,logicalCounter:n.logical};return t}inclusiveVersion(){let e={};for(let[t,n]of this.vv.entries())e[t]={...n};return e}peerId(){return this.peerIdValue}getMaxPhysicalTime(){return this.maxHlc.physicalTime}async exportInternal(e,t,n){let r=new Map;if(e)for(let[t,n]of Object.entries(e)){let e=wr(n);e&&r.set(t,e)}let i={},a=n?[n]:Array.from(this.vv.keys()),o=[];for(let e of a){let t=this.vv.get(e),n=r.get(e);t&&n&&(t.physicalTime<n.physicalTime||t.physicalTime===n.physicalTime&&t.logicalCounter<=n.logicalCounter)||o.push({peer:e,fromEntry:n})}if(n&&o.every(e=>e.peer!==n)&&o.push({peer:n,fromEntry:r.get(n)}),o.length===0)return{version:0,entries:i};for(let e of o){let n=[e.peer],r=`WHERE peer = ?`;e.fromEntry&&(r+=` AND physical >= ?`,n.push(e.fromEntry.physicalTime));let a=await this.db.query(`SELECT key, data, metadata, physical, logical, peer FROM ${this.tables.kv} ${r} ORDER BY physical, logical, key`,n);for(let n of a){let r={physicalTime:Number(n.physical??0),logicalCounter:Number(n.logical??0),peerId:String(n.peer??``)},a=e.fromEntry;if(a&&(r.physicalTime<a.physicalTime||r.physicalTime===a.physicalTime&&r.logicalCounter<=a.logicalCounter)||t!==void 0&&Number.isFinite(t)&&n.data===null&&r.physicalTime<t)continue;let o={},s=Q(n.data);s!==void 0&&(o.data=s);let c=br(n.metadata);c!==void 0&&(o.metadata=c);let l=Jn(n.key);i[Zn(l)]=Z(r,o)}}return{version:0,entries:i}}async exportWithHooks(e){let t=await this.exportInternal(e.from,e.pruneTombstonesBefore,e.peerId),n=e.hooks?.transform;if(!n)return t;let r={version:t.version,entries:{}};for(let[e,i]of Object.entries(t.entries)){let t=$(i.c),a={key:JSON.parse(e),clock:t,raw:{...i}},o={};i.d!==void 0&&(o.data=W(i.d));let s=G(i.m);s!==void 0&&(o.metadata=s);let c=K(o),l=q(o,await n(a,c)??c);r.entries[e]=Z(t,l)}return r}exportJson(e,t){return vr(e)?this.exportWithHooks(e):this.exportInternal(e,t)}async importInternal(e){if(this.txnEventSink!==void 0){let e=this.txnEventSink;throw this.txnEventSink=void 0,e.length>0&&this.emitEvents(`local`,e),Error(`import called during transaction - transaction was auto-committed`)}if(this.debounceState!==void 0&&this.commit(),e.version!==0)throw TypeError(`Unsupported bundle version`);let t=0,n=[],r=[];for(let[i,a]of Object.entries(e.entries)){let e;try{let t=JSON.parse(i);e=Array.isArray(t)?t:[]}catch{n.push({key:[],reason:`invalid key`});continue}let o=$(a.c),s={};a.d!==void 0&&(s.data=W(a.d));let c=G(a.m);c!==void 0&&(s.metadata=c),t+=1,await this.applyOperation({key:e,payload:s,clock:o,skipSameValue:!1,source:`import`,eventSink:r})}return r.length>0&&this.emitEvents(`import`,r),{accepted:t,skipped:n}}async importJson(e){if(yr(e)){let t=e.hooks?.preprocess,n=t?{version:e.bundle.version,entries:{...e.bundle.entries}}:e.bundle,r=[];if(t)for(let[e,i]of Object.entries(n.entries)){let a=JSON.parse(e),o=$(i.c),s={};i.d!==void 0&&(s.data=W(i.d));let c=G(i.m);c!==void 0&&(s.metadata=c);let l=_r(await t({key:a,clock:o,raw:i},K(s)));l.accept?n.entries[e]=Z(o,s):(r.push({key:a,reason:l.reason??`rejected`}),delete n.entries[e])}let i=await this.importInternal(n);return{accepted:i.accepted,skipped:r.concat(i.skipped)}}return this.importInternal(e)}async importJsonStr(e){let t=JSON.parse(e);return this.importJson(t)}async digest(){return u(await Er(await this.db.query(`SELECT key, data, metadata, physical, logical, peer FROM ${this.tables.kv} ORDER BY key ASC`)))}async kvToJson(){return this.exportInternal()}async merge(e){let t=await e.exportJson();await this.importJson(t)}static async checkConsistency(e,t){let[n,r]=await Promise.all([e.digest(),t.digest()]);return n===r}checkInvariants(){}subscribe(e){return this.listeners.add(e),()=>{this.listeners.delete(e)}}async txn(e){if(this.txnEventSink!==void 0)throw Error(`Nested transactions are not supported`);if(this.debounceState!==void 0)throw Error(`Cannot start transaction while autoDebounceCommit is active`);let t=[];this.txnEventSink=t;try{let n=await e();return t.length>0&&this.emitEvents(`local`,t),n}finally{this.txnEventSink=void 0}}isInTxn(){return this.txnEventSink!==void 0}autoDebounceCommit(e,t){if(this.txnEventSink!==void 0)throw Error(`Cannot enable autoDebounceCommit while transaction is active`);if(this.debounceState!==void 0)throw Error(`autoDebounceCommit is already active`);this.debounceState={timeout:e,maxDebounceTime:t?.maxDebounceTime??1e4,timerId:void 0,maxTimerId:void 0,pendingEvents:[]}}disableAutoDebounceCommit(){if(this.debounceState===void 0)return;let{timerId:e,maxTimerId:t,pendingEvents:n}=this.debounceState;e!==void 0&&clearTimeout(e),t!==void 0&&clearTimeout(t),this.debounceState=void 0,n.length>0&&this.emitEvents(`local`,n)}commit(){if(this.debounceState===void 0)return;let{timerId:e,maxTimerId:t,pendingEvents:n}=this.debounceState;e!==void 0&&(clearTimeout(e),this.debounceState.timerId=void 0),t!==void 0&&(clearTimeout(t),this.debounceState.maxTimerId=void 0),n.length>0&&(this.emitEvents(`local`,n),this.debounceState.pendingEvents=[])}isAutoDebounceActive(){return this.debounceState!==void 0}};export{kr as Flock,kr as FlockSQLite,gr as decodeVersionVector,hr as encodeVersionVector};
|
|
31
31
|
//# sourceMappingURL=index.mjs.map
|