Skip to content

Commit

Permalink
lib: prepare code to use metarhia-common
Browse files Browse the repository at this point in the history
  • Loading branch information
tshemsedinov committed Jul 9, 2017
1 parent 5091398 commit 880f6d1
Show file tree
Hide file tree
Showing 10 changed files with 59 additions and 49 deletions.
10 changes: 6 additions & 4 deletions lib/applications.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict';

const errors = require('./errors');
const common = require('./common');

const apps = {};
module.exports = apps;
Expand All @@ -24,21 +25,22 @@ class Application {
// callback - method callback
//
callMethod(connection, interfaceName, methodName, args, callback) {
const cb = common.cb(callback);
const appInterface = this.api[interfaceName];
if (!appInterface) {
return callback(errors.ERR_INTERFACE_NOT_FOUND);
return cb(errors.ERR_INTERFACE_NOT_FOUND);
}

const method = appInterface[methodName];
if (!method) {
return callback(errors.ERR_METHOD_NOT_FOUND);
return cb(errors.ERR_METHOD_NOT_FOUND);
}

if (method.length !== args.length + 2) {
return callback(errors.ERR_INVALID_SIGNATURE);
return cb(errors.ERR_INVALID_SIGNATURE);
}

method(connection, ...args, callback);
method(connection, ...args, cb);
}

// Get an array of methods of an interface
Expand Down
34 changes: 24 additions & 10 deletions lib/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
const common = {};
module.exports = common;

common.falseness = () => false;
common.trueness = () => true;
common.emptiness = () => {};

// Forward an event from one EventEmitter to another.
// from - EventEmitter to listen for event
// to - EventEmitter to emit event on
Expand Down Expand Up @@ -48,15 +52,25 @@ common.mixin = (target, source) => {
});
};

// If last element of the array args is a function then
// pops the array and returns that function else returns null.
//
common.extractCallback = (args) => {
if (typeof args[args.length - 1] === 'function') return args.pop();
return null;
// Wrap callback: call once, not null
// See: metarhia-common/api.common.cb
common.cb = (callback) => {
let done = false;
const wrap = (...args) => {
if (done) return;
done = true;
callback(...args);
};
return callback ? wrap : common.emptiness;
};

// This function can be used in contexts where a function (e.g., a callback) is
// required but no actions have to be done.
//
common.doNothing = () => {};
// Exctracts callback and wraps it with common.cb
// See: metarhia-common/api.common.cbExtract
common.cbExtract = (args) => {
const lastArg = args[args.length - 1];
if (typeof(lastArg) !== 'function') return common.falseness;

const cb = common.cb(lastArg);
args.pop();
return common.cb(cb);
};
6 changes: 2 additions & 4 deletions lib/connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,9 +128,7 @@ class Connection extends EventEmitter {
this.username = login;
}
this.sessionId = sessionId;
if (callback) {
callback(error, sessionId);
}
callback(error, sessionId);
};

this._send(packet);
Expand Down Expand Up @@ -171,7 +169,7 @@ class Connection extends EventEmitter {
ping(callback) {
const packet = this._createPacket('ping');
const packetId = packet.ping[0];
this._callbacks[packetId] = callback || common.doNothing;
this._callbacks[packetId] = callback || common.emptiness;
this._send(packet);
}

Expand Down
12 changes: 3 additions & 9 deletions lib/remote-proxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

const events = require('events');
const util = require('util');
const common = require('./common');

module.exports = RemoteProxy;

Expand Down Expand Up @@ -57,13 +58,6 @@ RemoteProxy.wrapRemoteMethod = (instance, methodName) => {
// methodName - name of a remote method
//
function remoteMethodWrapper(methodName, ...args) {
let callback = args[args.length - 1];
args = Array.prototype.slice.call(args, 0, -1);

if (typeof(callback) !== 'function') {
args.push(callback);
callback = null;
}

this._connection.callMethod(this._interfaceName, methodName, args, callback);
const cb = common.cbExtract(args);
this._connection.callMethod(this._interfaceName, methodName, args, cb);
}
7 changes: 4 additions & 3 deletions lib/simple-connect-policy.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@ module.exports = class SimpleConnectPolicy {
// (error, connection)
//
connect(appName, connection, callback) {
connection.handshake(appName, this.login, this.password, (error) => {
callback(error, connection);
});
connection.handshake(
appName, this.login, this.password,
error => callback(error, connection)
);
}
};
8 changes: 4 additions & 4 deletions lib/socket.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,12 +83,12 @@ class Transport extends EventEmitter {
}

const socketFactory = connect => (...options) => {
const callback = common.extractCallback(options);
const cb = common.cbExtract(options);
const socket = connect(...options);
socket.once('error', callback);
socket.once('error', cb);
socket.once('connect', () => {
socket.removeListener('error', callback);
callback(null, socket);
socket.removeListener('error', cb);
cb(null, socket);
});
};

Expand Down
16 changes: 8 additions & 8 deletions lib/transport-common.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ const common = require('./common');
const newConnectFn = (
connFactory, transportClass
) => (appName, client, ...options) => {
const callback = common.extractCallback(options);
const cb = common.cbExtract(options);
connFactory(...options, (error, rawConnection) => {
if (error) return callback(error);
if (error) return cb(error);

// eslint-disable-next-line new-cap
const transport = new transportClass(rawConnection);
Expand All @@ -49,11 +49,11 @@ const newConnectFn = (
}
const connection = new Connection(transport, null, client);
client.connectPolicy(appName, connection, (error, connection) => {
if (error) return callback(error, connection);
if (error) return cb(error, connection);
if (client.heartbeatInterval) {
connection.startHeartbeat(client.heartbeatInterval);
}
callback(null, connection);
cb(null, connection);
});
});
};
Expand All @@ -65,9 +65,9 @@ const newConnectFn = (
const newConnectAndInspectFn = (connFactory, transportClass) => {
const connect = newConnectFn(connFactory, transportClass);
return (appName, client, interfaces, ...options) => {
const callback = common.extractCallback(options);
const cb = common.cbExtract(options);
connect(appName, client, ...options, (error, connection) => {
if (error) return callback(error);
if (error) return cb(error);
const proxies = Object.create(null);
let errored = false;
const len = interfaces.length;
Expand All @@ -78,10 +78,10 @@ const newConnectAndInspectFn = (connFactory, transportClass) => {
count++;
if (error) {
errored = true;
return callback(error, connection);
return cb(error, connection);
}
proxies[name] = proxy;
if (count === len) callback(null, connection, proxies);
if (count === len) cb(null, connection, proxies);
}
}
));
Expand Down
7 changes: 4 additions & 3 deletions lib/ws-browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,13 @@ class Transport extends EventEmitter {
// appProvider - client application provider
//
const socketFactory = (url, callback) => {
const cb = common.cb(callback);
try {
const webSocket = new WebSocket(url, constants.WEBSOCKET_PROTOCOL_NAME);
webSocket.onopen = () => callback(null, webSocket);
webSocket.onerror = callback;
webSocket.onopen = () => cb(null, webSocket);
webSocket.onerror = cb;
} catch (error) {
if (callback) callback(error);
cb(error);
}
};

Expand Down
6 changes: 3 additions & 3 deletions lib/ws-internal.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,11 +116,11 @@ const initServer = function(options, httpServer) {
// is established.
//
const connectionFactory = (webSocketConfig, ...options) => {
const callback = common.extractCallback(options);
const cb = common.cbExtract(options);
const wsClient = new WebSocketClient(webSocketConfig);
options[1] = constants.WEBSOCKET_PROTOCOL_NAME;
wsClient.once('connect', connection => callback(null, connection));
wsClient.once('connectFailed', callback);
wsClient.once('connect', connection => cb(null, connection));
wsClient.once('connectFailed', cb);
wsClient.connect(...options);
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ const test = require('tap');

const common = require('../../lib/common');

test.equal(common.doNothing(), undefined, 'must not return a value');
test.equal(common.emptiness(), undefined, 'must not return a value');

0 comments on commit 880f6d1

Please sign in to comment.