Additional derive cleanups (#4307)

* Additional derive cleanups

* Adjust

* Multi cache set

* Adjust mapping
This commit is contained in:
Jaco
2021-12-09 12:12:05 +02:00
committed by GitHub
parent 584cbb994b
commit e32ac213b9
21 changed files with 129 additions and 154 deletions
+16 -3
View File
@@ -1,18 +1,19 @@
// Copyright 2017-2021 @polkadot/api-derive authors & contributors
// SPDX-License-Identifier: Apache-2.0
import type { ApiInterfaceRx } from '@polkadot/api/types';
import type { Observable } from 'rxjs';
import type { ApiInterfaceRx, ObsInnerType } from '@polkadot/api/types';
import type { EraIndex } from '@polkadot/types/interfaces';
import type { ExactDerive } from '../bundle';
import { switchMap } from 'rxjs';
import { combineLatest, of, switchMap } from 'rxjs';
import { memo } from '../util';
type ApplyReturn<T extends keyof ExactDerive['staking']> = ReturnType<ExactDerive['staking'][T]>;
export function filterEras <T extends { era: EraIndex }> (eras: EraIndex[], list: T[]): EraIndex[] {
return eras.filter((era) => !list.some((entry) => era.eq(entry.era)));
return eras.filter((e) => !list.some(({ era }) => e.eq(era)));
}
export function erasHistoricApply <F extends '_erasExposure' | '_erasPoints' | '_erasPrefs' | '_erasRewards' | '_erasSlashes'> (fn: F): (instanceId: string, api: ApiInterfaceRx) => (withActive?: boolean) => ApplyReturn<F> {
@@ -36,3 +37,15 @@ export function erasHistoricApplyAccount <F extends '_ownExposures' | '_ownSlash
)
) as any;
}
export function combineEras <F extends '_eraExposure' | '_eraPrefs' | '_eraSlashes'> (fn: F): (instanceId: string, api: ApiInterfaceRx) => (eras: EraIndex[], withActive: boolean) => Observable<ObsInnerType<ApplyReturn<F>>[]> {
return (instanceId: string, api: ApiInterfaceRx) =>
// Cannot quite get the typing right, but it is right in the code
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
memo(instanceId, (eras: EraIndex[], withActive: boolean) =>
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
eras.length
? combineLatest(eras.map((e) => api.derive.staking[fn](e, withActive)))
: of([])
) as any;
}