Skip to content

Commit

Permalink
Fix guaranteedCommand
Browse files Browse the repository at this point in the history
  • Loading branch information
madroneropaulo committed Mar 2, 2017
1 parent 7c80c83 commit f791b31
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 5 deletions.
1 change: 1 addition & 0 deletions lib/connect.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ export declare class Connect extends EventEmitter {
sendCommandWithoutResponse(payloadType: number, payload: Object): void;
sendMultiresponseCommand(multiResponseParams: IMultiResponseParams): void;
sendCommandWithPayloadtype(payloadType: number, payload: Object): Promise<IMessageWOMsgId>;
sendGuaranteedMultiresponseCommand(payloadType: number, payload: Object): Promise<IMessageWOMsgId>;
sendGuaranteedCommandWithPayloadtype(payloadType: number, payload: Object): Promise<IMessageWOMsgId>;
onConnect(): void;
onEnd(e: any): void;
Expand Down
25 changes: 23 additions & 2 deletions lib/connect.js
Original file line number Diff line number Diff line change
Expand Up @@ -175,15 +175,36 @@ var Connect = (function (_super) {
});
});
};
Connect.prototype.sendGuaranteedMultiresponseCommand = function (payloadType, payload) {
var _this = this;
return new Promise(function (resolve, reject) {
_this.sendMultiresponseCommand({
payloadType: payloadType,
payload: payload,
onMessage: function (result) {
if (_this.isError(result.payloadType)) {
reject(result);
}
else {
resolve(result);
}
return true;
},
onError: function (err) {
_this.sendGuaranteedMultiresponseCommand(payloadType, payload).then(resolve, reject);
}
});
});
};
Connect.prototype.sendGuaranteedCommandWithPayloadtype = function (payloadType, payload) {
var _this = this;
if (this.isConnected()) {
return this.sendCommandWithPayloadtype(payloadType, payload);
return this.sendGuaranteedMultiresponseCommand(payloadType, payload);
}
else {
return new Promise(function (resolve, reject) {
_this.callbacksOnConnect.push(function () {
_this.sendCommandWithPayloadtype(payloadType, payload)
_this.sendGuaranteedMultiresponseCommand(payloadType, payload)
.then(resolve, reject);
});
});
Expand Down
24 changes: 22 additions & 2 deletions lib/connect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -246,13 +246,33 @@ export class Connect extends EventEmitter {
});
}

public sendGuaranteedMultiresponseCommand(payloadType: number, payload: Object): Promise<IMessageWOMsgId> {
return new Promise((resolve, reject) => {
this.sendMultiresponseCommand({
payloadType,
payload,
onMessage: result => {
if (this.isError(result.payloadType)) {
reject(result);
} else {
resolve(result);
}
return true;
},
onError: (err) => {
this.sendGuaranteedMultiresponseCommand(payloadType, payload).then(resolve, reject);
}
});
});
}

public sendGuaranteedCommandWithPayloadtype(payloadType: number, payload: Object): Promise<IMessageWOMsgId> {
if (this.isConnected()) {
return this.sendCommandWithPayloadtype(payloadType, payload);
return this.sendGuaranteedMultiresponseCommand(payloadType, payload);
} else {
return new Promise((resolve, reject) => {
this.callbacksOnConnect.push(() => {
this.sendCommandWithPayloadtype(payloadType, payload)
this.sendGuaranteedMultiresponseCommand(payloadType, payload)
.then(resolve, reject);
});
});
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "connect-ts-api",
"version": "2.2.0",
"version": "2.3.0",
"main": "lib/connect.js",
"scripts": {
"lint": "tslint **/*.ts",
Expand Down

0 comments on commit f791b31

Please sign in to comment.