Expose whenReady and extrinsics/storage (#199)

* Expose whenReady and extrinsics/storage

* Load genesisHash, expose

* Fix import

* whenReady -> Observable<boolean>

* Check isConnected before making call

* Fix import order
This commit is contained in:
Jaco Greeff
2018-10-04 11:16:45 +02:00
committed by GitHub
parent 507377e2e2
commit 02aba14e7e
16 changed files with 68 additions and 24 deletions
+52 -6
View File
@@ -5,8 +5,13 @@
import { RxApiInterface, RxApiInterface$Method, RxApiInterface$Section } from '@polkadot/api-rx/types';
import { Method } from '@polkadot/jsonrpc/types';
import { Observable, combineLatest } from 'rxjs';
import { 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 { Hash } from '@polkadot/types/index';
import { StorageFunction } from '@polkadot/types/StorageKey';
import assert from '@polkadot/util/assert';
import isUndefined from '@polkadot/util/is/undefined';
@@ -19,10 +24,47 @@ const defaultMapFn = (result: any): any =>
// 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;
protected _api: RxApiInterface;
protected _genesisHash: Hash;
// Observable that returns the first time we are connected and loaded
whenReady: Observable<boolean>;
constructor (api: RxApiInterface) {
this.api = api;
this._api = api;
this._genesisHash = new Hash();
this.whenReady = from(this.init());
}
static extrinsics = extrinsicsStatic;
static storage = storageStatic;
private init (): Promise<boolean> {
let isReady: boolean = false;
return new Promise((resolveReady) => {
// On connection, load data from chain
this.isConnected().subscribe(async (isConnected) => {
if (!isConnected) {
return;
}
try {
const meta = await this._api.state.getMetadata().toPromise();
this._genesisHash = await this._api.state.getBlockHash(0).toPromise();
ApiBase.extrinsics = extrinsicsFromMeta(meta);
ApiBase.storage = storageFromMeta(meta);
if (!isReady) {
isReady = true;
resolveReady(true);
}
} catch (error) {
// swallow
}
});
});
}
protected combine = <T, R> (observables: Array<Observable<any>>, mapfn: MapFn<R, T> = defaultMapFn): Observable<T> => {
@@ -33,12 +75,16 @@ export default class ApiBase {
);
}
get genesisHash (): Hash {
return this._genesisHash;
}
isConnected = (): Observable<boolean> => {
return this.api.isConnected();
return this._api.isConnected();
}
rawCall = <T> ({ method, section }: Method, ...params: Array<any>): Observable<T> => {
const apiSection = this.api[section as keyof RxApiInterface] as RxApiInterface$Section;
const apiSection = this._api[section as keyof RxApiInterface] as RxApiInterface$Section;
assert(apiSection, `Unable to find 'api.${section}'`);
@@ -61,7 +107,7 @@ export default class ApiBase {
}
rawStorageMulti = <T> (...keys: Array<[StorageFunction] | [StorageFunction, any]>): Observable<T> => {
return this.api.state
return this._api.state
.storage(keys)
.pipe(
map((result?: any) =>