Skip to content

Commit

Permalink
Allow passing instance of service directly to client
Browse files Browse the repository at this point in the history
  • Loading branch information
SomeoneWeird committed Dec 24, 2016
1 parent d3dc588 commit ac74595
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/client.js
Expand Up @@ -7,6 +7,9 @@ const async = require('async')
const bluebird = require('bluebird')
const utils = require('ht-utils')

const Service = require('./service')
const LocalTransport = require('./transports').Local

let Client = function Client (services) {
if (!(this instanceof Client)) {
return new Client(services)
Expand Down Expand Up @@ -35,6 +38,11 @@ Client.prototype.add = function (name, transport) {
if (this.services[name]) {
throw new Error('Tried adding a service with duplicate name')
}
if (transport instanceof Service) {
let service = transport
transport = new LocalTransport()
service.addTransport(transport)
}
this.services[name] = transport
let client = new transport.Client()
this.connections[name] = client
Expand Down
8 changes: 8 additions & 0 deletions test/client.js
Expand Up @@ -5,6 +5,7 @@ const s = require('ht-schema')
const bluebird = require('bluebird')

const Client = require('../src/client')
const Service = require('../src/service')

const _data = {
hello: 'world'
Expand Down Expand Up @@ -143,6 +144,13 @@ describe('Client', function () {
client.add('test3', mockTransport()({}))
assert.equal(Object.keys(client.connections).length, countBefore + 1)
})

it('should allow adding a service directly', function () {
let client = new Client()
let service = new Service()
client.add('myService', service)
assert.deepEqual(Object.keys(client.connections), [ 'myService' ])
})
})

describe('getServices', function () {
Expand Down

0 comments on commit ac74595

Please sign in to comment.