Skip to content

Commit

Permalink
Update websocket.awaitable.js
Browse files Browse the repository at this point in the history
  • Loading branch information
HeyHamachi committed Feb 17, 2024
1 parent a7d6c68 commit 7d68b35
Showing 1 changed file with 3 additions and 6 deletions.
9 changes: 3 additions & 6 deletions src/js/websocket.awaitable.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@ export class AwaitbleWebSocket {
this.socket = new WebSocket(url);
this.messagePool = {};
this.isOpen = false;
let _resolve_ = () => { };

this.socket.addEventListener("open", (event) => {
this.isOpen = true;
_resolve_(this);
});
this.socket.addEventListener("message", (event) => {
let data = JSON.parse(event.data);
Expand All @@ -26,12 +28,7 @@ export class AwaitbleWebSocket {
});

return new Promise((resolve, reject) => {
let interval_id = setInterval(() => {
if (this.isOpen) {
resolve(this);
clearInterval(interval_id);
}
}, 10);
this.isOpen ? resolve(this) : _resolve_ = resolve;
});
}

Expand Down

0 comments on commit 7d68b35

Please sign in to comment.