Skip to content

Commit

Permalink
Release 2.0.0 Updated API
Browse files Browse the repository at this point in the history
Add tests,
Cleaner encoding/decoding mechanism, only one encoder needed. Protocol encoding to be performed on consumer.
Remove invasive public methods
  • Loading branch information
Paulo committed Jan 30, 2017
1 parent 00cd89a commit 1b84d47
Show file tree
Hide file tree
Showing 5 changed files with 126 additions and 120 deletions.
24 changes: 10 additions & 14 deletions lib/connect.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,52 +14,47 @@ export interface IAdapter {
onData: (data?: any) => any;
onError: (err?: any) => any;
onEnd: (err?: any) => any;
destroy?: () => void;
connect: () => any;
send: (message: any) => any;
}
export interface IConnectionParams {
encodeDecode: IEncoderDecoder;
protocol: IProtocol;
adapter: IAdapter;
onPushEvent?: (message: IMessageWOMsgId) => void;
}
export interface IMultiResponseParams {
payloadType: number;
payload: Object;
onMessage: (data) => boolean;
onError?: () => void;
onError?: (err?: any) => void;
}
export interface IEncoderDecoder {
encode: (params?: any) => any;
encode: (data: IDataToSend) => any;
decode: (params?: any) => any;
registerDecodeHandler: (handler: () => any) => any;
}
export interface IProtocol {
encode: (payloadType: number, payload: any, hatRes: any) => any;
decode: (params?: any) => any;
export interface IDataToSend {
payloadType: number;
payload: any;
msgId: number;
}
export declare class Connect extends EventEmitter {
private adapter;
private encodeDecode;
private protocol;
private _isConnected;
private connected;
private incomingMessagesListeners;
private handlePushEvent;
private callbacksOnConnect;
constructor(params: IConnectionParams);
getAdapter(): IAdapter;
updateAdapter(adapter: any): void;
private initialization();
start(): PromiseLike<void>;
private onData(data);
private onOpen();
sendGuaranteedCommand(payloadType: number, params: any): PromiseLike<any>;
sendCommand(payloadType: number, params: any): PromiseLike<any>;
private send(data);
private onMessage(data);
private onData(data);
private processData(clientMsgId, payloadType, msg);
protected isError(payloadType: any): boolean;
protected processMessage(command: any, msg: any, payloadType: any): void;
protected processPushEvent(msg: any, payloadType: any): void;
private _onEnd(e);
isDisconnected(): boolean;
Expand All @@ -72,4 +67,5 @@ export declare class Connect extends EventEmitter {
sendGuaranteedCommandWithPayloadtype(payloadType: number, payload: Object): PromiseLike<IMessageWOMsgId>;
onConnect(): void;
onEnd(e: any): void;
destroyAdapter(): void;
}
68 changes: 34 additions & 34 deletions lib/connect.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,20 @@ var Connect = (function (_super) {
__extends(Connect, _super);
function Connect(params) {
var _this = _super.call(this) || this;
_this._isConnected = false;
_this.connected = false;
_this.incomingMessagesListeners = [];
_this.callbacksOnConnect = [];
_this.encodeDecode = params.encodeDecode;
_this.protocol = params.protocol;
_this.handlePushEvent = params.onPushEvent;
_this.adapter = params.adapter;
_this.initialization();
return _this;
}
Connect.prototype.getAdapter = function () {
return this.adapter;
};
Connect.prototype.updateAdapter = function (adapter) {
if (this.adapter) {
this.destroyAdapter();
}
this.adapter = adapter;
};
Connect.prototype.initialization = function () {
this.encodeDecode.registerDecodeHandler(this.onMessage.bind(this));
};
Connect.prototype.start = function () {
var _this = this;
return new Promise(function (resolve, reject) {
Expand All @@ -45,11 +40,8 @@ var Connect = (function (_super) {
adapter.connect();
});
};
Connect.prototype.onData = function (data) {
this.encodeDecode.decode(data);
};
Connect.prototype.onOpen = function () {
this._isConnected = true;
this.connected = true;
this.onConnect();
this.callbacksOnConnect.forEach(function (fn) { return fn(); });
this.callbacksOnConnect = [];
Expand All @@ -61,10 +53,12 @@ var Connect = (function (_super) {
return this.sendCommandWithPayloadtype(payloadType, params).then(function (msg) { return msg.payload; });
};
Connect.prototype.send = function (data) {
this.adapter.send(this.encodeDecode.encode(data));
console.assert(this.adapter, 'Fatal: Adapter must be defined, use updateAdapter');
var encodedData = this.encodeDecode.encode(data);
this.adapter.send(encodedData);
};
Connect.prototype.onMessage = function (data) {
data = this.protocol.decode(data);
Connect.prototype.onData = function (data) {
data = this.encodeDecode.decode(data);
var msg = data.msg;
var payloadType = data.payloadType;
var clientMsgId = data.clientMsgId;
Expand Down Expand Up @@ -96,33 +90,25 @@ var Connect = (function (_super) {
//Overwrite this method by your buisness logic
return false;
};
Connect.prototype.processMessage = function (command, msg, payloadType) {
if (this.isError(payloadType)) {
command.fail(msg);
}
else {
command.done(msg);
}
};
Connect.prototype.processPushEvent = function (msg, payloadType) {
if (this.handlePushEvent) {
this.handlePushEvent({ payload: msg, payloadType: payloadType });
}
this.emit(payloadType, msg);
};
Connect.prototype._onEnd = function (e) {
this._isConnected = false;
this.connected = false;
this.incomingMessagesListeners.forEach(function (listener) {
listener.disconnectHandler();
});
this.incomingMessagesListeners = [];
this.onEnd(e);
};
Connect.prototype.isDisconnected = function () {
return !this._isConnected;
return !this.connected;
};
Connect.prototype.isConnected = function () {
return this._isConnected;
return this.connected;
};
Connect.prototype.addIncomingMessagesListener = function (fnToAdd) {
this.incomingMessagesListeners.push(fnToAdd);
Expand All @@ -131,7 +117,7 @@ var Connect = (function (_super) {
this.incomingMessagesListeners = this.incomingMessagesListeners.filter(function (fn) { return fn != fnToRemove; });
};
Connect.prototype.sendCommandWithoutResponse = function (payloadType, payload) {
this.send(this.protocol.encode(payloadType, payload, hat()));
this.send({ payloadType: payloadType, payload: payload, msgId: hat() });
};
Connect.prototype.sendMultiresponseCommand = function (multiResponseParams) {
var _this = this;
Expand All @@ -155,14 +141,14 @@ var Connect = (function (_super) {
this.addIncomingMessagesListener(incomingMessagesListener);
if (this.isConnected()) {
try {
this.send(this.protocol.encode(payloadType, payload, msgId));
this.send({ payloadType: payloadType, payload: payload, msgId: msgId });
}
catch (e) {
onError();
catch (err) {
onError(err);
}
}
else {
onError();
onError('Adapter not connected');
}
};
Connect.prototype.sendCommandWithPayloadtype = function (payloadType, payload) {
Expand All @@ -180,8 +166,8 @@ var Connect = (function (_super) {
}
return true;
},
onError: function () {
reject();
onError: function (err) {
reject(err);
}
});
});
Expand All @@ -204,6 +190,20 @@ var Connect = (function (_super) {
};
Connect.prototype.onEnd = function (e) {
};
Connect.prototype.destroyAdapter = function () {
this.adapter.onOpen = null;
this.adapter.onData = null;
this.adapter.onError = function () { };
if (this.adapter.onEnd) {
this.adapter.onEnd();
}
this.adapter.onEnd = function () { };
if (this.adapter.destroy) {
this.adapter.destroy();
}
this.adapter.destroy = function () { };
this.adapter = null;
};
return Connect;
}(events_1.EventEmitter));
exports.Connect = Connect;
Loading

0 comments on commit 1b84d47

Please sign in to comment.