UNPKG

@signaldb/sync

Version:

This is the sync implementation of [SignalDB](https://github.com/maxnowack/signaldb). SignalDB is a local-first JavaScript database with real-time sync, enabling optimistic UI with signal-based reactivity across multiple frameworks.

6 lines (4 loc) 11.6 kB
(function(f,r){typeof exports=="object"&&typeof module<"u"?r(exports,require("@signaldb/core")):typeof define=="function"&&define.amd?define(["exports","@signaldb/core"],r):(f=typeof globalThis<"u"?globalThis:f||self,r(f.SignalDB={},f.core))})(this,function(f,r){"use strict";var D=Object.defineProperty;var M=(f,r,g)=>r in f?D(f,r,{enumerable:!0,configurable:!0,writable:!0,value:g}):f[r]=g;var u=(f,r,g)=>M(f,typeof r!="symbol"?r+"":r,g);function g(l,e,t={}){let s,n;const{leading:a=!1,trailing:o=!0}=t;function c(...h){const i=a&&!s,p=o&&!s;return s&&clearTimeout(s),s=setTimeout(()=>{s=null,o&&!i&&(n=l.apply(this,h))},e),i?n=l.apply(this,h):p||(n=null),n}return c}class O{constructor(){u(this,"queue",[]);u(this,"pendingPromise",!1)}add(e){return new Promise((t,s)=>{this.queue.push(()=>e().then(t).catch(n=>{throw s(n),n})),this.dequeue()})}hasPendingPromise(){return this.pendingPromise}dequeue(){if(this.pendingPromise||this.queue.length===0)return;const e=this.queue.shift();e&&(this.pendingPromise=!0,e().then(()=>{this.pendingPromise=!1,this.dequeue()}).catch(()=>{this.pendingPromise=!1,this.dequeue()}))}}function w(l,e){const t=[],s=[],n=[],a=new Map(l.map(c=>[c.id,c])),o=new Map(e.map(c=>[c.id,c]));for(const[c,h]of a){const i=o.get(c);i?r.isEqual(i,h)||s.push(i):n.push(h)}for(const[c,h]of o)a.has(c)||t.push(h);return{added:t,modified:s,removed:n}}function C(l,e){if(e.items!=null)return e.items;const t=l||[];return e.changes.added.forEach(s=>{const n=t.findIndex(a=>a.id===s.id);n===-1?t.push(s):t[n]=s}),e.changes.modified.forEach(s=>{const n=t.findIndex(a=>a.id===s.id);n===-1?t.push(s):t[n]=s}),e.changes.removed.forEach(s=>{const n=t.findIndex(a=>a.id===s.id);n!==-1&&t.splice(n,1)}),t}function P(l,e){const t=new Map(l.map(s=>[s.id,s]));return e.forEach(s=>{if(s.type==="remove")t.delete(s.data);else if(s.type==="insert"){const n=t.get(s.data.id);t.set(s.data.id,n?{...n,...s.data}:s.data)}else{const n=t.get(s.data.id);t.set(s.data.id,n?r.modify(n,s.data.modifier):r.modify({id:s.data.id},s.data.modifier))}}),[...t.values()]}function v(l){return l.added.length>0||l.modified.length>0||l.removed.length>0}function $(l,e){return v(w(l,e))}async function E({changes:l,lastSnapshot:e,data:t,pull:s,push:n,insert:a,update:o,remove:c,batch:h}){let i=t,p=e||[],m=C(e,i);if(l.length>0){const y=P(p,l);if($(p,y)){const I=P(m,l),N=w(m,I);v(N)&&(await n(N),i=await s(),m=C(m,i)),p=y}}const d=i.changes==null?w(p,i.items):i.changes;return h(()=>{d.added.forEach(y=>a(y)),d.modified.forEach(y=>o(y.id,{$set:y})),d.removed.forEach(y=>c(y.id))}),m}class S{constructor(e){u(this,"options");u(this,"collections",new Map);u(this,"changes");u(this,"snapshots");u(this,"syncOperations");u(this,"scheduledPushes",new Set);u(this,"remoteChanges",[]);u(this,"syncQueues",new Map);u(this,"persistenceReady");u(this,"isDisposed",!1);u(this,"instanceId",r.randomId());u(this,"id");u(this,"debouncedFlush");this.options={autostart:!0,...e},this.id=this.options.id||"default-sync-manager";const{reactivity:t}=this.options,s=this.createPersistenceAdapter("changes"),n=this.createPersistenceAdapter("snapshots"),a=this.createPersistenceAdapter("sync-operations");this.changes=new r.Collection({name:`${this.options.id}-changes`,persistence:s==null?void 0:s.adapter,indices:[r.createIndex("collectionName")],reactivity:t}),this.snapshots=new r.Collection({name:`${this.options.id}-snapshots`,persistence:n==null?void 0:n.adapter,indices:[r.createIndex("collectionName")],reactivity:t}),this.syncOperations=new r.Collection({name:`${this.options.id}-sync-operations`,persistence:a==null?void 0:a.adapter,indices:[r.createIndex("collectionName"),r.createIndex("status")],reactivity:t}),this.changes.on("persistence.error",o=>s==null?void 0:s.handler(o)),this.snapshots.on("persistence.error",o=>n==null?void 0:n.handler(o)),this.syncOperations.on("persistence.error",o=>a==null?void 0:a.handler(o)),this.persistenceReady=Promise.all([this.syncOperations.isReady(),this.changes.isReady(),this.snapshots.isReady()]).then(()=>{}),this.changes.setMaxListeners(1e3),this.snapshots.setMaxListeners(1e3),this.syncOperations.setMaxListeners(1e3),this.debouncedFlush=g(this.flushScheduledPushes,this.options.debounceTime??100)}createPersistenceAdapter(e){if(this.options.persistenceAdapter==null)return;let t=()=>{};return{adapter:this.options.persistenceAdapter(`${this.id}-${e}`,n=>{t=n}),handler:n=>t(n)}}getSyncQueue(e){return this.syncQueues.get(e)==null&&this.syncQueues.set(e,new O),this.syncQueues.get(e)}async dispose(){this.collections.clear(),this.syncQueues.clear(),this.remoteChanges.splice(0,this.remoteChanges.length),await Promise.all([this.changes.dispose(),this.snapshots.dispose(),this.syncOperations.dispose()]),this.isDisposed=!0}getCollection(e){const{collection:t,options:s}=this.getCollectionProperties(e);return[t,s]}getCollectionProperties(e){const t=this.collections.get(e);if(t==null)throw new Error(`Collection with id '${e}' not found`);return t}addCollection(e,t){if(this.isDisposed)throw new Error("SyncManager is disposed");this.collections.set(t.name,{collection:e,options:t,readyPromise:e.isReady(),syncPaused:!0});const s=a=>{for(const o of this.remoteChanges)if(o!=null&&o.collectionName===a.collectionName&&o.type===a.type&&!(a.type==="remove"&&o.data!==a.data)&&o.data.id===a.data.id)return!0;return!1},n=(a,o)=>{const c=[...this.remoteChanges];for(let h=0;h<c.length;h+=1){const i=c[h];i!=null&&i.collectionName===a&&(i.type==="remove"&&i.data!==o||i.data.id===o&&(c[h]=null))}this.remoteChanges=c.filter(h=>h!=null)};e.on("added",a=>{if(s({collectionName:t.name,type:"insert",data:a})){n(t.name,a.id);return}this.changes.insert({collectionName:t.name,time:Date.now(),type:"insert",data:a}),!this.getCollectionProperties(t.name).syncPaused&&this.schedulePush(t.name)}),e.on("changed",({id:a},o)=>{const c={id:a,modifier:o};if(s({collectionName:t.name,type:"update",data:c})){n(t.name,a);return}this.changes.insert({collectionName:t.name,time:Date.now(),type:"update",data:c}),!this.getCollectionProperties(t.name).syncPaused&&this.schedulePush(t.name)}),e.on("removed",({id:a})=>{if(s({collectionName:t.name,type:"remove",data:a})){n(t.name,a);return}this.changes.insert({collectionName:t.name,time:Date.now(),type:"remove",data:a}),!this.getCollectionProperties(t.name).syncPaused&&this.schedulePush(t.name)}),this.options.autostart&&this.startSync(t.name).catch(a=>{this.options.onError&&this.options.onError(this.getCollectionProperties(t.name).options,a)})}flushScheduledPushes(){this.scheduledPushes.forEach(e=>{this.pushChanges(e).catch(()=>{})}),this.scheduledPushes.clear()}schedulePush(e){this.scheduledPushes.add(e),this.debouncedFlush()}async startAll(){await Promise.all([...this.collections.keys()].map(e=>this.startSync(e)))}async startSync(e){const t=this.getCollectionProperties(e);if(!t.syncPaused)return;this.schedulePush(e);const s=this.options.registerRemoteChange?await this.options.registerRemoteChange(t.options,async n=>{if(n==null)await this.sync(e);else{const a=Date.now(),o=this.syncOperations.insert({start:a,collectionName:e,instanceId:this.instanceId,status:"active"});await this.syncWithData(e,n).then(()=>{this.syncOperations.removeMany({id:{$ne:o},collectionName:e,$or:[{end:{$lte:a}},{status:"active"}]}),this.syncOperations.updateOne({id:o},{$set:{status:"done",end:Date.now()}})}).catch(c=>{throw this.options.onError&&this.options.onError(this.getCollectionProperties(e).options,c),this.syncOperations.updateOne({id:o},{$set:{status:"error",end:Date.now(),error:c.stack||c.message}}),c})}}):void 0;this.collections.set(e,{...t,syncPaused:!1,cleanupFunction:s})}async pauseAll(){await Promise.all([...this.collections.keys()].map(e=>this.pauseSync(e)))}async pauseSync(e){const t=this.getCollectionProperties(e);t.syncPaused||(t.cleanupFunction&&await t.cleanupFunction(),this.collections.set(e,{...t,cleanupFunction:void 0,syncPaused:!0}))}async syncAll(){if(this.isDisposed)throw new Error("SyncManager is disposed");const e=[];if(await Promise.all([...this.collections.keys()].map(t=>this.sync(t).catch(s=>{e.push({id:t,error:s})}))),e.length>0)throw new Error(`Error while syncing collections: ${e.map(t=>`${t.id}: ${t.error.message}`).join(` `)}`)}isSyncing(e){return this.syncOperations.findOne({...e?{collectionName:e}:{},status:"active"},{fields:{status:1}})!=null}async isReady(){await this.persistenceReady}async sync(e,t={}){if(this.isDisposed)throw new Error("SyncManager is disposed");await this.isReady();const{options:s,readyPromise:n}=this.getCollectionProperties(e);await n;const a=this.syncOperations.find({collectionName:e,instanceId:this.instanceId,status:"active"},{reactive:!1}).count()>0,o=Date.now();let c=null;await new Promise(i=>{setTimeout(i,0)});const h=async()=>{const i=this.syncOperations.findOne({collectionName:e,status:"done"},{sort:{end:-1},reactive:!1});if(t!=null&&t.onlyWithChanges&&this.changes.find({collectionName:e,time:{$lte:o}},{sort:{time:1},reactive:!1}).count()===0)return;a||(c=this.syncOperations.insert({start:o,collectionName:e,instanceId:this.instanceId,status:"active"}));const p=await this.options.pull(s,{lastFinishedSyncStart:i==null?void 0:i.start,lastFinishedSyncEnd:i==null?void 0:i.end});await this.syncWithData(e,p)};await(t!=null&&t.force?h():this.getSyncQueue(e).add(h)).catch(i=>{throw c!=null&&(this.options.onError&&this.options.onError(s,i),this.syncOperations.updateOne({id:c},{$set:{status:"error",end:Date.now(),error:i.stack||i.message}})),i}),c!=null&&(this.syncOperations.removeMany({id:{$ne:c},collectionName:e,$or:[{end:{$lte:o}},{status:"active"}]}),this.syncOperations.updateOne({id:c},{$set:{status:"done",end:Date.now()}}))}async pushChanges(e){await this.sync(e,{onlyWithChanges:!0})}async syncWithData(e,t){const{collection:s,options:n}=this.getCollectionProperties(e),a=Date.now(),o=this.syncOperations.findOne({collectionName:e,status:"done"},{sort:{end:-1},reactive:!1}),c=this.snapshots.findOne({collectionName:e},{sort:{time:-1},reactive:!1}),h=this.changes.find({collectionName:e,time:{$lte:a}},{sort:{time:1},reactive:!1}).fetch();await E({changes:h,lastSnapshot:c==null?void 0:c.items,data:t,pull:()=>this.options.pull(n,{lastFinishedSyncStart:o==null?void 0:o.start,lastFinishedSyncEnd:o==null?void 0:o.end}),push:i=>this.options.push(n,{changes:i}),insert:i=>{this.remoteChanges.push({collectionName:e,type:"insert",data:i},{collectionName:e,type:"update",data:{id:i.id,modifier:{$set:i}}}),s.replaceOne({id:i.id},i,{upsert:!0})},update:(i,p)=>{this.remoteChanges.push({collectionName:e,type:"insert",data:{id:i,...p.$set}},{collectionName:e,type:"update",data:{id:i,modifier:p}}),s.updateOne({id:i},{...p,$setOnInsert:{id:i}},{upsert:!0})},remove:i=>{s.findOne({id:i},{reactive:!1})&&(this.remoteChanges.push({collectionName:e,type:"remove",data:i}),s.removeOne({id:i}))},batch:i=>{s.batch(()=>{i()})}}).then(async i=>{if(this.snapshots.removeMany({collectionName:e,time:{$lte:a}}),this.changes.removeMany({collectionName:e,id:{$in:h.map(d=>d.id)}}),this.snapshots.insert({time:a,collectionName:e,items:i}),await new Promise(d=>{setTimeout(d,0)}),this.changes.find({collectionName:e},{reactive:!1}).count()>0){await this.sync(e,{force:!0,onlyWithChanges:!0});return}const m=s.find({id:{$nin:i.map(d=>d.id)}},{reactive:!1}).map(d=>d.id);s.batch(()=>{i.forEach(d=>{this.remoteChanges.push({collectionName:e,type:"insert",data:d},{collectionName:e,type:"update",data:{id:d.id,modifier:{$set:d}}}),s.replaceOne({id:d.id},d,{upsert:!0})}),m.forEach(d=>{s.removeOne({id:d})})})})}}f.SyncManager=S,Object.defineProperty(f,Symbol.toStringTag,{value:"Module"})}); //# sourceMappingURL=index.umd.js.map