// Copyright 2017-2021 @polkadot/api-derive authors & contributors // SPDX-License-Identifier: Apache-2.0 import type { ApiInterfaceRx } from '@polkadot/api/types'; import type { Option } from '@polkadot/types'; import type { Hash, VoteThreshold } from '@polkadot/types/interfaces'; import type { ITuple } from '@polkadot/types/types'; import type { Observable } from '@polkadot/x-rxjs'; import type { DeriveProposalExternal } from '../types'; import { of } from '@polkadot/x-rxjs'; import { map, switchMap } from '@polkadot/x-rxjs/operators'; 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) ); }