Remove asyncawait from Client, test the channel API

This commit is contained in:
haad
2016-04-13 09:47:22 +02:00
parent 431e102808
commit bed7cd580a
2 changed files with 94 additions and 15 deletions

View File

@@ -1,8 +1,6 @@
'use strict';
const EventEmitter = require('events').EventEmitter;
const async = require('asyncawait/async');
const await = require('asyncawait/await');
const logger = require('orbit-common/lib/logger')("orbit-db.Client");
const ipfsDaemon = require('orbit-common/lib/ipfs-daemon');
const PubSub = require('./PubSub');
@@ -23,27 +21,33 @@ class Client {
if(password === undefined) password = '';
if(subscribe === undefined) subscribe = true;
await(this.db.use(channel, this.user));
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) => await(this.db.sync(channel, message))));
return {
const api = {
iterator: (options) => this._iterator(channel, password, options),
delete: () => this.db.deleteChannel(channel, password),
del: (key) => this.db.del(channel, password, key),
add: (data) => this.db.add(channel, password, data),
put: (key, data) => this.db.put(channel, password, key, data),
put: (key, value) => this.db.put(channel, password, key, value),
get: (key) => {
const items = this._iterator(channel, password, { key: key }).collect();
return items[0] ? items[0] : null;
},
close: () => this._pubsub.unsubscribe(channel)
}
return new Promise((resolve, reject) => {
// Hook to the events from the db and pubsub
this.db.use(channel, this.user).then(() => {
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, this._onMessage.bind(this));
resolve(api);
}).catch(reject);
});
}
disconnect() {
@@ -53,6 +57,10 @@ class Client {
this.network = null;
}
_onMessage(channel, message) {
this.db.sync(channel, message);
}
_onWrite(channel, hash) {
this._pubsub.publish(channel, hash);
this.events.emit('data', channel, hash);

View File

@@ -15,7 +15,7 @@ const username = 'testrunner';
const password = '';
describe('Orbit Client', function() {
this.timeout(20000);
this.timeout(2000);
let client, db;
let channel = 'abcdefgh';
@@ -23,7 +23,7 @@ describe('Orbit Client', function() {
before(async((done) => {
client = await(OrbitClient.connect('localhost', 3333, username, password, null, { allowOffline: true }));
db = client.channel(channel, '', false);
db = await(client.channel(channel, '', false));
db.delete();
done();
}));
@@ -33,6 +33,77 @@ describe('Orbit Client', function() {
if(client) client.disconnect();
});
describe('API', function() {
let api;
const getFunctionParams = (f) => {
let res = f.toString().split('=>')[0].replace('(', '').replace(')', '').replace(' ', '').split(',');
res = res.map((f) => f.trim());
if(res[0] === '') res = [];
return res;
};
beforeEach(async((done) => {
api = await(client.channel('api'));
done();
}));
it('returns an \'iterator\' function', async((done) => {
assert.equal(typeof api.iterator === 'function', true);
const params = getFunctionParams(api.iterator);
assert.equal(params.length, 1);
assert.equal(params[0], 'options');
done();
}));
it('returns a \'delete\' function', async((done) => {
assert.equal(typeof api.delete === 'function', true);
const params = getFunctionParams(api.delete);
assert.equal(params.length, 0);
done();
}));
it('returns a \'del\' function', async((done) => {
assert.equal(typeof api.del === 'function', true);
const params = getFunctionParams(api.del);
assert.equal(params.length, 1);
assert.equal(params[0], 'key');
done();
}));
it('returns a \'add\' function', async((done) => {
assert.equal(typeof api.add === 'function', true);
const params = getFunctionParams(api.add);
assert.equal(params.length, 1);
assert.equal(params[0], 'data');
done();
}));
it('returns a \'put\' function', async((done) => {
assert.equal(typeof api.put === 'function', true);
const params = getFunctionParams(api.put);
assert.equal(params.length, 2);
assert.equal(params[0], 'key');
assert.equal(params[1], 'value');
done();
}));
it('returns a \'get\' function', async((done) => {
assert.equal(typeof api.get === 'function', true);
const params = getFunctionParams(api.get);
assert.equal(params.length, 1);
assert.equal(params[0], 'key');
done();
}));
it('returns a \'close\' function', async((done) => {
assert.equal(typeof api.close === 'function', true);
const params = getFunctionParams(api.close);
assert.equal(params.length, 0);
done();
}));
});
describe('Add events', function() {
beforeEach(() => {
db.delete();