From bf85f331bfdad262f81f2fb67e141db31985a810 Mon Sep 17 00:00:00 2001 From: Adam Brady Date: Thu, 22 Dec 2016 10:22:14 +1100 Subject: [PATCH] Allow passing instace of service directly to client --- src/client.js | 8 ++++++++ test/client.js | 8 ++++++++ 2 files changed, 16 insertions(+) diff --git a/src/client.js b/src/client.js index 1e9a5fa..1a5db99 100644 --- a/src/client.js +++ b/src/client.js @@ -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) @@ -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 diff --git a/test/client.js b/test/client.js index 826bde5..5df933c 100644 --- a/test/client.js +++ b/test/client.js @@ -6,6 +6,7 @@ const s = require("ht-schema"); const bluebird = require("bluebird"); const Client = require("../src/client"); +const Service = require("../src/service"); const _data = { hello: "world" @@ -163,6 +164,13 @@ describe("Client", function() { 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("call", function() {