mirror of
https://github.com/orbitdb/orbitdb.git
synced 2025-03-30 15:08:28 +00:00
Add 'load' and 'loaded' events for broadcasting load state
This commit is contained in:
parent
50a2681421
commit
e2e3374228
@ -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;
|
||||
|
@ -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 */
|
||||
|
Loading…
x
Reference in New Issue
Block a user