* Bump common (w/ rxjs 7) * Adjust * pre bump * Cleanup imports * Add missing switchMap * CHANGELOG * Remove duplicate imports * Bump common * Direct rxjs import
40 lines
1.3 KiB
TypeScript
40 lines
1.3 KiB
TypeScript
// Copyright 2017-2021 @polkadot/api-derive authors & contributors
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
import type { Observable } from 'rxjs';
|
|
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 { DeriveProposalExternal } from '../types';
|
|
|
|
import { map, of, switchMap } from 'rxjs';
|
|
|
|
import { memo } from '../util';
|
|
|
|
function withImage (api: ApiInterfaceRx, nextOpt: Option<ITuple<[Hash, VoteThreshold]>>): Observable<DeriveProposalExternal | null> {
|
|
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<DeriveProposalExternal | null> {
|
|
return memo(instanceId, (): Observable<DeriveProposalExternal | null> =>
|
|
api.query.democracy?.nextExternal
|
|
? api.query.democracy.nextExternal<Option<ITuple<[Hash, VoteThreshold]>>>().pipe(
|
|
switchMap((nextOpt) => withImage(api, nextOpt))
|
|
)
|
|
: of(null)
|
|
);
|
|
}
|