Return ReferendumIndex along with info (derive) (#669)
* Return ReferendumIndex along with info (derive) * Rename index * Doc rename
This commit is contained in:
@@ -0,0 +1,44 @@
|
||||
// Copyright 2017-2019 @polkadot/api-derive authors & contributors
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||
|
||||
import BN from 'bn.js';
|
||||
import { Observable } from 'rxjs';
|
||||
import { map } from 'rxjs/operators';
|
||||
import { ApiInterface$Rx } from '@polkadot/api/types';
|
||||
import { ReferendumInfo, ReferendumIndex } from '@polkadot/types/index';
|
||||
|
||||
import { drr } from '../util/drr';
|
||||
|
||||
/**
|
||||
* @name ReferendumInfoExtended
|
||||
* @description
|
||||
* A [[ReferendumInfo]] with an additional `index` field
|
||||
*/
|
||||
export class ReferendumInfoExtended extends ReferendumInfo {
|
||||
private _index: ReferendumIndex;
|
||||
|
||||
constructor (info: ReferendumInfo, index: BN | number) {
|
||||
super(info);
|
||||
|
||||
this._index = new ReferendumIndex(index);
|
||||
}
|
||||
|
||||
/**
|
||||
* @description Convenience getter, returns the referendumIndex
|
||||
*/
|
||||
get index (): ReferendumIndex {
|
||||
return this._index;
|
||||
}
|
||||
}
|
||||
|
||||
export function referendumInfo (api: ApiInterface$Rx) {
|
||||
return (id: BN | number): Observable<ReferendumInfoExtended> => {
|
||||
return (api.query.democracy.referendumInfoOf(id) as Observable<ReferendumInfo>)
|
||||
.pipe(
|
||||
map((info) => new ReferendumInfoExtended(info, id)),
|
||||
drr()
|
||||
);
|
||||
};
|
||||
|
||||
}
|
||||
@@ -4,22 +4,19 @@
|
||||
|
||||
import BN from 'bn.js';
|
||||
import { combineLatest, Observable, of } from 'rxjs';
|
||||
import { filter } from 'rxjs/operators';
|
||||
import { ApiInterface$Rx } from '@polkadot/api/types';
|
||||
import { ReferendumInfo } from '@polkadot/types/index';
|
||||
|
||||
import { drr } from '../util/drr';
|
||||
import { referendumInfo, ReferendumInfoExtended } from './referendumInfo';
|
||||
|
||||
export function referendumInfos (api: ApiInterface$Rx) {
|
||||
return (ids: Array<BN | number> = []): Observable<Array<ReferendumInfo>> => {
|
||||
const referendumInfoOf = referendumInfo(api);
|
||||
|
||||
return (ids: Array<BN | number> = []): Observable<Array<ReferendumInfoExtended>> => {
|
||||
return !ids || !ids.length
|
||||
? of([]).pipe(drr())
|
||||
: combineLatest(
|
||||
ids.map(
|
||||
(id) => (api.query.democracy.referendumInfoOf(id) as Observable<ReferendumInfo>).pipe(
|
||||
filter((info) => !!info)
|
||||
)
|
||||
)
|
||||
ids.map((id) => referendumInfoOf(id))
|
||||
).pipe(
|
||||
drr()
|
||||
);
|
||||
|
||||
@@ -5,13 +5,14 @@
|
||||
import { combineLatest, Observable, of } from 'rxjs';
|
||||
import { switchMap } from 'rxjs/operators';
|
||||
import { ApiInterface$Rx } from '@polkadot/api/types';
|
||||
import { ReferendumIndex, ReferendumInfo } from '@polkadot/types/index';
|
||||
import { ReferendumIndex } from '@polkadot/types/index';
|
||||
|
||||
import { referendumInfos } from './referendumInfos';
|
||||
import { drr } from '../util/drr';
|
||||
import { ReferendumInfoExtended } from './referendumInfo';
|
||||
import { referendumInfos } from './referendumInfos';
|
||||
|
||||
export function referendums (api: ApiInterface$Rx) {
|
||||
return (): Observable<Array<ReferendumInfo>> =>
|
||||
return (): Observable<Array<ReferendumInfoExtended>> =>
|
||||
(combineLatest(
|
||||
api.query.democracy.nextTally(),
|
||||
api.query.democracy.referendumCount()
|
||||
|
||||
Reference in New Issue
Block a user