2022-07-20 07:36:21 +00:00
|
|
|
import { callModule as callModuleKernel, connectModule as connectModuleKernel, } from "libkernel";
|
|
|
|
import { callModule as callModuleModule, connectModule as connectModuleModule, } from "libkmodule";
|
|
|
|
const RPC_MODULE = "AQDaEPIo_lpdvz7AKbeafERBHR331RiyvweJ6OrFTplzyg";
|
|
|
|
let callModule, connectModule;
|
2022-07-20 21:22:25 +00:00
|
|
|
if (typeof window !== "undefined" && window?.document) {
|
2022-07-20 07:36:21 +00:00
|
|
|
callModule = callModuleKernel;
|
|
|
|
connectModule = connectModuleKernel;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
callModule = callModuleModule;
|
|
|
|
connectModule = connectModuleModule;
|
|
|
|
}
|
|
|
|
export class RpcNetwork {
|
|
|
|
_actionQueue = [];
|
|
|
|
get ready() {
|
|
|
|
return callModule(RPC_MODULE, "ready");
|
|
|
|
}
|
|
|
|
addRelay(pubkey) {
|
|
|
|
this._actionQueue.push(callModule(RPC_MODULE, "addRelay", { pubkey }));
|
|
|
|
}
|
|
|
|
removeRelay(pubkey) {
|
|
|
|
this._actionQueue.push(callModule(RPC_MODULE, "removeRelay", { pubkey }));
|
|
|
|
}
|
|
|
|
clearRelays() {
|
|
|
|
this._actionQueue.push(callModule(RPC_MODULE, "clearRelays"));
|
|
|
|
}
|
|
|
|
query(query, chain, data = {}, force = false) {
|
|
|
|
return new RpcQuery(this, {
|
|
|
|
query,
|
|
|
|
chain,
|
|
|
|
data,
|
|
|
|
force: force,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
async processQueue() {
|
2022-07-20 08:45:36 +00:00
|
|
|
for (const promise in this._actionQueue) {
|
|
|
|
try {
|
|
|
|
await promise;
|
|
|
|
}
|
2022-07-20 21:22:25 +00:00
|
|
|
catch (e) { }
|
2022-07-20 08:45:36 +00:00
|
|
|
}
|
2022-07-20 07:36:21 +00:00
|
|
|
this._actionQueue = [];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
export class RpcQuery {
|
|
|
|
_promise;
|
|
|
|
constructor(network, query) {
|
|
|
|
this._promise = network
|
|
|
|
.processQueue()
|
|
|
|
.then(() => callModule(RPC_MODULE, "query", query));
|
|
|
|
}
|
|
|
|
get result() {
|
|
|
|
return this._promise.then((result) => {
|
|
|
|
if (result[1]) {
|
|
|
|
throw new Error(result[1]);
|
|
|
|
}
|
|
|
|
return result[0];
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|