Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
return new Promise((resolve, reject) => {
if(hash && hash !== this.lastWrite && this._logs[channel]) {
this.events[channel].emit('load', 'sync', channel);
const oldCount = this._logs[channel].items.length;
Log.fromIpfsHash(this._ipfs, hash).then((other) => {
this._logs[channel].join(other).then((merged) => {
// Only emit the event if something was added
const joinedCount = this._logs[channel].items.length - oldCount;
if(joinedCount > 0) {
Cache.set(channel, hash);
// Cache the payloads
this._cacheOperations(this._logs[channel])
.then(() => {
this.events[channel].emit('sync', channel, hash);
this.events[channel].emit('loaded', 'sync', channel);
resolve();
})
.catch(reject);
} else {
this.events[channel].emit('loaded', 'sync', channel);
resolve();
sync(hash) {
if(!hash || hash === this._lastWrite)
return Promise.resolve([]);
const oldCount = this._oplog.items.length;
let newItems = [];
this.events.emit('load', this.dbname);
return Log.fromIpfsHash(this._ipfs, hash)
.then((log) => this._oplog.join(log))
.then((merged) => newItems = merged)
.then(() => Log.getIpfsHash(this._ipfs, this._oplog))
.then((hash) => Cache.set(this.dbname, hash))
.then(() => this._index.updateIndex(this._oplog, newItems))
.then(() => {
if(newItems.length > 0)
this.events.emit('readable', this.dbname);
})
.then(() => newItems)
}
return Cache.loadCache(this.options.cacheFile).then(() => {
const cached = Cache.get(this.dbname);
if(cached) {
return Log.fromIpfsHash(this._ipfs, cached)
.then((log) => this._oplog.join(log))
.then((merged) => this._index.updateIndex(this._oplog, merged))
.then(() => this.events.emit('readable', this.dbname))
.then(() => this.events);
}
return Promise.resolve(this.events);
});
}