*Add new query type just for clearing a query hash

This commit is contained in:
Derrick Hammer 2022-12-04 05:38:46 -05:00
parent 144e19e635
commit 5e1c52352e
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
2 changed files with 65 additions and 1 deletions

View File

@ -5,6 +5,8 @@ import RPC from "@lumeweb/rpc";
import { isPromise } from "./util.js";
import SimpleRpcQuery from "./query/simple.js";
import WisdomRpcQuery from "./query/wisdom.js";
import ClearCacheRpcQuery from "./query/clearCache.js";
import { RpcQueryOptions } from "./types.js";
export default class RpcNetwork {
constructor(dht = new DHT()) {
@ -127,7 +129,7 @@ export default class RpcNetwork {
module: string,
data: object | any[] = {},
bypassCache: boolean = false,
options: {}
options: RpcQueryOptions = {}
): SimpleRpcQuery {
return new SimpleRpcQuery(
this,
@ -141,4 +143,23 @@ export default class RpcNetwork {
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();
}
}

43
src/query/clearCache.ts Normal file
View File

@ -0,0 +1,43 @@
import RpcNetwork from "../network.js";
import { RPCBroadcastRequest, RPCRequest } from "@lumeweb/relay-types";
import { RpcQueryOptions } from "../types.js";
import { hashQuery } from "../util.js";
import { getActiveRelay, setupRelay } from "../sharedRelay.js";
import SimpleRpcQuery from "./simple.js";
export default class ClearCacheRpcQuery extends SimpleRpcQuery {
protected _relays: string[];
constructor(
network: RpcNetwork,
relays: string[],
query: RPCRequest,
options: RpcQueryOptions
) {
super(network, "", query, options);
this._relays = relays;
}
protected async _run(): Promise<void> {
await setupRelay(this._network);
// @ts-ignore
this._relay = getActiveRelay().stream.remotePublicKey;
await this.queryRelay();
await this.checkResponses();
}
protected async queryRelay(): Promise<any> {
return this.queryRpc(getActiveRelay(), {
module: "rpc",
method: "broadcast_request",
data: {
request: {
module: "rpc",
method: "clear_cached_item",
data: hashQuery(this._query),
},
relays: this._relays,
} as RPCBroadcastRequest,
});
}
}