kernel-rpc-client/dist/index.js

84 lines
2.2 KiB
JavaScript
Raw Normal View History

2023-03-23 17:03:01 +00:00
import { Client, factory } from "@lumeweb/libkernel-universal";
2023-04-05 20:03:33 +00:00
const RPC_MODULE = "fABcMA-oh1MzTKyYCX3KUAG2f-tfGyxYF_tXjlv9jPyq3Q";
2023-03-23 16:59:48 +00:00
export class RpcNetwork extends Client {
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
});
}
2023-03-24 17:39:50 +00:00
simpleQuery({ relay, query, options = {}, }) {
2023-03-24 19:58:40 +00:00
return createSimpleRpcQuery({
2023-03-23 16:59:48 +00:00
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;
}
}
2023-03-23 17:03:01 +00:00
export const createClient = factory(RpcNetwork, RPC_MODULE);
2023-03-24 19:58:40 +00:00
const createSimpleRpcQuery = factory(SimpleRpcQuery, RPC_MODULE);