Option construction & ReferendumInfoExtended (#720)

* Option construction & ReferendumInfoExtended

* Simplify, optional index
This commit is contained in:
Jaco Greeff
2019-02-19 17:46:22 +01:00
committed by GitHub
parent 0e4671fc5b
commit 447ab2fde0
2 changed files with 19 additions and 9 deletions
@@ -7,6 +7,7 @@ import { Observable } from 'rxjs';
import { map } from 'rxjs/operators';
import { ApiInterface$Rx } from '@polkadot/api/types';
import { Option, ReferendumInfo, ReferendumIndex } from '@polkadot/types/index';
import { isNull } from '@polkadot/util';
import { drr } from '../util/drr';
@@ -16,17 +17,21 @@ import { drr } from '../util/drr';
* A [[ReferendumInfo]] with an additional `index` field
*/
export class ReferendumInfoExtended extends ReferendumInfo {
constructor (value: ReferendumInfo) {
private _index: ReferendumIndex;
constructor (value: ReferendumInfo | ReferendumInfoExtended, index?: BN | number) {
super(value);
this.set('index', value.get('index') as ReferendumIndex);
this._index = value instanceof ReferendumInfoExtended
? value.index
: new ReferendumIndex(index);
}
/**
* @description Convenience getter, returns the referendumIndex
*/
get index (): ReferendumIndex {
return this.get('index') as ReferendumIndex;
return this._index;
}
}
@@ -37,11 +42,12 @@ export function referendumInfo (api: ApiInterface$Rx) {
map((optionInfo) => {
const info = optionInfo.unwrapOr(null);
if (info) {
info.set('index', new ReferendumIndex(index));
}
return new Option<ReferendumInfoExtended>(ReferendumInfoExtended, info);
return new Option<ReferendumInfoExtended>(
ReferendumInfoExtended,
isNull(info)
? null
: new ReferendumInfoExtended(info, index)
);
}),
drr()
);
+5 -1
View File
@@ -34,7 +34,11 @@ export default class Option<T extends Codec> extends Base<T> implements Codec {
return isNull(value) || isUndefined(value) || value instanceof Null
? new Null()
: new Type(value);
: (
value instanceof Type
? value
: new Type(value)
);
}
static with<O extends Codec> (Type: Constructor): Constructor<Option<O>> {