*refactor query api to use object bags and a dedicated factory at a factory object namespace

This commit is contained in:
Derrick Hammer 2022-12-04 06:35:57 -05:00
parent 27d396d969
commit 6429bd513c
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
5 changed files with 80 additions and 70 deletions

View File

@ -1,8 +1,3 @@
import RpcNetwork from "./network.js";
import RpcQueryBase from "./query/base.js";
import SimpleRpcQuery from "./query/simple.js";
import WisdomRpcQuery from "./query/wisdom.js";
export * from "./types.js"; export * from "./types.js";
export * from "./query/index.js";
export { RpcNetwork, RpcQueryBase, SimpleRpcQuery, WisdomRpcQuery }; export * from "./network.js";

View File

@ -7,12 +7,20 @@ import SimpleRpcQuery from "./query/simple.js";
import WisdomRpcQuery from "./query/wisdom.js"; import WisdomRpcQuery from "./query/wisdom.js";
import ClearCacheRpcQuery from "./query/clearCache.js"; import ClearCacheRpcQuery from "./query/clearCache.js";
import { RpcQueryOptions } from "./types.js"; import { RpcQueryOptions } from "./types.js";
import { ClientRPCRequest } from "@lumeweb/relay-types";
import RpcNetworkQueryFactory from "./query/index.js";
export default class RpcNetwork { export default class RpcNetwork {
constructor(dht = new DHT()) { constructor(dht = new DHT()) {
this._dht = dht; this._dht = dht;
} }
private _factory = new RpcNetworkQueryFactory(this);
get factory(): RpcNetworkQueryFactory {
return this._factory;
}
private _dht: typeof DHT; private _dht: typeof DHT;
get dht() { get dht() {
@ -104,62 +112,4 @@ export default class RpcNetwork {
public clearRelays(): void { public clearRelays(): void {
this._relays = []; this._relays = [];
} }
public wisdomQuery(
method: string,
module: string,
data: object | any[] = {},
bypassCache: boolean = false,
options = {}
): WisdomRpcQuery {
return new WisdomRpcQuery(
this,
{
method,
module,
data,
bypassCache: bypassCache || this._bypassCache,
},
options
).run();
}
public simpleQuery(
relay: string,
method: string,
module: string,
data: object | any[] = {},
bypassCache: boolean = false,
options: RpcQueryOptions = {}
): SimpleRpcQuery {
return new SimpleRpcQuery(
this,
relay,
{
method,
module,
data,
bypassCache: bypassCache || this._bypassCache,
},
options
).run();
}
public clearCacheQuery(
relays: string[],
method: string,
module: string,
data: object | any[] = {},
options: RpcQueryOptions = {}
): SimpleRpcQuery {
return new ClearCacheRpcQuery(
this,
relays,
{
method,
module,
data,
},
options
).run();
}
} }

66
src/query/index.ts Normal file
View File

@ -0,0 +1,66 @@
import { ClientRPCRequest, RPCRequest } from "@lumeweb/relay-types";
import { RpcQueryOptions } from "../types.js";
import WisdomRpcQuery from "./wisdom.js";
import SimpleRpcQuery from "./simple.js";
import ClearCacheRpcQuery from "./clearCache.js";
import RpcNetwork from "../network.js";
import RpcQueryBase from "./base.js";
export default class RpcNetworkQueryFactory {
private _network: RpcNetwork;
constructor(network: RpcNetwork) {
this._network = network;
}
wisdom({
query,
options = {},
}: {
query: ClientRPCRequest;
options?: RpcQueryOptions;
}): WisdomRpcQuery {
return new WisdomRpcQuery(
this._network,
{
...query,
bypassCache: query.bypassCache || this._network.bypassCache,
},
options
).run();
}
simple({
relay,
query,
options = {},
}: {
relay: string;
query: ClientRPCRequest;
options?: RpcQueryOptions;
}): SimpleRpcQuery {
return new SimpleRpcQuery(
this._network,
relay,
{
...query,
bypassCache: query.bypassCache || this._network.bypassCache,
},
options
).run();
}
clearCache({
relays,
query,
options = {},
}: {
relays: string[];
query: RPCRequest;
options?: RpcQueryOptions;
}): ClearCacheRpcQuery {
return new ClearCacheRpcQuery(this._network, relays, query, options).run();
}
}
export { RpcNetwork, RpcQueryBase, SimpleRpcQuery, WisdomRpcQuery };

View File

@ -19,6 +19,7 @@ import RpcQueryBase from "./base.js";
export default class SimpleRpcQuery extends RpcQueryBase { export default class SimpleRpcQuery extends RpcQueryBase {
protected _relay: string; protected _relay: string;
protected declare _query: ClientRPCRequest;
constructor( constructor(
network: RpcNetwork, network: RpcNetwork,

View File

@ -62,12 +62,10 @@ export default class WisdomRpcQuery extends RpcQueryBase {
if (this._query.bypassCache) { if (this._query.bypassCache) {
delete this._query.bypassCache; delete this._query.bypassCache;
const clearCacheQuery = this._network.clearCacheQuery( const clearCacheQuery = this._network.factory.clearCache({
relays, relays,
this._query.method, query: this._query,
this._query.module, });
this._query.data
);
await clearCacheQuery.result; await clearCacheQuery.result;
} }