@signaldb/sync
Advanced tools
Comparing version 1.1.0 to 1.1.1
@@ -1,5 +0,5 @@ | ||
(function(p,r){typeof exports=="object"&&typeof module<"u"?r(exports,require("@signaldb/core")):typeof define=="function"&&define.amd?define(["exports","@signaldb/core"],r):(p=typeof globalThis<"u"?globalThis:p||self,r(p.SignalDB={},p.core))})(this,function(p,r){"use strict";var M=Object.defineProperty;var I=(p,r,g)=>r in p?M(p,r,{enumerable:!0,configurable:!0,writable:!0,value:g}):p[r]=g;var u=(p,r,g)=>I(p,typeof r!="symbol"?r+"":r,g);function g(d,e,t={}){let s,n;const{leading:o=!1,trailing:a=!0}=t;function c(...l){const i=o&&!s,f=a&&!s;return s&&clearTimeout(s),s=setTimeout(()=>{s=null,a&&!i&&(n=d.apply(this,l))},e),i?n=d.apply(this,l):f||(n=null),n}return c}class N{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(d,e){const t=[],s=[],n=[],o=new Map(d.map(c=>[c.id,c])),a=new Map(e.map(c=>[c.id,c]));for(const[c,l]of o){const i=a.get(c);i?r.isEqual(i,l)||s.push(i):n.push(l)}for(const[c,l]of a)o.has(c)||t.push(l);return{added:t,modified:s,removed:n}}function C(d,e){if(e.items!=null)return e.items;const t=d||[];return e.changes.added.forEach(s=>{const n=t.findIndex(o=>o.id===s.id);n===-1?t.push(s):t[n]=s}),e.changes.modified.forEach(s=>{const n=t.findIndex(o=>o.id===s.id);n===-1?t.push(s):t[n]=s}),e.changes.removed.forEach(s=>{const n=t.findIndex(o=>o.id===s.id);n!==-1&&t.splice(n,1)}),t}async function P(d,e){const t=new r.Collection;t.batch(()=>{d.forEach(n=>t.insert(n)),e.forEach(n=>{if(n.type==="remove"){t.removeOne({id:n.data});return}const o={id:n.data.id},a=t.findOne(o);if(n.type==="insert"){a?t.updateOne(o,{$set:n.data}):t.insert(n.data);return}a?t.updateOne(o,n.data.modifier):t.insert(r.modify(o,n.data.modifier))})});const s=t.find().fetch();return await t.dispose(),s}function O(d){return d.added.length>0||d.modified.length>0||d.removed.length>0}function E(d,e){return O(w(d,e))}async function v({changes:d,lastSnapshot:e,data:t,pull:s,push:n,insert:o,update:a,remove:c,batch:l}){let i=t,f=e||[],y=C(e,i);if(d.length>0){const m=await P(f,d);if(E(f,m)){const D=await P(y,d),$=w(y,D);O($)&&(await n($),i=await s(),y=C(y,i)),f=m}}const h=i.changes==null?w(f,i.items):i.changes;return l(()=>{h.added.forEach(m=>o(m)),h.modified.forEach(m=>a(m.id,{$set:m})),h.removed.forEach(m=>c(m.id))}),y}class S{constructor(e){u(this,"options");u(this,"collections",new Map);u(this,"changes");u(this,"snapshots");u(this,"syncOperations");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,"deboucedPush",g(e=>{this.pushChanges(e).catch(()=>{})},100));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"),o=this.createPersistenceAdapter("sync-operations");this.changes=new r.Collection({name:`${this.options.id}-changes`,persistence:s==null?void 0:s.adapter,reactivity:t}),this.snapshots=new r.Collection({name:`${this.options.id}-snapshots`,persistence:n==null?void 0:n.adapter,reactivity:t}),this.syncOperations=new r.Collection({name:`${this.options.id}-sync-operations`,persistence:o==null?void 0:o.adapter,reactivity:t}),this.changes.on("persistence.error",a=>s==null?void 0:s.handler(a)),this.snapshots.on("persistence.error",a=>n==null?void 0:n.handler(a)),this.syncOperations.on("persistence.error",a=>o==null?void 0:o.handler(a)),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)}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 N),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=o=>{for(const a of this.remoteChanges)if(r.isEqual(a,o))return!0;return!1},n=o=>{for(let a=0;a<this.remoteChanges.length;a+=1)if(r.isEqual(this.remoteChanges[a],o)){this.remoteChanges.splice(a,1);return}};e.on("added",o=>{if(s({collectionName:t.name,type:"insert",data:o})){n({collectionName:t.name,type:"insert",data:o});return}this.changes.insert({collectionName:t.name,time:Date.now(),type:"insert",data:o}),!this.getCollectionProperties(t.name).syncPaused&&this.schedulePush(t.name)}),e.on("changed",({id:o},a)=>{const c={id:o,modifier:a};if(s({collectionName:t.name,type:"update",data:c})){n({collectionName:t.name,type:"update",data:c});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:o})=>{if(s({collectionName:t.name,type:"remove",data:o})){n({collectionName:t.name,type:"remove",data:o});return}this.changes.insert({collectionName:t.name,time:Date.now(),type:"remove",data:o}),!this.getCollectionProperties(t.name).syncPaused&&this.schedulePush(t.name)}),this.options.autostart&&this.startSync(t.name).catch(o=>{this.options.onError&&this.options.onError(this.getCollectionProperties(t.name).options,o)})}schedulePush(e){this.deboucedPush(e)}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 o=Date.now(),a=this.syncOperations.insert({start:o,collectionName:e,instanceId:this.instanceId,status:"active"});await this.syncWithData(e,n).then(()=>{this.syncOperations.removeMany({id:{$ne:a},collectionName:e,$or:[{end:{$lte:o}},{status:"active"}]}),this.syncOperations.updateOne({id:a},{$set:{status:"done",end:Date.now()}})}).catch(c=>{throw this.options.onError&&this.options.onError(this.getCollectionProperties(e).options,c),this.syncOperations.updateOne({id:a},{$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: | ||
(function(f,h){typeof exports=="object"&&typeof module<"u"?h(exports,require("@signaldb/core")):typeof define=="function"&&define.amd?define(["exports","@signaldb/core"],h):(f=typeof globalThis<"u"?globalThis:f||self,h(f.SignalDB={},f.core))})(this,function(f,h){"use strict";var x=Object.defineProperty;var M=(f,h,g)=>h in f?x(f,h,{enumerable:!0,configurable:!0,writable:!0,value:g}):f[h]=g;var p=(f,h,g)=>M(f,typeof h!="symbol"?h+"":h,g);function g(l,e,t={}){let s,n;const{leading:a=!1,trailing:o=!0}=t;function c(...d){const i=a&&!s,u=o&&!s;return s&&clearTimeout(s),s=setTimeout(()=>{s=null,o&&!i&&(n=l.apply(this,d))},e),i?n=l.apply(this,d):u||(n=null),n}return c}class E{constructor(){p(this,"queue",[]);p(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,d]of a){const i=o.get(c);i?h.isEqual(i,d)||s.push(i):n.push(d)}for(const[c,d]of o)a.has(c)||t.push(d);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}async function P(l,e){const t=new h.Collection;t.batch(()=>{l.forEach(n=>t.insert(n)),e.forEach(n=>{if(n.type==="remove"){t.removeOne({id:n.data});return}const a={id:n.data.id},o=t.findOne(a);if(n.type==="insert"){o?t.updateOne(a,{$set:n.data}):t.insert(n.data);return}o?t.updateOne(a,n.data.modifier):t.insert(h.modify(a,n.data.modifier))})});const s=t.find().fetch();return await t.dispose(),s}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 N({changes:l,lastSnapshot:e,data:t,pull:s,push:n,insert:a,update:o,remove:c,batch:d}){let i=t,u=e||[],m=C(e,i);if(l.length>0){const y=await P(u,l);if($(u,y)){const D=await P(m,l),O=w(m,D);v(O)&&(await n(O),i=await s(),m=C(m,i)),u=y}}const r=i.changes==null?w(u,i.items):i.changes;return d(()=>{r.added.forEach(y=>a(y)),r.modified.forEach(y=>o(y.id,{$set:y})),r.removed.forEach(y=>c(y.id))}),m}class S{constructor(e){p(this,"options");p(this,"collections",new Map);p(this,"changes");p(this,"snapshots");p(this,"syncOperations");p(this,"remoteChanges",[]);p(this,"syncQueues",new Map);p(this,"persistenceReady");p(this,"isDisposed",!1);p(this,"instanceId",h.randomId());p(this,"id");p(this,"deboucedPush",g(e=>{this.pushChanges(e).catch(()=>{})},100));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 h.Collection({name:`${this.options.id}-changes`,persistence:s==null?void 0:s.adapter,reactivity:t}),this.snapshots=new h.Collection({name:`${this.options.id}-snapshots`,persistence:n==null?void 0:n.adapter,reactivity:t}),this.syncOperations=new h.Collection({name:`${this.options.id}-sync-operations`,persistence:a==null?void 0:a.adapter,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)}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 E),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(h.isEqual(o,a))return!0;return!1},n=a=>{for(let o=0;o<this.remoteChanges.length;o+=1)if(h.isEqual(this.remoteChanges[o],a)){this.remoteChanges.splice(o,1);return}};e.on("added",a=>{if(s({collectionName:t.name,type:"insert",data:a})){n({collectionName:t.name,type:"insert",data:a});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({collectionName:t.name,type:"update",data:c});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({collectionName:t.name,type:"remove",data: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)})}schedulePush(e){this.deboucedPush(e)}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 o=this.syncOperations.find({collectionName:e,instanceId:this.instanceId,status:"active"}).count()>0,a=Date.now();let c=null;await new Promise(i=>{setTimeout(i,0)});const l=async()=>{const i=this.syncOperations.findOne({collectionName:e,status:"done"},{sort:{end:-1}});if(t!=null&&t.onlyWithChanges&&this.changes.find({collectionName:e,$and:[{time:{$lte:a}}]},{sort:{time:1}}).count()===0)return;o||(c=this.syncOperations.insert({start:a,collectionName:e,instanceId:this.instanceId,status:"active"}));const f=await this.options.pull(s,{lastFinishedSyncStart:i==null?void 0:i.start,lastFinishedSyncEnd:i==null?void 0:i.end});await this.syncWithData(e,f)};await(t!=null&&t.force?l():this.getSyncQueue(e).add(l)).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:a}},{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),o=Date.now(),a=this.syncOperations.findOne({collectionName:e,status:"done"},{sort:{end:-1}}),c=this.snapshots.findOne({collectionName:e},{sort:{time:-1}}),l=this.changes.find({collectionName:e,$and:[{time:{$lte:o}}]},{sort:{time:1}}).fetch();await v({changes:l,lastSnapshot:c==null?void 0:c.items,data:t,pull:()=>this.options.pull(n,{lastFinishedSyncStart:a==null?void 0:a.start,lastFinishedSyncEnd:a==null?void 0:a.end}),push:i=>this.options.push(n,{changes:i}),insert:i=>{if(i.id&&s.findOne({id:i.id})){this.remoteChanges.push({collectionName:e,type:"update",data:{id:i.id,modifier:{$set:i}}}),s.updateOne({id:i.id},{$set:i});return}this.remoteChanges.push({collectionName:e,type:"insert",data:i}),s.insert(i)},update:(i,f)=>{if(i&&!s.findOne({id:i})){const y={...f.$set,id:i};this.remoteChanges.push({collectionName:e,type:"insert",data:y}),s.insert(y);return}this.remoteChanges.push({collectionName:e,type:"update",data:{id:i,modifier:f}}),s.updateOne({id:i},f)},remove:i=>{s.findOne({id:i})&&(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:o}}),this.changes.removeMany({collectionName:e,id:{$in:l.map(h=>h.id)}}),this.snapshots.insert({time:o,collectionName:e,items:i}),await new Promise(h=>{setTimeout(h,0)}),this.changes.find({collectionName:e}).count()>0){await this.sync(e,{force:!0,onlyWithChanges:!0});return}const y=s.find({id:{$nin:i.map(h=>h.id)}}).map(h=>h.id);s.batch(()=>{i.forEach(h=>{const m=!!s.findOne({id:h.id});/* istanbul ignore else -- @preserve */m?(this.remoteChanges.push({collectionName:e,type:"update",data:{id:h.id,modifier:{$set:h}}}),s.updateOne({id:h.id},{$set:h})):(this.remoteChanges.push({collectionName:e,type:"insert",data:h}),s.insert(h))}),y.forEach(h=>{s.removeOne({id:h})})})})}}p.SyncManager=S,Object.defineProperty(p,Symbol.toStringTag,{value:"Module"})}); | ||
`)}`)}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 d=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,$and:[{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 u=await this.options.pull(s,{lastFinishedSyncStart:i==null?void 0:i.start,lastFinishedSyncEnd:i==null?void 0:i.end});await this.syncWithData(e,u)};await(t!=null&&t.force?d():this.getSyncQueue(e).add(d)).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}),d=this.changes.find({collectionName:e,$and:[{time:{$lte:a}}]},{sort:{time:1},reactive:!1}).fetch();await N({changes:d,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=>{if(i.id&&!!s.findOne({id:i.id},{reactive:!1})){this.remoteChanges.push({collectionName:e,type:"update",data:{id:i.id,modifier:{$set:i}}}),s.updateOne({id:i.id},{$set:i});return}this.remoteChanges.push({collectionName:e,type:"insert",data:i}),s.insert(i)},update:(i,u)=>{if(i&&!s.findOne({id:i},{reactive:!1})){const r={...u.$set,id:i};this.remoteChanges.push({collectionName:e,type:"insert",data:r}),s.insert(r);return}this.remoteChanges.push({collectionName:e,type:"update",data:{id:i,modifier:u}}),s.updateOne({id:i},u)},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:d.map(r=>r.id)}}),this.snapshots.insert({time:a,collectionName:e,items:i}),await new Promise(r=>{setTimeout(r,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(r=>r.id)}},{reactive:!1}).map(r=>r.id);s.batch(()=>{i.forEach(r=>{const y=!!s.findOne({id:r.id},{reactive:!1});/* istanbul ignore else -- @preserve */y?(this.remoteChanges.push({collectionName:e,type:"update",data:{id:r.id,modifier:{$set:r}}}),s.updateOne({id:r.id},{$set:r})):(this.remoteChanges.push({collectionName:e,type:"insert",data:r}),s.insert(r))}),m.forEach(r=>{s.removeOne({id:r})})})})}}f.SyncManager=S,Object.defineProperty(f,Symbol.toStringTag,{value:"Module"})}); | ||
//# sourceMappingURL=index.umd.js.map |
{ | ||
"name": "@signaldb/sync", | ||
"version": "1.1.0", | ||
"version": "1.1.1", | ||
"description": "", | ||
@@ -5,0 +5,0 @@ "scripts": { |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
149907
979