Pull in observable API from UI (#175)

* Pull in observable api...

* Progress changing stuff...

* votingBalance

* Update resul types

* Expose toBn on Moment

* Rework... WIP

* ... More type cleanups

* Linting passes

* BN helpers for UInt

* Update api-observable

* Add api-observable package.json

* Allow Struct/Tuple to create from hex string

* Future-proof Length with bitLength (u64/u128 compant encoding PR out)

* Make Compact number encoding shareable

* Split Api into functional sections

* Cleanup types

* Additional per-use comments

* Remove params/primitives from README

* api-format  -> api-codec

* Whitespace.
This commit is contained in:
Jaco Greeff
2018-10-01 11:22:15 +02:00
committed by GitHub
parent 465d986de1
commit 782bb8063c
24 changed files with 1010 additions and 68 deletions
+74
View File
@@ -0,0 +1,74 @@
// Copyright 2017-2018 @polkadot/ui-observable authors & contributors
// This software may be modified and distributed under the terms
// of the ISC license. See the LICENSE file for details.
import { RxApiInterface, RxApiInterface$Method, RxApiInterface$Section } from '@polkadot/api-rx/types';
import { Method } from '@polkadot/jsonrpc/types';
import { Observable, combineLatest } from 'rxjs';
import { defaultIfEmpty, map } from 'rxjs/operators';
import { StorageFunction } from '@polkadot/api-codec/StorageKey';
import assert from '@polkadot/util/assert';
import isUndefined from '@polkadot/util/is/undefined';
type MapFn<R, T> = (combined: R) => T;
const defaultMapFn = (result: any): any =>
result;
// Raw base implementation for the observable API. It simply provides access to raw calls, allowing
// decendants to make direct queries to either API methods or actual storage
export default class ApiBase {
protected api: RxApiInterface;
constructor (api: RxApiInterface) {
this.api = api;
}
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)
);
}
isConnected = (): Observable<boolean> => {
return this.api.isConnected();
}
rawCall = <T> ({ name, section }: Method, ...params: Array<any>): Observable<T> => {
const apiSection = this.api[section as keyof RxApiInterface] as RxApiInterface$Section;
assert(apiSection, `Unable to find 'api.${section}'`);
const fn: RxApiInterface$Method = apiSection[name];
assert(fn, `Unable to find 'api.${section}.${name}'`);
return fn.apply(null, params);
}
rawStorage = <T> (key: StorageFunction, ...params: Array<any>): Observable<T> => {
return this
.rawStorageMulti([key, ...params] as [StorageFunction, any])
.pipe(
// @ts-ignore After upgrade to 6.3.2
map(([result]: Array<T>): T =>
result
)
);
}
rawStorageMulti = <T> (...keys: Array<[StorageFunction] | [StorageFunction, any]>): Observable<T> => {
return this.api.state
.storage(keys)
.pipe(
map((result?: any) =>
isUndefined(result)
? []
: result
)
);
}
}