Rename events OrbitDB emits.

This commit is contained in:
haad 2016-05-11 12:27:05 +02:00
parent 5889a4ef50
commit 8b786ae6b5
2 changed files with 28 additions and 18 deletions

View File

@ -54,10 +54,11 @@ class OrbitDB {
_subscribe(store, dbname, subscribe, callback) {
if(subscribe === undefined) subscribe = true;
store.events.on('ready', this._onReady.bind(this));
store.events.on('readable', this._onSync.bind(this));
store.events.on('data', this._onWrite.bind(this));
store.events.on('load', this._onLoad.bind(this));
store.events.on('ready', this._onReady.bind(this));
store.events.on('sync', this._onSync.bind(this));
store.events.on('updated', this._onSynced.bind(this));
store.events.on('data', this._onWrite.bind(this));
store.events.on('close', this._onClose.bind(this));
if(subscribe)
@ -72,20 +73,29 @@ class OrbitDB {
}
_onWrite(dbname, hash) {
// console.log(".WRITE", dbname);
if(!hash) throw new Error("Hash can't be null!");
this._pubsub.publish(dbname, hash);
this.events.emit('data', dbname, hash);
}
_onSync(dbname, hash) {
this.events.emit('readable', dbname, hash);
_onSync(dbname) {
// console.log(".SYNC", dbname);
this.events.emit('sync', dbname);
}
_onSynced(dbname, items) {
// console.log(".SYNCED", dbname);
this.events.emit('synced', dbname, items);
}
_onLoad(dbname) {
// console.log(".LOAD", dbname);
this.events.emit('load', dbname);
}
_onReady(dbname) {
// console.log(".READY", dbname);
this.events.emit('ready', this.stores[dbname]);
}

View File

@ -170,8 +170,8 @@ describe('Orbit Client', function() {
const items = db.iterator({ limit: -1 }).collect();
assert.equal(items.length, 5);
assert.equal(_.first(items.map((f) => f.value)), 'hello1');
assert.equal(_.last(items.map((f) => f.value)), 'hello5');
assert.equal(_.first(items.map((f) => f.payload.value)), 'hello1');
assert.equal(_.last(items.map((f) => f.payload.value)), 'hello5');
done();
}));
@ -207,7 +207,7 @@ describe('Orbit Client', function() {
await(db.remove(head));
const items = db.iterator({ limit: -1 }).collect();
assert.equal(items.length, 1);
assert.equal(items[0].value, 'hello1');
assert.equal(items[0].payload.value, 'hello1');
done();
}));
@ -218,9 +218,9 @@ describe('Orbit Client', function() {
await(db.add('hello3'));
const items = db.iterator().collect();
assert.equal(items.length, 1);
assert.equal(items[0].key, null);
assert.equal(items[0].hash.startsWith('Qm'), true);
assert.equal(items[0].value, 'hello3');
assert.equal(items[0].payload.key, null);
assert.equal(items[0].payload.value, 'hello3');
done();
}));
});
@ -253,10 +253,10 @@ describe('Orbit Client', function() {
const iter = db.iterator();
const next = iter.next().value;
assert.notEqual(next, null);
assert.equal(next.key, null);
assert.equal(next.hash.startsWith('Qm'), true);
assert.equal(next.value, 'hello4');
assert.notEqual(next.meta.ts, null);
assert.equal(next.payload.key, null);
assert.equal(next.payload.value, 'hello4');
assert.notEqual(next.payload.meta.ts, null);
done();
}));
@ -277,7 +277,7 @@ describe('Orbit Client', function() {
const second = iter.next().value;
assert.equal(first.hash, items[items.length - 1]);
assert.equal(second, null);
assert.equal(first.value, 'hello4');
assert.equal(first.payload.value, 'hello4');
done();
}));
});
@ -286,8 +286,8 @@ describe('Orbit Client', function() {
it('returns all items', async((done) => {
const messages = db.iterator({ limit: -1 }).collect();
assert.equal(messages.length, items.length);
assert.equal(messages[0].value, 'hello0');
assert.equal(messages[messages.length - 1].value, 'hello4');
assert.equal(messages[0].payload.value, 'hello0');
assert.equal(messages[messages.length - 1].payload.value, 'hello4');
done();
}));