diff --git a/packages/api/src/base/Decorate.ts b/packages/api/src/base/Decorate.ts index 49c205a65e..76a485fc0d 100644 --- a/packages/api/src/base/Decorate.ts +++ b/packages/api/src/base/Decorate.ts @@ -174,6 +174,13 @@ export abstract class Decorate 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 extends Events { }, input as DecoratedRpc); } + // only be called if supportMulti is true protected _decorateMulti (decorateMethod: DecorateMethod): QueryableStorageMulti { // eslint-disable-next-line @typescript-eslint/no-unsafe-return return decorateMethod((calls: QueryableStorageMultiArg[]): Observable => - this._rpcCore.state.subscribeStorage( + (this.hasSubscriptions + ? this._rpcCore.state.subscribeStorage + : this._rpcCore.state.queryStorageAt)( calls.map((arg: QueryableStorageMultiArg) => Array.isArray(arg) ? [arg[0].creator, ...arg.slice(1)] @@ -403,8 +413,7 @@ export abstract class Decorate 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 => this._retrieveMulti(args.map((arg) => [creator, arg]))); @@ -454,7 +463,10 @@ export abstract class Decorate extends Events { ...Array(Math.ceil(keys.length / PAGE_SIZE_VALS)) .fill(0) .map((_, index): Observable => - 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[] => diff --git a/packages/api/src/base/Init.ts b/packages/api/src/base/Init.ts index 809aca0002..0a3a968761 100644 --- a/packages/api/src/base/Init.ts +++ b/packages/api/src/base/Init.ts @@ -54,8 +54,12 @@ export abstract class Init extends Decorate { 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));