From e2e3374228456e4aa2d26cc266593ee2b874899f Mon Sep 17 00:00:00 2001 From: haad Date: Mon, 21 Mar 2016 14:25:05 +0100 Subject: [PATCH] Add 'load' and 'loaded' events for broadcasting load state --- src/Client.js | 12 ++++++++++++ src/OrbitDB.js | 25 +++++++++++++++---------- 2 files changed, 27 insertions(+), 10 deletions(-) diff --git a/src/Client.js b/src/Client.js index 54a1eab..8d2bf0b 100644 --- a/src/Client.js +++ b/src/Client.js @@ -24,6 +24,8 @@ class Client { await(this.db.use(channel, this.user, password)); this.db.events[channel].on('write', this._onWrite.bind(this)); this.db.events[channel].on('sync', this._onSync.bind(this)); + this.db.events[channel].on('load', this._onLoad.bind(this)); + this.db.events[channel].on('loaded', this._onLoaded.bind(this)); if(subscribe) this._pubsub.subscribe(channel, password, async((channel, message) => this.db.sync(channel, message))); @@ -58,6 +60,16 @@ class Client { this.events.emit('data', channel, hash); } + _onLoad(channel, hash) { + // console.log("LOAD!", channel, hash) + this.events.emit('load', channel, hash); + } + + _onLoaded(channel, hash) { + // console.log("LOADED!", channel, hash) + this.events.emit('loaded', channel, hash); + } + _iterator(channel, password, options) { const messages = this.db.query(channel, password, options); let currentIndex = 0; diff --git a/src/OrbitDB.js b/src/OrbitDB.js index 22c9a6a..bd53fb9 100644 --- a/src/OrbitDB.js +++ b/src/OrbitDB.js @@ -23,17 +23,22 @@ class OrbitDB { } sync(channel, hash) { - // console.log("--> Head:", hash) - if(hash && this._logs[channel]) { - const oldCount = this._logs[channel].items.length; - const other = await(Log.fromIpfsHash(this._ipfs, hash)); - await(this._logs[channel].join(other)); + this.events[channel].emit('load', channel, hash); + // setTimeout(async(() => { + // console.log("--> Head:", hash) + if(hash && this._logs[channel]) { + const oldCount = this._logs[channel].items.length; + const other = await(Log.fromIpfsHash(this._ipfs, hash)); + await(this._logs[channel].join(other)); - // Only emit the event if something was added - const joinedCount = (this._logs[channel].items.length - oldCount); - if(joinedCount > 0) - this.events[channel].emit('sync', channel, 'empty'); - } + // Only emit the event if something was added + const joinedCount = (this._logs[channel].items.length - oldCount); + if(joinedCount > 0) + this.events[channel].emit('sync', channel, hash); + + } + this.events[channel].emit('loaded', channel, hash); + // }), 3000); } /* DB Operations */