Move api-derive to correct location (#4343)
* Move api-derive to correct location * Adjust (linting ok) * No composite * ApiDerive split * Adjust * Still WIP, getting closer * It lints * derive build issues sorted * It builds :) * Add system_health to mock * api-augment usage * Cleanups * Adjust paths * linting * Some <'s * Revert <>'s * linting * ordering * Augmentation tweaks * yarn.lock * export
This commit is contained in:
@@ -0,0 +1,46 @@
|
||||
// Copyright 2017-2021 @polkadot/api-derive authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { EraIndex } from '@polkadot/types/interfaces';
|
||||
|
||||
import { deriveCache } from '../util';
|
||||
|
||||
export function getEraCache <T> (CACHE_KEY: string, era: EraIndex, withActive?: boolean): [string, T | undefined] {
|
||||
const cacheKey = `${CACHE_KEY}-${era.toString()}`;
|
||||
|
||||
return [
|
||||
cacheKey,
|
||||
withActive
|
||||
? undefined
|
||||
: deriveCache.get<T>(cacheKey)
|
||||
];
|
||||
}
|
||||
|
||||
export function getEraMultiCache <T> (CACHE_KEY: string, eras: EraIndex[], withActive?: boolean): T[] {
|
||||
const cached: T[] = withActive
|
||||
? []
|
||||
: eras
|
||||
.map((e) => deriveCache.get<T>(`${CACHE_KEY}-${e.toString()}`))
|
||||
.filter((v): v is T => !!v);
|
||||
|
||||
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));
|
||||
|
||||
return values;
|
||||
}
|
||||
|
||||
export function filterCachedEras <T extends { era: EraIndex }> (eras: EraIndex[], cached: T[], query: T[]): T[] {
|
||||
return eras.map((e) =>
|
||||
cached.find(({ era }) => e.eq(era)) ||
|
||||
query.find(({ era }) => e.eq(era)) as T
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user