// Copyright 2017-2020 @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 { Hash, VoteThreshold } from '@polkadot/types/interfaces'; import { ITuple } from '@polkadot/types/types'; import { DeriveProposalExternal } from '../types'; import { Observable, of } from 'rxjs'; import { map, switchMap } from 'rxjs/operators'; import { ApiInterfaceRx } from '@polkadot/api/types'; import { Option } from '@polkadot/types'; import { memo } from '../util'; function withImage (api: ApiInterfaceRx, nextOpt: Option>): Observable { if (nextOpt.isNone) { return of(null); } const [imageHash, threshold] = nextOpt.unwrap(); return api.derive.democracy.preimage(imageHash).pipe( map((image): DeriveProposalExternal => ({ image, imageHash, threshold })) ); } export function nextExternal (instanceId: string, api: ApiInterfaceRx): () => Observable { return memo(instanceId, (): Observable => api.query.democracy?.nextExternal ? api.query.democracy.nextExternal>>().pipe( switchMap((nextOpt) => withImage(api, nextOpt)) ) : of(null) ); }