Skip to content
This repository has been archived by the owner on Feb 9, 2022. It is now read-only.

Commit

Permalink
fix responses resolving more than one awaiter promise (#65)
Browse files Browse the repository at this point in the history
fix responses resolving more than one awaiter promise
hopefully fixing #32
  • Loading branch information
RAnders00 authored Aug 31, 2020
1 parent 37a6fbc commit 52934bc
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 2 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## Unversioned

- Bugfix: Fixed a single server message resolving/rejecting more than one promise (e.g. cases where many messages were sent to the same channel, the first success/error response would resolve/reject all the waiting promises) (#32, #65)

## v4.0.1

- Bugfix: Fixed exception if new unstable "flags" parsing failed because of an out-of-bounds index.
Expand Down
8 changes: 6 additions & 2 deletions lib/await/await-response.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,6 @@ export class ResponseAwaiter {
});

this.subscribeTo("close", this.onConnectionClosed);
this.subscribeTo("message", this.onConnectionMessage);
this.joinPendingResponsesQueue();
}

Expand Down Expand Up @@ -199,12 +198,17 @@ export class ResponseAwaiter {
}
}

private onConnectionMessage(msg: IRCMessage): void {
// returns true if something matched, preventing "later" matchers from
// running against that message
public onConnectionMessage(msg: IRCMessage): boolean {
if (this.config.failure(msg)) {
this.reject(new MessageError(`Bad response message: ${msg.rawSource}`));
return true;
} else if (this.config.success(msg)) {
this.resolve(msg);
return true;
}
return false;
}

private subscribeTo(
Expand Down
9 changes: 9 additions & 0 deletions lib/client/connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,15 @@ export class SingleConnection extends BaseClient {

replyToServerPing(this);
handleReconnectMessage(this);

this.on("message", (msg) => {
for (const awaiter of this.pendingResponses) {
const stop = awaiter.onConnectionMessage(msg);
if (stop) {
break;
}
}
});
}

public connect(): void {
Expand Down

0 comments on commit 52934bc

Please sign in to comment.