2022-07-20 07:36:21 +00:00
|
|
|
const RPC_MODULE = "AQDaEPIo_lpdvz7AKbeafERBHR331RiyvweJ6OrFTplzyg";
|
|
|
|
let callModule, connectModule;
|
2022-07-21 04:58:47 +00:00
|
|
|
async function loadLibs() {
|
|
|
|
if (callModule && connectModule) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (typeof window !== "undefined" && window?.document) {
|
|
|
|
const pkg = await import("libkernel");
|
|
|
|
callModule = pkg.callModule;
|
|
|
|
connectModule = pkg.connectModule;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
const pkg = await import("libkmodule");
|
|
|
|
callModule = pkg.callModule;
|
|
|
|
connectModule = pkg.connectModule;
|
|
|
|
}
|
2022-07-20 07:36:21 +00:00
|
|
|
}
|
|
|
|
export class RpcNetwork {
|
|
|
|
_actionQueue = [];
|
2022-07-21 04:58:47 +00:00
|
|
|
_addQueue = [];
|
|
|
|
_removeQueue = [];
|
2022-08-31 18:42:49 +00:00
|
|
|
_def;
|
|
|
|
constructor(def = true) {
|
|
|
|
this._def = def;
|
|
|
|
}
|
|
|
|
_networkId = 0;
|
|
|
|
get networkId() {
|
|
|
|
return this._networkId;
|
|
|
|
}
|
2022-07-20 07:36:21 +00:00
|
|
|
get ready() {
|
2022-08-31 18:42:49 +00:00
|
|
|
let promise = loadLibs();
|
|
|
|
if (this._def) {
|
2022-08-31 19:25:36 +00:00
|
|
|
this._networkId = 1;
|
2022-08-31 18:42:49 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
promise = promise
|
|
|
|
.then(() => callModule(RPC_MODULE, "createNetwork"))
|
|
|
|
.then((ret) => (this._networkId = ret[0]));
|
|
|
|
}
|
|
|
|
return promise.then(() => callModule(RPC_MODULE, "ready", { network: this._networkId }));
|
|
|
|
}
|
|
|
|
static deleteItem(array, item) {
|
|
|
|
if (array.includes(item)) {
|
|
|
|
let queue = new Set(array);
|
|
|
|
queue.delete(item);
|
|
|
|
[].splice.apply(array, [0, array.length].concat([...queue]));
|
|
|
|
}
|
2022-07-20 07:36:21 +00:00
|
|
|
}
|
|
|
|
addRelay(pubkey) {
|
2022-07-21 04:58:47 +00:00
|
|
|
this._addQueue.push(pubkey);
|
|
|
|
this._addQueue = [...new Set(this._addQueue)];
|
|
|
|
RpcNetwork.deleteItem(this._removeQueue, pubkey);
|
2022-07-20 07:36:21 +00:00
|
|
|
}
|
|
|
|
removeRelay(pubkey) {
|
2022-07-21 04:58:47 +00:00
|
|
|
this._removeQueue.push(pubkey);
|
|
|
|
this._removeQueue = [...new Set(this._removeQueue)];
|
|
|
|
RpcNetwork.deleteItem(this._addQueue, pubkey);
|
2022-07-20 07:36:21 +00:00
|
|
|
}
|
|
|
|
clearRelays() {
|
2022-07-21 04:58:47 +00:00
|
|
|
this._actionQueue.push(["clearRelays", {}]);
|
|
|
|
}
|
2022-08-31 01:39:52 +00:00
|
|
|
wisdomQuery(method, module, data = {}, bypassCache = false, options = {}) {
|
|
|
|
return new WisdomRpcQuery(this, {
|
|
|
|
method,
|
|
|
|
module,
|
2022-07-20 07:36:21 +00:00
|
|
|
data,
|
2022-08-31 01:39:52 +00:00
|
|
|
bypassCache,
|
|
|
|
}, options).run();
|
|
|
|
}
|
|
|
|
streamingQuery(relay, method, module, streamHandler, data = {}, options = {}) {
|
|
|
|
return new StreamingRpcQuery(this, relay, { method, module, data }, { streamHandler, ...options }).run();
|
|
|
|
}
|
|
|
|
simpleQuery(relay, method, module, data = {}, options = {}) {
|
|
|
|
return new SimpleRpcQuery(this, relay, {
|
|
|
|
method,
|
|
|
|
module,
|
|
|
|
data,
|
|
|
|
}, options).run();
|
2022-07-20 07:36:21 +00:00
|
|
|
}
|
|
|
|
async processQueue() {
|
2022-07-21 04:58:47 +00:00
|
|
|
await loadLibs();
|
|
|
|
for (const action of this._actionQueue) {
|
2022-07-20 08:45:36 +00:00
|
|
|
try {
|
2022-08-31 18:42:49 +00:00
|
|
|
await callModule(RPC_MODULE, action[0], {
|
|
|
|
...action[1],
|
|
|
|
network: this._networkId,
|
|
|
|
});
|
2022-07-20 08:45:36 +00:00
|
|
|
}
|
2022-07-20 21:22:25 +00:00
|
|
|
catch (e) { }
|
2022-07-20 08:45:36 +00:00
|
|
|
}
|
2022-08-31 18:42:49 +00:00
|
|
|
await Promise.allSettled(this._removeQueue.map((item) => callModule(RPC_MODULE, "removeRelay", {
|
|
|
|
pubkey: item,
|
|
|
|
network: this._networkId,
|
|
|
|
})));
|
|
|
|
await Promise.allSettled(this._addQueue.map((item) => callModule(RPC_MODULE, "addRelay", {
|
|
|
|
pubkey: item,
|
|
|
|
network: this._networkId,
|
|
|
|
})));
|
2022-07-20 07:36:21 +00:00
|
|
|
this._actionQueue = [];
|
2022-07-21 04:58:47 +00:00
|
|
|
this._removeQueue = [];
|
|
|
|
this._addQueue = [];
|
2022-07-20 07:36:21 +00:00
|
|
|
}
|
|
|
|
}
|
2022-08-31 01:39:52 +00:00
|
|
|
export class RpcQueryBase {
|
2022-07-20 07:36:21 +00:00
|
|
|
_promise;
|
2022-08-31 01:39:52 +00:00
|
|
|
_network;
|
|
|
|
_query;
|
|
|
|
_options;
|
|
|
|
_queryType;
|
|
|
|
constructor(network, query, options = {}, queryType) {
|
|
|
|
this._network = network;
|
|
|
|
this._query = query;
|
|
|
|
this._options = options;
|
|
|
|
this._queryType = queryType;
|
|
|
|
}
|
2022-07-20 07:36:21 +00:00
|
|
|
get result() {
|
|
|
|
return this._promise.then((result) => {
|
|
|
|
if (result[1]) {
|
2022-08-31 02:18:07 +00:00
|
|
|
return { error: result[1] };
|
2022-07-20 07:36:21 +00:00
|
|
|
}
|
|
|
|
return result[0];
|
|
|
|
});
|
|
|
|
}
|
2022-08-31 18:42:49 +00:00
|
|
|
run() {
|
|
|
|
this._promise = this._network.processQueue().then(() => callModule(RPC_MODULE, this._queryType, {
|
|
|
|
query: this._query,
|
|
|
|
options: this._options,
|
|
|
|
network: this._network.networkId,
|
|
|
|
}));
|
|
|
|
return this;
|
|
|
|
}
|
2022-07-20 07:36:21 +00:00
|
|
|
}
|
2022-08-31 01:39:52 +00:00
|
|
|
export class SimpleRpcQuery extends RpcQueryBase {
|
2022-08-31 21:33:29 +00:00
|
|
|
_relay;
|
2022-08-31 01:39:52 +00:00
|
|
|
constructor(network, relay, query, options) {
|
|
|
|
super(network, query, options, "simpleQuery");
|
2022-08-31 21:33:29 +00:00
|
|
|
this._relay = relay;
|
|
|
|
}
|
|
|
|
run() {
|
|
|
|
this._promise = this._network.processQueue().then(() => callModule(RPC_MODULE, this._queryType, {
|
|
|
|
relay: this._relay,
|
|
|
|
query: this._query,
|
|
|
|
options: this._options,
|
|
|
|
network: this._network.networkId,
|
|
|
|
}));
|
|
|
|
return this;
|
2022-08-31 01:39:52 +00:00
|
|
|
}
|
|
|
|
}
|
2022-08-31 21:33:29 +00:00
|
|
|
export class StreamingRpcQuery extends SimpleRpcQuery {
|
2022-08-31 01:39:52 +00:00
|
|
|
_options;
|
|
|
|
constructor(network, relay, query, options) {
|
2022-08-31 21:33:29 +00:00
|
|
|
super(network, relay, query, options);
|
2022-08-31 01:39:52 +00:00
|
|
|
this._options = options;
|
2022-08-31 21:33:29 +00:00
|
|
|
this._queryType = "streamingQuery";
|
2022-08-31 01:39:52 +00:00
|
|
|
}
|
|
|
|
run() {
|
2022-08-31 01:49:59 +00:00
|
|
|
this._promise = this._network.processQueue().then(() => connectModule(RPC_MODULE, this._queryType, {
|
|
|
|
query: this._query,
|
2022-08-31 23:05:01 +00:00
|
|
|
options: { ...this._options, streamHandler: true },
|
2022-08-31 18:42:49 +00:00
|
|
|
network: this._network.networkId,
|
2022-08-31 01:49:59 +00:00
|
|
|
}, this._options.streamHandler));
|
2022-08-31 01:39:52 +00:00
|
|
|
return this;
|
|
|
|
}
|
2022-08-31 23:15:57 +00:00
|
|
|
get result() {
|
|
|
|
return this._promise
|
|
|
|
.then((result) => result[1])
|
|
|
|
.then((response) => {
|
|
|
|
if (response[1]) {
|
|
|
|
return { error: response[1] };
|
|
|
|
}
|
|
|
|
return response[0];
|
|
|
|
});
|
|
|
|
}
|
2022-08-31 01:39:52 +00:00
|
|
|
}
|
|
|
|
export class WisdomRpcQuery extends RpcQueryBase {
|
|
|
|
constructor(network, query, options = {}) {
|
|
|
|
super(network, query, options, "wisdomQuery");
|
|
|
|
}
|
|
|
|
}
|