2022-11-27 23:22:49 +00:00
|
|
|
import ProtomuxRPC from "protomux-rpc";
|
2022-11-14 16:09:02 +00:00
|
|
|
// @ts-ignore
|
2022-11-27 23:22:49 +00:00
|
|
|
import c from "compact-encoding";
|
|
|
|
import b4a from "b4a";
|
2022-11-14 16:09:02 +00:00
|
|
|
|
2022-11-27 23:22:49 +00:00
|
|
|
const ID = b4a.from("lumeweb");
|
2022-11-14 16:09:02 +00:00
|
|
|
|
|
|
|
export default class RPC extends ProtomuxRPC {
|
2023-07-04 08:33:19 +00:00
|
|
|
private _ready: Promise<void>;
|
|
|
|
|
2022-11-27 23:22:49 +00:00
|
|
|
constructor(stream: any, options = {}) {
|
|
|
|
options = {
|
|
|
|
...{
|
|
|
|
id: ID,
|
|
|
|
valueEncoding: c.json,
|
|
|
|
},
|
|
|
|
...options,
|
|
|
|
};
|
|
|
|
super(stream, options);
|
2023-07-23 17:54:42 +00:00
|
|
|
|
|
|
|
this._ready = new Promise((resolve, reject) => {
|
|
|
|
stream.on("open", onopen);
|
|
|
|
stream.on("destroy", ondestroy);
|
|
|
|
|
|
|
|
function onopen(handshake) {
|
|
|
|
removeListener();
|
|
|
|
resolve(handshake);
|
|
|
|
}
|
|
|
|
|
|
|
|
function ondestroy() {
|
|
|
|
removeListener();
|
|
|
|
reject(new Error("Client could not connect"));
|
|
|
|
}
|
|
|
|
|
|
|
|
function removeListener() {
|
|
|
|
stream.off("open", onopen);
|
|
|
|
stream.off("destroy", ondestroy);
|
|
|
|
}
|
2023-07-04 08:33:19 +00:00
|
|
|
});
|
2023-07-23 18:08:18 +00:00
|
|
|
|
|
|
|
stream.setKeepAlive(5000);
|
2022-11-27 23:22:49 +00:00
|
|
|
}
|
2022-11-27 23:22:12 +00:00
|
|
|
|
2022-11-28 04:59:33 +00:00
|
|
|
async request(method: any, value: any | string = "", options = {}) {
|
2022-11-27 23:22:49 +00:00
|
|
|
return super.request(method, value, options);
|
|
|
|
}
|
2023-07-04 08:33:19 +00:00
|
|
|
|
|
|
|
get ready(): Promise<void> {
|
|
|
|
return this._ready;
|
|
|
|
}
|
2022-11-14 16:09:02 +00:00
|
|
|
}
|