Remove check value for useCache{Map, Value} (#8906)

This commit is contained in:
Jaco 2023-01-31 20:23:19 +02:00 committed by GitHub
parent ddfe566621
commit cbaec8fdde
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 24 additions and 47 deletions

View File

@ -60,7 +60,7 @@ function useExposureImpl ({ stashId }: Validator, { activeEra }: SessionInfo): U
[clipExposure, fullExposure]
);
return useCacheMap('useExposure', stashId, activeEra, result);
return useCacheMap('useExposure', stashId, result);
}
export default createNamedHook('useExposure', useExposureImpl);

View File

@ -50,7 +50,7 @@ function useHeartbeatImpl ({ stashId, stashIndex }: Validator, { currentSession
[authoredBlocks, receivedHeartbeats]
);
return useCacheMap('useHeartbeat', stashId, currentSession, result) || EMPTY;
return useCacheMap('useHeartbeat', stashId, result) || EMPTY;
}
export default createNamedHook('useHeartbeat', useHeartbeatImpl);

View File

@ -33,7 +33,7 @@ function usePointsImpl ({ activeEra }: SessionInfo): UsePoints | undefined {
const points = useCall(queryParams && api.query.staking.erasRewardPoints, queryParams, OPT_POINTS);
return useCacheValue('usePoints', activeEra, points);
return useCacheValue('usePoints', points);
}
export default createNamedHook('usePoints', usePointsImpl);

View File

@ -4,7 +4,6 @@
import type { BN } from '@polkadot/util';
export interface CacheValue<T> {
check: BN;
value?: T;
}

View File

@ -1,7 +1,6 @@
// Copyright 2017-2023 @polkadot/app-staking authors & contributors
// SPDX-License-Identifier: Apache-2.0
import type { BN } from '@polkadot/util';
import type { CacheValue } from './types';
import { useEffect } from 'react';
@ -15,20 +14,13 @@ const cache: Record<CacheSection, Record<string, CacheValue<any>>> = {
useHeartbeat: {}
};
export function useCacheMap <T> (section: CacheSection, id: string, check?: BN | null, value?: T): T | undefined {
// ensure that the specific id has an entry
useEffect((): void => {
if (check && (!cache[section][id] || !cache[section][id].check.eq(check))) {
cache[section][id] = { check };
}
}, [check, id, section]);
export function useCacheMap <T> (section: CacheSection, id: string, value?: T): T | undefined {
// update the cached result on value changes
useEffect((): void => {
if (value && check && cache[section][id] && cache[section][id].check.eq(check)) {
cache[section][id] = { check, value };
if (value) {
cache[section][id] = { value };
}
}, [check, id, section, value]);
}, [id, section, value]);
return getResultValue(cache[section][id] as CacheValue<T>, check, value);
return getResultValue(cache[section][id] as CacheValue<T>, value);
}

View File

@ -5,41 +5,27 @@ import type { CacheValue } from './types';
import { useEffect } from 'react';
import { BN } from '@polkadot/util';
type CacheSection = `use${'ElectedValidators' | 'Points' | 'ValidatorsActive' | 'ValidatorsAll' | 'ValidatorsWaiting'}`;
const cache: Record<CacheSection, CacheValue<any>> = {
useElectedValidators: { check: new BN(-1) },
usePoints: { check: new BN(-1) },
useValidatorsActive: { check: new BN(-1) },
useValidatorsAll: { check: new BN(-1) },
useValidatorsWaiting: { check: new BN(-1) }
useElectedValidators: {},
usePoints: {},
useValidatorsActive: {},
useValidatorsAll: {},
useValidatorsWaiting: {}
};
export function getResultValue <T> (cached?: CacheValue<T>, check?: BN | null, value?: T): T | undefined {
return value || (
check &&
cached &&
cached.check.eq(check) &&
cached.value
) || undefined;
export function getResultValue <T> (cached?: CacheValue<T>, value?: T): T | undefined {
return value || cached?.value;
}
export function useCacheValue <T> (section: CacheSection, check?: BN | null, value?: T): T | undefined {
// ensure that the specific id has an entry
useEffect((): void => {
if (check && !cache[section].check.eq(check)) {
cache[section] = { check };
}
}, [check, section]);
export function useCacheValue <T> (section: CacheSection, value?: T): T | undefined {
// update the cached result on value changes
useEffect((): void => {
if (value && check && cache[section].check.eq(check)) {
cache[section] = { check, value };
if (value) {
cache[section] = { value };
}
}, [check, section, value]);
}, [section, value]);
return getResultValue(cache[section] as CacheValue<T>, check, value);
return getResultValue(cache[section] as CacheValue<T>, value);
}

View File

@ -28,7 +28,7 @@ function useElectedValidatorsImpl ({ currentEra }: SessionInfo): string[] | unde
const elected = useMapKeys(electedParams && api.query.staking.erasStakers, electedParams, OPT_ELECTED);
return useCacheValue('useElectedValidators', currentEra, elected);
return useCacheValue('useElectedValidators', elected);
}
export default createNamedHook('useElectedValidators', useElectedValidatorsImpl);

View File

@ -30,7 +30,7 @@ function useValidatorsActiveImpl (favorites: string[], sessionInfo: SessionInfo)
const sessionValidators = useCall(api.query.session.validators, undefined, OPT_VALIDATORS);
const tagged = useTaggedValidators(favorites, sessionInfo, sessionValidators);
return useCacheValue('useValidatorsActive', sessionInfo.activeEra, tagged);
return useCacheValue('useValidatorsActive', tagged);
}
export default createNamedHook('useValidatorsActive', useValidatorsActiveImpl);

View File

@ -65,7 +65,7 @@ function useValidatorsAllImpl (favorites: string[], sessionInfo: SessionInfo): V
const tagged = useTaggedValidators(favorites, sessionInfo, validatorsIndexed);
return useCacheValue('useValidatorsAll', sessionInfo.currentEra, tagged);
return useCacheValue('useValidatorsAll', tagged);
}
export default createNamedHook('useValidatorsAll', useValidatorsAllImpl);

View File

@ -26,7 +26,7 @@ function useValidatorsWaitingImpl (favorites: string[], sessionInfo: SessionInfo
[activeValidators, allValidators]
);
return useCacheValue('useValidatorsWaiting', sessionInfo.activeEra, tagged);
return useCacheValue('useValidatorsWaiting', tagged);
}
export default createNamedHook('useValidatorsWaiting', useValidatorsWaitingImpl);