Fix derive heartbeats to query via index (#1446)

This commit is contained in:
Jaco Greeff
2019-10-04 14:13:06 +02:00
committed by GitHub
parent 6b0e2fe919
commit d594149ed1
2 changed files with 17 additions and 15 deletions
@@ -7,7 +7,7 @@ import { AccountId, SessionIndex } from '@polkadot/types/interfaces';
import { of, Observable } from 'rxjs';
import { map, switchMap } from 'rxjs/operators';
import { ApiInterfaceRx } from '@polkadot/api/types';
import { Bytes } from '@polkadot/types';
import { Bytes, Vec } from '@polkadot/types';
import { drr } from '../util/drr';
@@ -17,21 +17,22 @@ import { drr } from '../util/drr';
export function receivedHeartbeats (api: ApiInterfaceRx): (addresses: (AccountId | string)[]) => Observable<boolean[]> {
return (addresses: (AccountId | string)[]): Observable<boolean[]> => {
return api.query.imOnline && api.query.imOnline.receivedHeartbeats
? api.query.session
.currentIndex<SessionIndex>()
.pipe(
switchMap((currentIndex): Observable<Bytes[]> =>
api.query.imOnline.receivedHeartbeats.multi(
addresses.map((address): [SessionIndex, AccountId | string] =>
[currentIndex, address]
)
? api.queryMulti<[SessionIndex, Vec<AccountId>]>([
api.query.session.currentIndex,
api.query.imOnline.keys
]).pipe(
switchMap(([currentIndex, keys]): Observable<Bytes[]> =>
api.query.imOnline.receivedHeartbeats.multi(
addresses.map((address): [SessionIndex, number] =>
[currentIndex, keys.indexOf(address)]
)
),
map((heartbeats): boolean[] =>
heartbeats.map((heartbeat): boolean => !heartbeat.isEmpty)
),
drr()
)
)
),
map((heartbeats): boolean[] =>
heartbeats.map((heartbeat): boolean => !heartbeat.isEmpty)
),
drr()
)
: of([...new Array(addresses.length).keys()].map((): boolean => false));
};
}