*Add support for a network id
This commit is contained in:
parent
ad2830cba6
commit
9d04fa3933
80
src/index.ts
80
src/index.ts
|
@ -31,9 +31,40 @@ export class RpcNetwork {
|
|||
private _actionQueue: [string, any][] = [];
|
||||
private _addQueue: string[] = [];
|
||||
private _removeQueue: string[] = [];
|
||||
private _def: boolean;
|
||||
|
||||
constructor(def: boolean = true) {
|
||||
this._def = def;
|
||||
}
|
||||
|
||||
private _networkId: number = 0;
|
||||
|
||||
get networkId(): number {
|
||||
return this._networkId;
|
||||
}
|
||||
|
||||
get ready(): Promise<ErrTuple> {
|
||||
return loadLibs().then(() => callModule(RPC_MODULE, "ready"));
|
||||
let promise = loadLibs();
|
||||
|
||||
if (this._def) {
|
||||
this._networkId = 0;
|
||||
} else {
|
||||
promise = promise
|
||||
.then(() => callModule(RPC_MODULE, "createNetwork"))
|
||||
.then((ret: ErrTuple) => (this._networkId = ret[0]));
|
||||
}
|
||||
|
||||
return promise.then(() =>
|
||||
callModule(RPC_MODULE, "ready", { network: this._networkId })
|
||||
);
|
||||
}
|
||||
|
||||
private static deleteItem(array: Array<any>, item: string): void {
|
||||
if (array.includes(item)) {
|
||||
let queue = new Set(array);
|
||||
queue.delete(item);
|
||||
[].splice.apply(array, [0, array.length].concat([...queue]) as any);
|
||||
}
|
||||
}
|
||||
|
||||
public addRelay(pubkey: string): void {
|
||||
|
@ -52,14 +83,6 @@ export class RpcNetwork {
|
|||
this._actionQueue.push(["clearRelays", {}]);
|
||||
}
|
||||
|
||||
private static deleteItem(array: Array<any>, item: string): void {
|
||||
if (array.includes(item)) {
|
||||
let queue = new Set(array);
|
||||
queue.delete(item);
|
||||
[].splice.apply(array, [0, array.length].concat([...queue]) as any);
|
||||
}
|
||||
}
|
||||
|
||||
public wisdomQuery(
|
||||
method: string,
|
||||
module: string,
|
||||
|
@ -118,18 +141,27 @@ export class RpcNetwork {
|
|||
await loadLibs();
|
||||
for (const action of this._actionQueue) {
|
||||
try {
|
||||
await callModule(RPC_MODULE, action[0], action[1]);
|
||||
await callModule(RPC_MODULE, action[0], {
|
||||
...action[1],
|
||||
network: this._networkId,
|
||||
});
|
||||
} catch (e: any) {}
|
||||
}
|
||||
|
||||
await Promise.allSettled(
|
||||
this._removeQueue.map((item: string) =>
|
||||
callModule(RPC_MODULE, "removeRelay", { pubkey: item })
|
||||
callModule(RPC_MODULE, "removeRelay", {
|
||||
pubkey: item,
|
||||
network: this._networkId,
|
||||
})
|
||||
)
|
||||
);
|
||||
await Promise.allSettled(
|
||||
this._addQueue.map((item: string) =>
|
||||
callModule(RPC_MODULE, "addRelay", { pubkey: item })
|
||||
callModule(RPC_MODULE, "addRelay", {
|
||||
pubkey: item,
|
||||
network: this._networkId,
|
||||
})
|
||||
)
|
||||
);
|
||||
|
||||
|
@ -158,17 +190,6 @@ export abstract class RpcQueryBase {
|
|||
this._queryType = queryType;
|
||||
}
|
||||
|
||||
public run(): this {
|
||||
this._promise = this._network.processQueue().then(() =>
|
||||
callModule(RPC_MODULE, this._queryType, {
|
||||
query: this._query,
|
||||
options: this._options,
|
||||
})
|
||||
);
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
get result(): Promise<RPCResponse> {
|
||||
return (this._promise as Promise<any>).then((result): RPCResponse => {
|
||||
if (result[1]) {
|
||||
|
@ -177,6 +198,18 @@ export abstract class RpcQueryBase {
|
|||
return result[0];
|
||||
});
|
||||
}
|
||||
|
||||
public run(): this {
|
||||
this._promise = this._network.processQueue().then(() =>
|
||||
callModule(RPC_MODULE, this._queryType, {
|
||||
query: this._query,
|
||||
options: this._options,
|
||||
network: this._network.networkId,
|
||||
})
|
||||
);
|
||||
|
||||
return this;
|
||||
}
|
||||
}
|
||||
|
||||
export class SimpleRpcQuery extends RpcQueryBase {
|
||||
|
@ -211,6 +244,7 @@ export class StreamingRpcQuery extends RpcQueryBase {
|
|||
{
|
||||
query: this._query,
|
||||
options: this._options,
|
||||
network: this._network.networkId,
|
||||
},
|
||||
this._options.streamHandler
|
||||
)
|
||||
|
|
Loading…
Reference in New Issue