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,6 +23,8 @@ class OrbitDB {
} }
sync(channel, hash) { sync(channel, hash) {
this.events[channel].emit('load', channel, hash);
// setTimeout(async(() => {
// console.log("--> Head:", hash) // console.log("--> Head:", hash)
if(hash && this._logs[channel]) { if(hash && this._logs[channel]) {
const oldCount = this._logs[channel].items.length; const oldCount = this._logs[channel].items.length;
@ -32,8 +34,11 @@ class OrbitDB {
// 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 */