Skip to content

Commit 59c2327

Browse files
authored
Add firstMemo (dedupes) (#4321)
* Add firstMemo (dedupes) * Adjust * flatten * Readability * Readability
1 parent 4b0c6aa commit 59c2327

File tree

14 files changed

+90
-70
lines changed

14 files changed

+90
-70
lines changed

packages/api-derive/src/accounts/identity.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import { combineLatest, map, of, switchMap } from 'rxjs';
1313

1414
import { isHex, u8aToString } from '@polkadot/util';
1515

16-
import { firstObservable, memo } from '../util';
16+
import { firstMemo, memo } from '../util';
1717

1818
type IdentityInfoAdditional = PalletIdentityIdentityInfo['additional'][0];
1919

@@ -104,11 +104,10 @@ export function identity (instanceId: string, api: ApiInterfaceRx): (accountId?:
104104
);
105105
}
106106

107-
export function hasIdentity (instanceId: string, api: ApiInterfaceRx): (accountId: AccountId | Uint8Array | string) => Observable<DeriveHasIdentity> {
108-
return memo(instanceId, (accountId: AccountId | Uint8Array | string): Observable<DeriveHasIdentity> =>
109-
firstObservable(api.derive.accounts.hasIdentityMulti([accountId]))
110-
);
111-
}
107+
export const hasIdentity = firstMemo(
108+
(api: ApiInterfaceRx, accountId: AccountId | Uint8Array | string) =>
109+
api.derive.accounts.hasIdentityMulti([accountId])
110+
);
112111

113112
export function hasIdentityMulti (instanceId: string, api: ApiInterfaceRx): (accountIds: (AccountId | Uint8Array | string)[]) => Observable<DeriveHasIdentity[]> {
114113
return memo(instanceId, (accountIds: (AccountId | Uint8Array | string)[]): Observable<DeriveHasIdentity[]> =>

packages/api-derive/src/chain/bestNumber.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// Copyright 2017-2021 @polkadot/api-derive authors & contributors
22
// SPDX-License-Identifier: Apache-2.0
33

4+
import type { ApiInterfaceRx } from '@polkadot/api/types';
5+
46
import { unwrapBlockNumber } from './util';
57

68
/**
@@ -15,4 +17,7 @@ import { unwrapBlockNumber } from './util';
1517
* });
1618
* ```
1719
*/
18-
export const bestNumber = unwrapBlockNumber((api) => api.derive.chain.subscribeNewHeads());
20+
export const bestNumber = unwrapBlockNumber(
21+
(api: ApiInterfaceRx) =>
22+
api.derive.chain.subscribeNewHeads()
23+
);

packages/api-derive/src/chain/bestNumberFinalized.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// Copyright 2017-2021 @polkadot/api-derive authors & contributors
22
// SPDX-License-Identifier: Apache-2.0
33

4+
import type { ApiInterfaceRx } from '@polkadot/api/types';
5+
46
import { unwrapBlockNumber } from './util';
57

68
/**
@@ -16,4 +18,7 @@ import { unwrapBlockNumber } from './util';
1618
* });
1719
* ```
1820
*/
19-
export const bestNumberFinalized = unwrapBlockNumber((api) => api.rpc.chain.subscribeFinalizedHeads());
21+
export const bestNumberFinalized = unwrapBlockNumber(
22+
(api: ApiInterfaceRx) =>
23+
api.rpc.chain.subscribeFinalizedHeads()
24+
);

packages/api-derive/src/staking/account.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import { combineLatest, map, switchMap } from 'rxjs';
1111

1212
import { BN, BN_ZERO } from '@polkadot/util';
1313

14-
import { firstObservable, memo } from '../util';
14+
import { firstMemo, memo } from '../util';
1515

1616
const QUERY_OPTS = {
1717
withDestination: true,
@@ -73,7 +73,9 @@ export function accounts (instanceId: string, api: ApiInterfaceRx): (accountIds:
7373
api.derive.staking.keysMulti(accountIds),
7474
api.derive.staking.queryMulti(accountIds, QUERY_OPTS)
7575
]).pipe(
76-
map(([keys, queries]) => queries.map((query, index) => parseResult(api, sessionInfo, keys[index], query)))
76+
map(([keys, queries]) =>
77+
queries.map((q, index) => parseResult(api, sessionInfo, keys[index], q))
78+
)
7779
)
7880
)
7981
)
@@ -83,8 +85,7 @@ export function accounts (instanceId: string, api: ApiInterfaceRx): (accountIds:
8385
/**
8486
* @description From a stash, retrieve the controllerId and fill in all the relevant staking details
8587
*/
86-
export function account (instanceId: string, api: ApiInterfaceRx): (accountId: Uint8Array | string) => Observable<DeriveStakingAccount> {
87-
return memo(instanceId, (accountId: Uint8Array | string): Observable<DeriveStakingAccount> =>
88-
firstObservable(api.derive.staking.accounts([accountId]))
89-
);
90-
}
88+
export const account = firstMemo(
89+
(api: ApiInterfaceRx, accountId: Uint8Array | string) =>
90+
api.derive.staking.accounts([accountId])
91+
);

packages/api-derive/src/staking/erasExposure.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ function mapStakers (era: EraIndex, stakers: KeysAndExposures): DeriveEraExposur
3838
return { era, nominators, validators };
3939
}
4040

41-
export function _eraExposure (instanceId: string, api: ApiInterfaceRx): (era: EraIndex, withActive: boolean) => Observable<DeriveEraExposure> {
42-
return memo(instanceId, (era: EraIndex, withActive: boolean): Observable<DeriveEraExposure> => {
41+
export function _eraExposure (instanceId: string, api: ApiInterfaceRx): (era: EraIndex, withActive?: boolean) => Observable<DeriveEraExposure> {
42+
return memo(instanceId, (era: EraIndex, withActive = false): Observable<DeriveEraExposure> => {
4343
const [cacheKey, cached] = getEraCache<DeriveEraExposure>(CACHE_KEY, era, withActive);
4444

4545
return cached

packages/api-derive/src/staking/erasHistoric.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ import { BN_ONE, BN_ZERO } from '@polkadot/util';
1313

1414
import { memo } from '../util';
1515

16-
export function erasHistoric (instanceId: string, api: ApiInterfaceRx): (withActive: boolean) => Observable<EraIndex[]> {
17-
return memo(instanceId, (withActive: boolean): Observable<EraIndex[]> =>
16+
export function erasHistoric (instanceId: string, api: ApiInterfaceRx): (withActive?: boolean) => Observable<EraIndex[]> {
17+
return memo(instanceId, (withActive?: boolean): Observable<EraIndex[]> =>
1818
api.queryMulti<[Option<ActiveEraInfo>, u32]>([
1919
api.query.staking.activeEra,
2020
api.query.staking.historyDepth

packages/api-derive/src/staking/keys.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import type { DeriveStakingKeys } from './types';
1111

1212
import { combineLatest, map, of, switchMap } from 'rxjs';
1313

14-
import { firstObservable, memo } from '../util';
14+
import { firstMemo, memo } from '../util';
1515

1616
function extractsIds (stashId: Uint8Array | string, queuedKeys: [AccountId, NodeRuntimeSessionKeys | AccountId[]][], nextKeys: Option<NodeRuntimeSessionKeys>): DeriveStakingKeys {
1717
const sessionIds = (queuedKeys.find(([currentId]) => currentId.eq(stashId)) || [undefined, [] as AccountId[]])[1];
@@ -27,11 +27,10 @@ function extractsIds (stashId: Uint8Array | string, queuedKeys: [AccountId, Node
2727
};
2828
}
2929

30-
export function keys (instanceId: string, api: ApiInterfaceRx): (stashId: Uint8Array | string) => Observable<DeriveStakingKeys> {
31-
return memo(instanceId, (stashId: Uint8Array | string): Observable<DeriveStakingKeys> =>
32-
firstObservable(api.derive.staking.keysMulti([stashId]))
33-
);
34-
}
30+
export const keys = firstMemo(
31+
(api: ApiInterfaceRx, stashId: Uint8Array | string) =>
32+
api.derive.staking.keysMulti([stashId])
33+
);
3534

3635
export function keysMulti (instanceId: string, api: ApiInterfaceRx): (stashIds: (Uint8Array | string)[]) => Observable<DeriveStakingKeys[]> {
3736
return memo(instanceId, (stashIds: (Uint8Array | string)[]): Observable<DeriveStakingKeys[]> =>

packages/api-derive/src/staking/ownExposure.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import type { DeriveOwnExposure } from '../types';
99

1010
import { map, of } from 'rxjs';
1111

12-
import { firstObservable, memo } from '../util';
12+
import { firstMemo, memo } from '../util';
1313
import { erasHistoricApplyAccount } from './util';
1414

1515
export function _ownExposures (instanceId: string, api: ApiInterfaceRx): (accountId: Uint8Array | string, eras: EraIndex[], withActive: boolean) => Observable<DeriveOwnExposure[]> {
@@ -28,10 +28,9 @@ export function _ownExposures (instanceId: string, api: ApiInterfaceRx): (accoun
2828
);
2929
}
3030

31-
export function ownExposure (instanceId: string, api: ApiInterfaceRx): (accountId: Uint8Array | string, era: EraIndex) => Observable<DeriveOwnExposure> {
32-
return memo(instanceId, (accountId: Uint8Array | string, era: EraIndex): Observable<DeriveOwnExposure> =>
33-
firstObservable(api.derive.staking._ownExposures(accountId, [era], true))
34-
);
35-
}
31+
export const ownExposure = firstMemo(
32+
(api: ApiInterfaceRx, accountId: Uint8Array | string, era: EraIndex) =>
33+
api.derive.staking._ownExposures(accountId, [era], true)
34+
);
3635

3736
export const ownExposures = erasHistoricApplyAccount('_ownExposures');

packages/api-derive/src/staking/ownSlashes.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import type { DeriveStakerSlashes } from '../types';
1010

1111
import { map, of } from 'rxjs';
1212

13-
import { firstObservable, memo } from '../util';
13+
import { firstMemo, memo } from '../util';
1414
import { erasHistoricApplyAccount } from './util';
1515

1616
export function _ownSlashes (instanceId: string, api: ApiInterfaceRx): (accountId: Uint8Array | string, eras: EraIndex[], withActive: boolean) => Observable<DeriveStakerSlashes[]> {
@@ -34,10 +34,9 @@ export function _ownSlashes (instanceId: string, api: ApiInterfaceRx): (accountI
3434
);
3535
}
3636

37-
export function ownSlash (instanceId: string, api: ApiInterfaceRx): (accountId: Uint8Array | string, era: EraIndex) => Observable<DeriveStakerSlashes> {
38-
return memo(instanceId, (accountId: Uint8Array | string, era: EraIndex): Observable<DeriveStakerSlashes> =>
39-
firstObservable(api.derive.staking._ownSlashes(accountId, [era], true))
40-
);
41-
}
37+
export const ownSlash = firstMemo(
38+
(api: ApiInterfaceRx, accountId: Uint8Array | string, era: EraIndex) =>
39+
api.derive.staking._ownSlashes(accountId, [era], true)
40+
);
4241

4342
export const ownSlashes = erasHistoricApplyAccount('_ownSlashes');

packages/api-derive/src/staking/query.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import type { DeriveStakingQuery, StakingQueryFlags } from '../types';
1010

1111
import { combineLatest, map, of, switchMap } from 'rxjs';
1212

13-
import { firstObservable, memo } from '../util';
13+
import { firstMemo, memo } from '../util';
1414

1515
function parseDetails (stashId: AccountId, controllerIdOpt: Option<AccountId> | null, nominatorsOpt: Option<PalletStakingNominations>, rewardDestination: PalletStakingRewardDestination, validatorPrefs: PalletStakingValidatorPrefs, exposure: PalletStakingExposure, stakingLedgerOpt: Option<PalletStakingStakingLedger>): DeriveStakingQuery {
1616
return {
@@ -93,11 +93,10 @@ function getBatch (api: ApiInterfaceRx, activeEra: EraIndex, stashIds: AccountId
9393
/**
9494
* @description From a stash, retrieve the controllerId and all relevant details
9595
*/
96-
export function query (instanceId: string, api: ApiInterfaceRx): (accountId: Uint8Array | string, flags: StakingQueryFlags) => Observable<DeriveStakingQuery> {
97-
return memo(instanceId, (accountId: Uint8Array | string, flags: StakingQueryFlags): Observable<DeriveStakingQuery> =>
98-
firstObservable(api.derive.staking.queryMulti([accountId], flags))
99-
);
100-
}
96+
export const query = firstMemo(
97+
(api: ApiInterfaceRx, accountId: Uint8Array | string, flags: StakingQueryFlags) =>
98+
api.derive.staking.queryMulti([accountId], flags)
99+
);
101100

102101
export function queryMulti (instanceId: string, api: ApiInterfaceRx): (accountIds: (Uint8Array | string)[], flags: StakingQueryFlags) => Observable<DeriveStakingQuery[]> {
103102
return memo(instanceId, (accountIds: (Uint8Array | string)[], flags: StakingQueryFlags): Observable<DeriveStakingQuery[]> =>

0 commit comments

Comments
 (0)