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,11 +54,12 @@ class OrbitDB {
_subscribe(store, dbname, subscribe, callback) { _subscribe(store, dbname, subscribe, callback) {
if(subscribe === undefined) subscribe = true; if(subscribe === undefined) subscribe = true;
store.events.on('ready', this._onReady.bind(this)); store.events.on('load', this._onLoad.bind(this));
store.events.on('readable', this._onSync.bind(this)); store.events.on('ready', this._onReady.bind(this));
store.events.on('data', this._onWrite.bind(this)); store.events.on('sync', this._onSync.bind(this));
store.events.on('load', this._onLoad.bind(this)); store.events.on('updated', this._onSynced.bind(this));
store.events.on('close', this._onClose.bind(this)); store.events.on('data', this._onWrite.bind(this));
store.events.on('close', this._onClose.bind(this));
if(subscribe) if(subscribe)
this._pubsub.subscribe(dbname, '', this._onMessage.bind(this)); this._pubsub.subscribe(dbname, '', this._onMessage.bind(this));
@ -72,20 +73,29 @@ class OrbitDB {
} }
_onWrite(dbname, hash) { _onWrite(dbname, hash) {
// console.log(".WRITE", dbname);
if(!hash) throw new Error("Hash can't be null!"); if(!hash) throw new Error("Hash can't be null!");
this._pubsub.publish(dbname, hash); this._pubsub.publish(dbname, hash);
this.events.emit('data', dbname, hash); this.events.emit('data', dbname, hash);
} }
_onSync(dbname, hash) { _onSync(dbname) {
this.events.emit('readable', dbname, hash); // console.log(".SYNC", dbname);
this.events.emit('sync', dbname);
}
_onSynced(dbname, items) {
// console.log(".SYNCED", dbname);
this.events.emit('synced', dbname, items);
} }
_onLoad(dbname) { _onLoad(dbname) {
// console.log(".LOAD", dbname);
this.events.emit('load', dbname); this.events.emit('load', dbname);
} }
_onReady(dbname) { _onReady(dbname) {
// console.log(".READY", dbname);
this.events.emit('ready', this.stores[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(); const items = db.iterator({ limit: -1 }).collect();
assert.equal(items.length, 5); assert.equal(items.length, 5);
assert.equal(_.first(items.map((f) => f.value)), 'hello1'); assert.equal(_.first(items.map((f) => f.payload.value)), 'hello1');
assert.equal(_.last(items.map((f) => f.value)), 'hello5'); assert.equal(_.last(items.map((f) => f.payload.value)), 'hello5');
done(); done();
})); }));
@ -207,7 +207,7 @@ describe('Orbit Client', function() {
await(db.remove(head)); await(db.remove(head));
const items = db.iterator({ limit: -1 }).collect(); const items = db.iterator({ limit: -1 }).collect();
assert.equal(items.length, 1); assert.equal(items.length, 1);
assert.equal(items[0].value, 'hello1'); assert.equal(items[0].payload.value, 'hello1');
done(); done();
})); }));
@ -218,9 +218,9 @@ describe('Orbit Client', function() {
await(db.add('hello3')); await(db.add('hello3'));
const items = db.iterator().collect(); const items = db.iterator().collect();
assert.equal(items.length, 1); assert.equal(items.length, 1);
assert.equal(items[0].key, null);
assert.equal(items[0].hash.startsWith('Qm'), true); 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(); done();
})); }));
}); });
@ -253,10 +253,10 @@ describe('Orbit Client', function() {
const iter = db.iterator(); const iter = db.iterator();
const next = iter.next().value; const next = iter.next().value;
assert.notEqual(next, null); assert.notEqual(next, null);
assert.equal(next.key, null);
assert.equal(next.hash.startsWith('Qm'), true); assert.equal(next.hash.startsWith('Qm'), true);
assert.equal(next.value, 'hello4'); assert.equal(next.payload.key, null);
assert.notEqual(next.meta.ts, null); assert.equal(next.payload.value, 'hello4');
assert.notEqual(next.payload.meta.ts, null);
done(); done();
})); }));
@ -277,7 +277,7 @@ describe('Orbit Client', function() {
const second = iter.next().value; const second = iter.next().value;
assert.equal(first.hash, items[items.length - 1]); assert.equal(first.hash, items[items.length - 1]);
assert.equal(second, null); assert.equal(second, null);
assert.equal(first.value, 'hello4'); assert.equal(first.payload.value, 'hello4');
done(); done();
})); }));
}); });
@ -286,8 +286,8 @@ describe('Orbit Client', function() {
it('returns all items', async((done) => { it('returns all items', async((done) => {
const messages = db.iterator({ limit: -1 }).collect(); const messages = db.iterator({ limit: -1 }).collect();
assert.equal(messages.length, items.length); assert.equal(messages.length, items.length);
assert.equal(messages[0].value, 'hello0'); assert.equal(messages[0].payload.value, 'hello0');
assert.equal(messages[messages.length - 1].value, 'hello4'); assert.equal(messages[messages.length - 1].payload.value, 'hello4');
done(); done();
})); }));