2023-03-23 16:59:48 +00:00
|
|
|
import { Client } from "@lumeweb/libkernel-universal";
|
|
|
|
const RPC_MODULE = "fAAgZfXMqN3YOn0-b9DICt8OPsOFeWw3YKY2p84aytzBww";
|
|
|
|
export class RpcNetwork extends Client {
|
2022-07-20 07:36:21 +00:00
|
|
|
_actionQueue = [];
|
2022-07-21 04:58:47 +00:00
|
|
|
_addQueue = [];
|
|
|
|
_removeQueue = [];
|
2022-08-31 18:42:49 +00:00
|
|
|
_def;
|
|
|
|
constructor(def = true) {
|
2023-03-23 16:59:48 +00:00
|
|
|
super();
|
2022-08-31 18:42:49 +00:00
|
|
|
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
|
|
|
if (this._def) {
|
2022-08-31 19:25:36 +00:00
|
|
|
this._networkId = 1;
|
2022-08-31 18:42:49 +00:00
|
|
|
}
|
|
|
|
else {
|
2023-03-23 16:59:48 +00:00
|
|
|
Promise.resolve()
|
|
|
|
.then(() => this.callModuleReturn(RPC_MODULE, "createNetwork"))
|
2022-08-31 18:42:49 +00:00
|
|
|
.then((ret) => (this._networkId = ret[0]));
|
|
|
|
}
|
2023-03-23 16:59:48 +00:00
|
|
|
return this.callModuleReturn("ready", {
|
2022-08-31 18:42:49 +00:00
|
|
|
network: this._networkId,
|
2023-03-23 16:59:48 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
simpleQuery(relay, query, data = {}, options = {}) {
|
|
|
|
return new SimpleRpcQuery({
|
|
|
|
network: this,
|
|
|
|
relay,
|
|
|
|
query,
|
|
|
|
options,
|
|
|
|
}).run();
|
2022-07-20 07:36:21 +00:00
|
|
|
}
|
|
|
|
}
|
2023-03-23 16:59:48 +00:00
|
|
|
export class RpcQueryBase extends Client {
|
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) {
|
2023-03-23 16:59:48 +00:00
|
|
|
super();
|
2022-08-31 01:39:52 +00:00
|
|
|
this._network = network;
|
|
|
|
this._query = query;
|
|
|
|
this._options = options;
|
|
|
|
this._queryType = queryType;
|
|
|
|
}
|
2022-07-20 07:36:21 +00:00
|
|
|
get result() {
|
2023-03-23 16:59:48 +00:00
|
|
|
return this._promise
|
|
|
|
.then((result) => {
|
2022-07-20 07:36:21 +00:00
|
|
|
return result[0];
|
2023-03-23 16:59:48 +00:00
|
|
|
})
|
|
|
|
.catch((error) => {
|
|
|
|
return { error: error.message };
|
2022-07-20 07:36:21 +00:00
|
|
|
});
|
|
|
|
}
|
2022-08-31 18:42:49 +00:00
|
|
|
run() {
|
2023-03-23 16:59:48 +00:00
|
|
|
this._promise = this.callModule(this._queryType, {
|
2022-08-31 18:42:49 +00:00
|
|
|
query: this._query,
|
|
|
|
options: this._options,
|
|
|
|
network: this._network.networkId,
|
2023-03-23 16:59:48 +00:00
|
|
|
});
|
2022-08-31 18:42:49 +00:00
|
|
|
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;
|
2023-03-23 16:59:48 +00:00
|
|
|
constructor({ network, relay, query, options, }) {
|
2022-08-31 01:39:52 +00:00
|
|
|
super(network, query, options, "simpleQuery");
|
2022-08-31 21:33:29 +00:00
|
|
|
this._relay = relay;
|
|
|
|
}
|
|
|
|
run() {
|
2023-03-23 16:59:48 +00:00
|
|
|
this._promise = this.callModule(this._queryType, {
|
2022-08-31 21:33:29 +00:00
|
|
|
relay: this._relay,
|
|
|
|
query: this._query,
|
|
|
|
options: this._options,
|
|
|
|
network: this._network.networkId,
|
2022-09-01 00:14:48 +00:00
|
|
|
});
|
2022-08-31 01:39:52 +00:00
|
|
|
return this;
|
|
|
|
}
|
|
|
|
}
|