Update api.derive to use api.queryMulti (#911)

* session api.queryMulti

* Make a single subscription to retrieve all session info

* Adjustments

* WIP

* Seems mostly comprehensive, need to full test e2e

* e2e tests for operations

* Allow "create from empty"

* skip

* Remove comment

* Remove ancient ?'s

* Update types.ts
This commit is contained in:
Jaco Greeff
2019-05-16 11:36:41 +02:00
committed by GitHub
parent bafda6dc93
commit 2dfc55cb5c
16 changed files with 237 additions and 132 deletions
@@ -3,28 +3,17 @@
// of the Apache-2.0 license. See the LICENSE file for details.
import BN from 'bn.js';
import { combineLatest, Observable } from 'rxjs';
import { Observable } from 'rxjs';
import { map } from 'rxjs/operators';
import { ApiInterface$Rx } from '@polkadot/api/types';
import { BlockNumber, Option } from '@polkadot/types';
import { bestNumber } from '../chain';
import { drr } from '../util/drr';
import { info } from './info';
export function sessionProgress (api: ApiInterface$Rx) {
return (): Observable<BN> =>
combineLatest([
bestNumber(api)(),
api.query.session.sessionLength(),
api.query.session.lastLengthChange()
]).pipe(
map(
([bestNumber, sessionLength, lastLengthChange]) =>
(bestNumber || new BN(0))
.sub((lastLengthChange as Option<BlockNumber>).unwrapOr(new BN(0)))
.add(sessionLength as BlockNumber || new BN(1))
.mod(sessionLength as BlockNumber || new BN(1))
),
info(api)().pipe(
map(({ sessionProgress }) => sessionProgress),
drr()
);
}