Http provider support multi (#2950)
* use queryStorageAt for multi storage queries * fix lint * e2e tests * skip lints for api.spec.ts * remove e2e test from PR * do not decorate multi() if can not supported * empty line * update Co-authored-by: Ian He <ian.he@centrality.ai>
This commit is contained in:
@@ -174,6 +174,13 @@ export abstract class Decorate<ApiType extends ApiTypes> extends Events {
|
||||
return this._rpcCore.provider.hasSubscriptions;
|
||||
}
|
||||
|
||||
/**
|
||||
* @returns `true` if the API decorate multi-key queries
|
||||
*/
|
||||
get supportMulti (): boolean {
|
||||
return this._rpcCore.provider.hasSubscriptions || !!this._rpcCore.state.queryStorageAt;
|
||||
}
|
||||
|
||||
public injectMetadata (metadata: Metadata, fromEmpty?: boolean, registry?: Registry): void {
|
||||
const decoratedMeta = expandMetadata(registry || this.#registry, metadata);
|
||||
|
||||
@@ -300,10 +307,13 @@ export abstract class Decorate<ApiType extends ApiTypes> extends Events {
|
||||
}, input as DecoratedRpc<ApiType, RpcInterface>);
|
||||
}
|
||||
|
||||
// only be called if supportMulti is true
|
||||
protected _decorateMulti<ApiType extends ApiTypes> (decorateMethod: DecorateMethod<ApiType>): QueryableStorageMulti<ApiType> {
|
||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
|
||||
return decorateMethod((calls: QueryableStorageMultiArg<ApiType>[]): Observable<Codec[]> =>
|
||||
this._rpcCore.state.subscribeStorage(
|
||||
(this.hasSubscriptions
|
||||
? this._rpcCore.state.subscribeStorage
|
||||
: this._rpcCore.state.queryStorageAt)(
|
||||
calls.map((arg: QueryableStorageMultiArg<ApiType>) =>
|
||||
Array.isArray(arg)
|
||||
? [arg[0].creator, ...arg.slice(1)]
|
||||
@@ -403,8 +413,7 @@ export abstract class Decorate<ApiType extends ApiTypes> extends Events {
|
||||
this._retrieveMapKeysPaged(creator, opts)));
|
||||
}
|
||||
|
||||
// only support multi where subs are available
|
||||
if (this.hasSubscriptions) {
|
||||
if (this.supportMulti) {
|
||||
// When using double map storage function, user need to pass double map key as an array
|
||||
decorated.multi = decorateMethod((args: (Arg | Arg[])[]): Observable<Codec[]> =>
|
||||
this._retrieveMulti(args.map((arg) => [creator, arg])));
|
||||
@@ -454,7 +463,10 @@ export abstract class Decorate<ApiType extends ApiTypes> extends Events {
|
||||
...Array(Math.ceil(keys.length / PAGE_SIZE_VALS))
|
||||
.fill(0)
|
||||
.map((_, index): Observable<Codec[]> =>
|
||||
this._rpcCore.state.subscribeStorage(keys.slice(index * PAGE_SIZE_VALS, (index * PAGE_SIZE_VALS) + PAGE_SIZE_VALS))
|
||||
(this.hasSubscriptions
|
||||
? this._rpcCore.state.subscribeStorage
|
||||
: this._rpcCore.state.queryStorageAt
|
||||
)(keys.slice(index * PAGE_SIZE_VALS, (index * PAGE_SIZE_VALS) + PAGE_SIZE_VALS))
|
||||
)
|
||||
).pipe(
|
||||
map((valsArr): Codec[] =>
|
||||
|
||||
@@ -54,8 +54,12 @@ export abstract class Init<ApiType extends ApiTypes> extends Decorate<ApiType> {
|
||||
|
||||
this._rpc = this._decorateRpc(this._rpcCore, this._decorateMethod);
|
||||
this._rx.rpc = this._decorateRpc(this._rpcCore, this._rxDecorateMethod);
|
||||
this._queryMulti = this._decorateMulti(this._decorateMethod);
|
||||
this._rx.queryMulti = this._decorateMulti(this._rxDecorateMethod);
|
||||
|
||||
if (this.supportMulti) {
|
||||
this._queryMulti = this._decorateMulti(this._decorateMethod);
|
||||
this._rx.queryMulti = this._decorateMulti(this._rxDecorateMethod);
|
||||
}
|
||||
|
||||
this._rx.signer = options.signer;
|
||||
|
||||
this._rpcCore.setRegistrySwap((hash: string | Uint8Array) => this.getBlockRegistry(hash));
|
||||
|
||||
Reference in New Issue
Block a user