Skip to content

Commit

Permalink
fix(core): reopen websocket
Browse files Browse the repository at this point in the history
  • Loading branch information
Hanssen0 committed Oct 12, 2024
1 parent 557ad55 commit a3d5359
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 44 deletions.
5 changes: 5 additions & 0 deletions .changeset/brave-keys-relate.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@ckb-ccc/core": patch
---

fix(core): reopen websocket
90 changes: 46 additions & 44 deletions packages/core/src/client/transports/webSocket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,63 +10,65 @@ export class TransportWebSocket implements Transport {
ReturnType<typeof setTimeout>,
]
> = new Map();
private socket?: Promise<WebSocket>;
private socket?: WebSocket;
private openSocket?: Promise<WebSocket>;

constructor(
private readonly url: string,
private readonly timeout = 30000,
) {}

request(data: JsonRpcPayload) {
const socket = (this.socket ?? Promise.resolve(undefined)).then(
(existed) => {
if (
existed &&
existed.readyState !== existed.CLOSING &&
existed.readyState !== existed.CLOSED
) {
return existed;
const socket = (() => {
if (
this.socket &&
this.socket.readyState !== this.socket.CLOSING &&
this.socket.readyState !== this.socket.CLOSED &&
this.openSocket
) {
return this.openSocket;
}
const socket = new WebSocket(this.url);
const onMessage = ({ data }: { data: string }) => {
const res = JSON.parse(data);
if (typeof res !== "object" || res === null) {
throw new Error(`Unknown response ${data}`);
}

const socket = new WebSocket(this.url);
const onMessage = ({ data }: { data: string }) => {
const res = JSON.parse(data);
if (typeof res !== "object" || res === null) {
throw new Error(`Unknown response ${data}`);
}
const req = this.ongoing.get(res.id);
if (!req) {
return;
}
const [resolve, _, timeout] = req;
clearTimeout(timeout);
this.ongoing.delete(res.id);

const req = this.ongoing.get(res.id);
if (!req) {
return;
}
const [resolve, _, timeout] = req;
resolve(res);
};
const onClose = () => {
this.ongoing.forEach(([_, reject, timeout]) => {
clearTimeout(timeout);
this.ongoing.delete(res.id);

resolve(res);
};
const onClose = () => {
this.ongoing.forEach(([_, reject, timeout]) => {
clearTimeout(timeout);
reject(new Error("Connection closed"));
});
this.ongoing.clear();
};
reject(new Error("Connection closed"));
});
this.ongoing.clear();
};

socket.onclose = onClose;
socket.onerror = onClose;
socket.onmessage = onMessage;
socket.onclose = onClose;
socket.onerror = onClose;
socket.onmessage = onMessage;

this.socket = new Promise((resolve) => {
if (socket.readyState === socket.OPEN) {
resolve(undefined);
} else {
socket.onopen = resolve;
}
}).then(() => socket);
return this.socket;
},
);
this.socket = socket;
this.openSocket = new Promise<WebSocket>((resolve) => {
if (socket.readyState === socket.OPEN) {
resolve(socket);
} else {
socket.onopen = () => {
resolve(socket);
};
}
});
return this.openSocket;
})();

return new Promise((resolve, reject) => {
const req: [
Expand Down

0 comments on commit a3d5359

Please sign in to comment.