Additional derive cleanups (#4308)

* Additional derive cleanups

* unwrapNumber

* Adjust
This commit is contained in:
Jaco
2021-12-09 12:54:51 +02:00
committed by GitHub
parent 23717bb180
commit 969151c4a7
8 changed files with 48 additions and 62 deletions
+6
View File
@@ -26,6 +26,12 @@ export function getEraMultiCache <T> (CACHE_KEY: string, eras: EraIndex[], withA
return cached;
}
export function setEraCache <T extends { era: EraIndex }> (cacheKey: string, withActive: boolean, value: T): T {
!withActive && deriveCache.set(cacheKey, value);
return value;
}
export function setEraMultiCache <T extends { era: EraIndex }> (CACHE_KEY: string, withActive: boolean, values: T[]): T[] {
!withActive && values.forEach((v) => deriveCache.set(`${CACHE_KEY}-${v.era.toString()}`, v));
@@ -10,9 +10,9 @@ import type { DeriveEraExposure, DeriveEraNominatorExposure, DeriveEraValidatorE
import { map, of } from 'rxjs';
import { deriveCache, memo } from '../util';
import { getEraCache } from './cache';
import { combineEras, erasHistoricApply } from './util';
import { memo } from '../util';
import { getEraCache, setEraCache } from './cache';
import { combineEras, erasHistoricApply, singleEra } from './util';
type KeysAndExposures = [StorageKey<[EraIndex, AccountId]>, PalletStakingExposure][];
@@ -45,22 +45,11 @@ export function _eraExposure (instanceId: string, api: ApiInterfaceRx): (era: Er
return cached
? of(cached)
: api.query.staking.erasStakersClipped.entries(era).pipe(
map((stakers): DeriveEraExposure => {
const value = mapStakers(era, stakers);
!withActive && deriveCache.set(cacheKey, value);
return value;
})
map((r) => setEraCache(cacheKey, withActive, mapStakers(era, r)))
);
});
}
export function eraExposure (instanceId: string, api: ApiInterfaceRx): (era: EraIndex) => Observable<DeriveEraExposure> {
return memo(instanceId, (era: EraIndex): Observable<DeriveEraExposure> =>
api.derive.staking._eraExposure(era, true)
);
}
export const eraExposure = singleEra('_eraExposure');
export const _erasExposure = combineEras('_eraExposure');
export const erasExposure = erasHistoricApply('_erasExposure');
+5 -16
View File
@@ -10,9 +10,9 @@ import type { DeriveEraPrefs, DeriveEraValPrefs } from '../types';
import { map, of } from 'rxjs';
import { deriveCache, memo } from '../util';
import { getEraCache } from './cache';
import { combineEras, erasHistoricApply } from './util';
import { memo } from '../util';
import { getEraCache, setEraCache } from './cache';
import { combineEras, erasHistoricApply, singleEra } from './util';
const CACHE_KEY = 'eraPrefs';
@@ -33,22 +33,11 @@ export function _eraPrefs (instanceId: string, api: ApiInterfaceRx): (era: EraIn
return cached
? of(cached)
: api.query.staking.erasValidatorPrefs.entries(era).pipe(
map((prefs): DeriveEraPrefs => {
const value = mapPrefs(era, prefs);
!withActive && deriveCache.set(cacheKey, value);
return value;
})
map((r) => setEraCache(cacheKey, withActive, mapPrefs(era, r)))
);
});
}
export function eraPrefs (instanceId: string, api: ApiInterfaceRx): (era: EraIndex) => Observable<DeriveEraPrefs> {
return memo(instanceId, (era: EraIndex): Observable<DeriveEraPrefs> =>
api.derive.staking._eraPrefs(era, true)
);
}
export const eraPrefs = singleEra('_eraPrefs');
export const _erasPrefs = combineEras('_eraPrefs');
export const erasPrefs = erasHistoricApply('_erasPrefs');
+5 -16
View File
@@ -10,9 +10,9 @@ import type { DeriveEraSlashes, DeriveEraValSlash } from '../types';
import { combineLatest, map, of } from 'rxjs';
import { deriveCache, memo } from '../util';
import { getEraCache } from './cache';
import { combineEras, erasHistoricApply } from './util';
import { memo } from '../util';
import { getEraCache, setEraCache } from './cache';
import { combineEras, erasHistoricApply, singleEra } from './util';
const CACHE_KEY = 'eraSlashes';
@@ -41,22 +41,11 @@ export function _eraSlashes (instanceId: string, api: ApiInterfaceRx): (era: Era
api.query.staking.nominatorSlashInEra.entries(era),
api.query.staking.validatorSlashInEra.entries(era)
]).pipe(
map(([noms, vals]): DeriveEraSlashes => {
const value = mapSlashes(era, noms, vals);
!withActive && deriveCache.set(cacheKey, value);
return value;
})
map(([n, v]) => setEraCache(cacheKey, withActive, mapSlashes(era, n, v)))
);
});
}
export function eraSlashes (instanceId: string, api: ApiInterfaceRx): (era: EraIndex) => Observable<DeriveEraSlashes> {
return memo(instanceId, (era: EraIndex): Observable<DeriveEraSlashes> =>
api.derive.staking._eraSlashes(era, true)
);
}
export const eraSlashes = singleEra('_eraSlashes');
export const _erasSlashes = combineEras('_eraSlashes');
export const erasSlashes = erasHistoricApply('_erasSlashes');
+9
View File
@@ -38,6 +38,15 @@ export function erasHistoricApplyAccount <F extends '_ownExposures' | '_ownSlash
) as any;
}
export function singleEra <F extends '_eraExposure' | '_eraPrefs' | '_eraSlashes'> (fn: F): (instanceId: string, api: ApiInterfaceRx) => (era: EraIndex) => 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, (era: EraIndex) =>
api.derive.staking[fn](era, true)
) 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