Update api-observable to UI version (#189)

* Update api-observable to UI version

* Cleanup lint
This commit is contained in:
Jaco Greeff
2018-10-04 14:14:54 +02:00
committed by GitHub
parent 67a3e75f31
commit 605f11d768
5 changed files with 91 additions and 66 deletions
+24 -15
View File
@@ -5,12 +5,13 @@
import { RxApiInterface, RxApiInterface$Method, RxApiInterface$Section } from '@polkadot/api-rx/types';
import { Method } from '@polkadot/jsonrpc/types';
import { Observable, combineLatest, from } from 'rxjs';
import { EMPTY, Observable, combineLatest, from } from 'rxjs';
import { defaultIfEmpty, map } from 'rxjs/operators';
import extrinsicsFromMeta from '@polkadot/extrinsics/fromMetadata';
import extrinsicsStatic from '@polkadot/extrinsics/static';
import storageFromMeta from '@polkadot/storage/fromMetadata';
import storageStatic from '@polkadot/storage/static';
import { Vector } from '@polkadot/types/codec';
import { Hash } from '@polkadot/types/index';
import { StorageFunction } from '@polkadot/types/StorageKey';
import assert from '@polkadot/util/assert';
@@ -69,7 +70,6 @@ export default class ApiBase {
protected combine = <T, R> (observables: Array<Observable<any>>, mapfn: MapFn<R, T> = defaultMapFn): Observable<T> => {
return combineLatest(...observables).pipe(
// FIXME There are a couple of places now where this casting happens after rxjs 6.3.2
defaultIfEmpty([] as any),
map(mapfn)
);
@@ -95,26 +95,35 @@ export default class ApiBase {
return fn.apply(null, params);
}
rawStorage = <T> (key: StorageFunction, ...params: Array<any>): Observable<T> => {
rawStorage = <T> (key: StorageFunction, ...params: Array<any>): Observable<T | undefined> => {
return this
.rawStorageMulti([key, ...params] as [StorageFunction, any])
.pipe(
// @ts-ignore After upgrade to 6.3.2
map(([result]: Array<T>): T =>
map((result: Array<T>): T | undefined =>
result
? result[0]
: undefined
)
);
}
rawStorageMulti = <T> (...keys: Array<[StorageFunction] | [StorageFunction, any]>): Observable<T> => {
return this._api.state
.storage(keys)
.pipe(
map((result?: any) =>
isUndefined(result)
? []
: result
)
);
rawStorageMulti = <T extends []> (...keys: Array<[StorageFunction] | [StorageFunction, any]>): Observable<T> => {
let observable;
try {
observable = this._api.state.storage(keys);
} catch (error) {
observable = EMPTY;
}
return observable.pipe(
defaultIfEmpty(),
map((result?: Vector<any>): T =>
isUndefined(result)
? [] as T
// FIXME When Vector extends Array, this mapping can be removed
: result.map((item: any) => item) as T
)
);
}
}