Cleanups for bestNumber derives (#4318)

* Cleanups for bestNumber derives

* Unused imports

* Re-exports of types for *.d.ts outputs
This commit is contained in:
Jaco
2021-12-11 09:35:52 +02:00
committed by GitHub
parent c32953b2ea
commit 63a55d2f8a
14 changed files with 73 additions and 105 deletions
+2 -8
View File
@@ -1,11 +1,7 @@
// Copyright 2017-2021 @polkadot/api-derive authors & contributors
// SPDX-License-Identifier: Apache-2.0
import type { Observable } from 'rxjs';
import type { ApiInterfaceRx } from '@polkadot/api/types';
import type { BlockNumber } from '@polkadot/types/interfaces';
import { unwrapNumber } from './util';
import { unwrapBlockNumber } from './util';
/**
* @name bestNumber
@@ -19,6 +15,4 @@ import { unwrapNumber } from './util';
* });
* ```
*/
export function bestNumber (instanceId: string, api: ApiInterfaceRx): () => Observable<BlockNumber> {
return unwrapNumber(instanceId, api.derive.chain.subscribeNewHeads);
}
export const bestNumber = unwrapBlockNumber((api) => api.derive.chain.subscribeNewHeads());
@@ -1,11 +1,7 @@
// Copyright 2017-2021 @polkadot/api-derive authors & contributors
// SPDX-License-Identifier: Apache-2.0
import type { Observable } from 'rxjs';
import type { ApiInterfaceRx } from '@polkadot/api/types';
import type { BlockNumber } from '@polkadot/types/interfaces';
import { unwrapNumber } from './util';
import { unwrapBlockNumber } from './util';
/**
* @name bestNumberFinalized
@@ -20,6 +16,4 @@ import { unwrapNumber } from './util';
* });
* ```
*/
export function bestNumberFinalized (instanceId: string, api: ApiInterfaceRx): () => Observable<BlockNumber> {
return unwrapNumber(instanceId, api.rpc.chain.subscribeFinalizedHeads);
}
export const bestNumberFinalized = unwrapBlockNumber((api) => api.rpc.chain.subscribeFinalizedHeads());
+12 -3
View File
@@ -2,13 +2,22 @@
// SPDX-License-Identifier: Apache-2.0
import type { Observable } from 'rxjs';
import type { Option } from '@polkadot/types';
import type { ApiInterfaceRx } from '@polkadot/api/types';
import type { Compact } from '@polkadot/types';
import type { BlockNumber } from '@polkadot/types/interfaces';
import { map } from 'rxjs';
import { memo } from '../util';
export function unwrapNumber <T extends { number: Option<BlockNumber> }> (instanceId: string, fn: () => Observable<T>): () => Observable<BlockNumber> {
return memo(instanceId, () => fn().pipe(map((r) => r.number.unwrap())));
// re-export these - since these needs to be resolvable from api-derive, i.e. without this
// we would emit code with ../<somewhere>/src embedded in the *.d.ts files
export type { ApiInterfaceRx } from '@polkadot/api/types';
export type { BlockNumber } from '@polkadot/types/interfaces';
export function unwrapBlockNumber <T extends { number: Compact<BlockNumber> }> (fn: (api: ApiInterfaceRx) => Observable<T>): (instanceId: string, api: ApiInterfaceRx) => () => Observable<BlockNumber> {
return (instanceId: string, api: ApiInterfaceRx) =>
memo(instanceId, () => fn(api).pipe(
map((r) => r.number.unwrap())
));
}