Add 'load' and 'loaded' events for broadcasting load state

This commit is contained in:
haad 2016-03-21 14:25:05 +01:00
parent 50a2681421
commit e2e3374228
2 changed files with 27 additions and 10 deletions

View File

@ -24,6 +24,8 @@ class Client {
await(this.db.use(channel, this.user, password)); await(this.db.use(channel, this.user, password));
this.db.events[channel].on('write', this._onWrite.bind(this)); 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('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) if(subscribe)
this._pubsub.subscribe(channel, password, async((channel, message) => this.db.sync(channel, message))); 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); 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) { _iterator(channel, password, options) {
const messages = this.db.query(channel, password, options); const messages = this.db.query(channel, password, options);
let currentIndex = 0; let currentIndex = 0;

View File

@ -23,17 +23,22 @@ class OrbitDB {
} }
sync(channel, hash) { sync(channel, hash) {
// console.log("--> Head:", hash) this.events[channel].emit('load', channel, hash);
if(hash && this._logs[channel]) { // setTimeout(async(() => {
const oldCount = this._logs[channel].items.length; // console.log("--> Head:", hash)
const other = await(Log.fromIpfsHash(this._ipfs, hash)); if(hash && this._logs[channel]) {
await(this._logs[channel].join(other)); 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 // Only emit the event if something was added
const joinedCount = (this._logs[channel].items.length - oldCount); const joinedCount = (this._logs[channel].items.length - oldCount);
if(joinedCount > 0) if(joinedCount > 0)
this.events[channel].emit('sync', channel, 'empty'); this.events[channel].emit('sync', channel, hash);
}
}
this.events[channel].emit('loaded', channel, hash);
// }), 3000);
} }
/* DB Operations */ /* DB Operations */