Minor readability enhancements (#1313)
* Minor readability enhancements * Split unwieldly ApiBase * Fix import case * Some movements, init in root * Simplify overloaded functions * Some more reworks * Hoist non-class functions * Flatten * Split decorate and init * Split meta from chain * getNext cleanup
This commit is contained in:
@@ -2,10 +2,10 @@
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||
|
||||
import { BlockNumber, Header } from '@polkadot/types/interfaces';
|
||||
import { BlockNumber } from '@polkadot/types/interfaces';
|
||||
|
||||
import { Observable } from 'rxjs';
|
||||
import { filter, map } from 'rxjs/operators';
|
||||
import { map } from 'rxjs/operators';
|
||||
import { ApiInterfaceRx } from '@polkadot/api/types';
|
||||
|
||||
import { drr } from '../util/drr';
|
||||
@@ -24,10 +24,8 @@ import { drr } from '../util/drr';
|
||||
*/
|
||||
export function bestNumber (api: ApiInterfaceRx): () => Observable<BlockNumber> {
|
||||
return (): Observable<BlockNumber> =>
|
||||
api.rpc.chain.subscribeNewHead()
|
||||
.pipe(
|
||||
filter((header: Header): boolean => !!header && !!header.number),
|
||||
map((header: Header): BlockNumber => header.number.unwrap()),
|
||||
drr()
|
||||
);
|
||||
api.rpc.chain.subscribeNewHead().pipe(
|
||||
map((header): BlockNumber => header.number.unwrap()),
|
||||
drr()
|
||||
);
|
||||
}
|
||||
|
||||
@@ -2,10 +2,10 @@
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||
|
||||
import { BlockNumber, Header } from '@polkadot/types/interfaces';
|
||||
import { BlockNumber } from '@polkadot/types/interfaces';
|
||||
|
||||
import { Observable } from 'rxjs';
|
||||
import { filter, map } from 'rxjs/operators';
|
||||
import { map } from 'rxjs/operators';
|
||||
import { ApiInterfaceRx } from '@polkadot/api/types';
|
||||
|
||||
import { drr } from '../util/drr';
|
||||
@@ -25,10 +25,8 @@ import { drr } from '../util/drr';
|
||||
*/
|
||||
export function bestNumberFinalized (api: ApiInterfaceRx): () => Observable<BlockNumber> {
|
||||
return (): Observable<BlockNumber> =>
|
||||
api.rpc.chain.subscribeFinalizedHeads()
|
||||
.pipe(
|
||||
filter((header: Header): boolean => !!header && !!header.number),
|
||||
map((header: Header): BlockNumber => header.number.unwrap()),
|
||||
drr()
|
||||
);
|
||||
api.rpc.chain.subscribeFinalizedHeads().pipe(
|
||||
map((header): BlockNumber => header.number.unwrap()),
|
||||
drr()
|
||||
);
|
||||
}
|
||||
|
||||
@@ -6,7 +6,7 @@ import { ApiInterfaceRx } from '@polkadot/api/types';
|
||||
import { AccountId, Header } from '@polkadot/types/interfaces';
|
||||
|
||||
import { Observable, combineLatest, of } from 'rxjs';
|
||||
import { filter, map, switchMap } from 'rxjs/operators';
|
||||
import { map, switchMap } from 'rxjs/operators';
|
||||
|
||||
import { HeaderExtended } from '../type';
|
||||
import { drr } from '../util/drr';
|
||||
@@ -28,23 +28,21 @@ export type HeaderAndValidators = [Header, AccountId[]];
|
||||
*/
|
||||
export function subscribeNewHead (api: ApiInterfaceRx): () => Observable<HeaderExtended> {
|
||||
return (): Observable<HeaderExtended> =>
|
||||
(api.rpc.chain.subscribeNewHead() as Observable<Header>)
|
||||
.pipe(
|
||||
filter((header: Header): boolean => !!header && !!header.number),
|
||||
switchMap((header: Header): Observable<HeaderAndValidators> =>
|
||||
(combineLatest([
|
||||
of(header),
|
||||
// theoretically we could combine at the first call with session.validators(), however
|
||||
// we make 100% sure we actually get the validators at a specific block so when these
|
||||
// change at an era boundary, we have the previous values to ensure our indexes are correct
|
||||
api.query.session
|
||||
? api.query.session.validators.at(header.hash)
|
||||
: of([])
|
||||
]) as Observable<HeaderAndValidators>)
|
||||
),
|
||||
map(([header, validators]): HeaderExtended =>
|
||||
new HeaderExtended(header, validators)
|
||||
),
|
||||
drr()
|
||||
);
|
||||
api.rpc.chain.subscribeNewHead().pipe(
|
||||
switchMap((header: Header): Observable<HeaderAndValidators> =>
|
||||
(combineLatest([
|
||||
of(header),
|
||||
// theoretically we could combine at the first call with session.validators(), however
|
||||
// we make 100% sure we actually get the validators at a specific block so when these
|
||||
// change at an era boundary, we have the previous values to ensure our indexes are correct
|
||||
api.query.session
|
||||
? api.query.session.validators.at(header.hash)
|
||||
: of([])
|
||||
]) as Observable<HeaderAndValidators>)
|
||||
),
|
||||
map(([header, validators]): HeaderExtended =>
|
||||
new HeaderExtended(header, validators)
|
||||
),
|
||||
drr()
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user