Simplify entries() decoding (#2159)

This commit is contained in:
Jaco Greeff
2020-04-13 12:33:24 +02:00
committed by GitHub
parent 0f08031792
commit eae573e6a2
3 changed files with 20 additions and 30 deletions
+8 -18
View File
@@ -477,7 +477,7 @@ export default abstract class Decorate<ApiType extends ApiTypes> extends Events
? startSubject.pipe(
switchMap((startKey) =>
this._rpcCore.state.getKeysPaged(headKey, PAGE_SIZE_KEYS, startKey).pipe(
map((keys) => keys.map((key) => key.decodeArgsFromMeta(meta)))
map((keys) => keys.map((key) => key.setMeta(meta)))
)
),
tap((keys): void => {
@@ -489,41 +489,31 @@ export default abstract class Decorate<ApiType extends ApiTypes> extends Events
map((keysArr: StorageKey[][]) => keysArr.reduce((result: StorageKey[], keys) => result.concat(keys), []))
)
: this._rpcCore.state.getKeys(headKey).pipe(
map((keys) => keys.map((key) => key.decodeArgsFromMeta(meta)))
map((keys) => keys.map((key) => key.setMeta(meta)))
);
}
private _retrieveMapEntries (entry: StorageEntry, arg?: Arg): Observable<[StorageKey, Codec][]> {
const outputType = unwrapStorageType(entry.meta.type, entry.meta.modifier.isOptional);
return this._retrieveMapKeys(entry, arg).pipe(
switchMap((keys): Observable<[StorageKey[], Option<Raw>[][]]> =>
switchMap((keys): Observable<[StorageKey[], Codec[][]]> =>
combineLatest([
of(keys),
from(Array(Math.ceil(keys.length / PAGE_SIZE_VALS)).fill(0)).pipe(
concatMap((_, index): Observable<Option<Raw>[]> => {
concatMap((_, index): Observable<Codec[]> => {
const keyset = keys.slice(index * PAGE_SIZE_VALS, (index * PAGE_SIZE_VALS) + PAGE_SIZE_VALS);
return this._rpcCore.state.queryStorageAt
? this._rpcCore.state.queryStorageAt<Option<Raw>[]>(keyset)
: this._rpcCore.state.subscribeStorage<Option<Raw>[]>(keyset).pipe(take(1));
? this._rpcCore.state.queryStorageAt<Codec[]>(keyset)
: this._rpcCore.state.subscribeStorage<Codec[]>(keyset).pipe(take(1));
}),
toArray()
)
])
),
map(([keys, valsArr]): [StorageKey, Codec][] => {
const vals = valsArr.reduce((result: Option<Raw>[], vals) => result.concat(vals), []);
const vals = valsArr.reduce((result: Codec[], vals) => result.concat(vals), []);
return keys.map((key, index): [StorageKey, Codec] => [
key,
this.createType(
outputType,
vals[index].isSome
? vals[index].unwrap().toHex()
: undefined
)
]);
return keys.map((key, index) => [key, vals[index]]);
})
);
}
+8 -12
View File
@@ -158,9 +158,9 @@ export default class StorageKey extends Bytes {
private _meta?: StorageEntryMetadataLatest;
private readonly _method?: string;
private _outputType: string;
private readonly _outputType: string;
private readonly _method?: string;
private readonly _section?: string;
@@ -169,13 +169,12 @@ export default class StorageKey extends Bytes {
super(registry, key);
this._meta = StorageKey.getMeta(value as StorageKey);
this._method = override.method || method;
this._outputType = StorageKey.getType(value as StorageKey);
this._section = override.section || section;
this._outputType = StorageKey.getType(value as StorageKey);
// decode the args (as applicable based on the key and the hashers, after all init)
this.decodeArgsFromMeta();
this.setMeta(StorageKey.getMeta(value as StorageKey));
}
public static getMeta (value: StorageKey | StorageEntry | [StorageEntry, any]): StorageEntryMetadataLatest | undefined {
@@ -250,15 +249,12 @@ export default class StorageKey extends Bytes {
public setMeta (meta?: StorageEntryMetadataLatest): this {
this._meta = meta;
return this.decodeArgsFromMeta();
}
if (meta) {
this._outputType = unwrapStorageType(meta.type);
}
/**
* @description Decode the args embedded in the key (assuming we have decodable hashers)
*/
public decodeArgsFromMeta (meta?: StorageEntryMetadataLatest): this {
try {
this._args = decodeArgsFromMeta(this.registry, this.toU8a(true), meta || this.meta);
this._args = decodeArgsFromMeta(this.registry, this.toU8a(true), this.meta);
} catch (error) {
// ignore...
}