From 2eb09374bc5db1fe1ab50a23d7589044a4a91d19 Mon Sep 17 00:00:00 2001 From: Jaco Greeff Date: Thu, 18 Jun 2020 00:06:19 +0200 Subject: [PATCH] Map entries/keys cleanups (#2360) --- CHANGELOG.md | 4 +++ packages/api/src/base/Decorate.ts | 55 +++++++++++++------------------ 2 files changed, 26 insertions(+), 33 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a96cc20794..ad77696d50 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # CHANGELOG +## 1.20.0-beta.x + +- Cleanup map entries support with only Substrate 2.0-era RPCs (when introduced) + ## 1.19.1 Jun 16, 2020 - Improve typing on `.entries()` queries (Thanks to https://github.com/monitz87) diff --git a/packages/api/src/base/Decorate.ts b/packages/api/src/base/Decorate.ts index 5d5f849a53..438a9aaddc 100644 --- a/packages/api/src/base/Decorate.ts +++ b/packages/api/src/base/Decorate.ts @@ -11,7 +11,7 @@ import { ApiInterfaceRx, ApiOptions, ApiTypes, DecorateMethod, DecoratedRpc, Dec import BN from 'bn.js'; import { BehaviorSubject, Observable, combineLatest, of } from 'rxjs'; -import { map, switchMap, take, tap, toArray } from 'rxjs/operators'; +import { map, switchMap, tap, toArray } from 'rxjs/operators'; import decorateDerive, { ExactDerive } from '@polkadot/api-derive'; import { memo } from '@polkadot/api-derive/util'; import DecoratedMeta from '@polkadot/metadata/Decorated'; @@ -495,35 +495,28 @@ export default abstract class Decorate extends Events const headKey = iterKey(arg).toHex(); const startSubject = new BehaviorSubject(headKey); - return this._rpcCore.state.getKeysPaged - ? startSubject.pipe( - switchMap((startKey) => - this._rpcCore.state.getKeysPaged(headKey, PAGE_SIZE_KEYS, startKey).pipe( - map((keys) => keys.map((key) => key.setMeta(meta))) - ) - ), - tap((keys): void => { - keys.length === PAGE_SIZE_KEYS - ? startSubject.next(keys[PAGE_SIZE_KEYS - 1].toHex()) - : startSubject.complete(); - }), - toArray(), // toArray since we want to startSubject to be completed - map((keysArr: StorageKey[][]) => keysArr.reduce((result: StorageKey[], keys) => result.concat(keys), [])) - ) - : this._rpcCore.state.getKeys(headKey).pipe( - map((keys) => keys.map((key) => key.setMeta(meta))) - ); + return startSubject.pipe( + switchMap((startKey) => + this._rpcCore.state.getKeysPaged(headKey, PAGE_SIZE_KEYS, startKey).pipe( + map((keys) => keys.map((key) => key.setMeta(meta))) + ) + ), + tap((keys): void => { + keys.length === PAGE_SIZE_KEYS + ? startSubject.next(keys[PAGE_SIZE_KEYS - 1].toHex()) + : startSubject.complete(); + }), + toArray(), // toArray since we want to startSubject to be completed + map((keysArr: StorageKey[][]) => keysArr.reduce((result: StorageKey[], keys) => result.concat(keys), [])) + ); } private _retrieveMapKeysPaged ({ iterKey, meta }: StorageEntry, opts: PaginationOptions): Observable { assert(iterKey && (meta.type.isMap || meta.type.isDoubleMap), 'keys can only be retrieved on maps, linked maps and double maps'); const headKey = iterKey(opts.arg).toHex(); - const getKeysPaged = this._rpcCore.state.getKeysPaged; - assert(getKeysPaged, 'Pagination not supported by the chain'); - - return getKeysPaged(headKey, opts.pageSize, opts.startKey || headKey).pipe( + return this._rpcCore.state.getKeysPaged(headKey, opts.pageSize, opts.startKey || headKey).pipe( map((keys) => keys.map((key) => key.setMeta(meta))) ); } @@ -535,13 +528,11 @@ export default abstract class Decorate extends Events of(keys), ...Array(Math.ceil(keys.length / PAGE_SIZE_VALS)) .fill(0) - .map((_, index): Observable => { - const keyset = keys.slice(index * PAGE_SIZE_VALS, (index * PAGE_SIZE_VALS) + PAGE_SIZE_VALS); - - return this._rpcCore.state.queryStorageAt - ? this._rpcCore.state.queryStorageAt(keyset) - : this._rpcCore.state.subscribeStorage(keyset).pipe(take(1)); - }) + .map((_, index): Observable => + this._rpcCore.state.queryStorageAt( + keys.slice(index * PAGE_SIZE_VALS, (index * PAGE_SIZE_VALS) + PAGE_SIZE_VALS) + ) + ) ]) ), map(([keys, ...valsArr]): [StorageKey, Codec][] => @@ -557,9 +548,7 @@ export default abstract class Decorate extends Events switchMap((keys) => combineLatest<[StorageKey[], ...Codec[][]]>([ of(keys), - this._rpcCore.state.queryStorageAt - ? this._rpcCore.state.queryStorageAt(keys) - : this._rpcCore.state.subscribeStorage(keys).pipe(take(1)) + this._rpcCore.state.queryStorageAt(keys) ]) ), map(([keys, ...valsArr]): [StorageKey, Codec][] =>