Additional derive cleanups (#4308)
* Additional derive cleanups * unwrapNumber * Adjust
This commit is contained in:
@@ -5,9 +5,7 @@ import type { Observable } from 'rxjs';
|
||||
import type { ApiInterfaceRx } from '@polkadot/api/types';
|
||||
import type { BlockNumber } from '@polkadot/types/interfaces';
|
||||
|
||||
import { map } from 'rxjs';
|
||||
|
||||
import { memo } from '../util';
|
||||
import { unwrapNumber } from './util';
|
||||
|
||||
/**
|
||||
* @name bestNumber
|
||||
@@ -22,8 +20,5 @@ import { memo } from '../util';
|
||||
* ```
|
||||
*/
|
||||
export function bestNumber (instanceId: string, api: ApiInterfaceRx): () => Observable<BlockNumber> {
|
||||
return memo(instanceId, (): Observable<BlockNumber> =>
|
||||
api.derive.chain.subscribeNewHeads().pipe(
|
||||
map((header) => header.number.unwrap())
|
||||
));
|
||||
return unwrapNumber(instanceId, api.derive.chain.subscribeNewHeads);
|
||||
}
|
||||
|
||||
@@ -5,9 +5,7 @@ import type { Observable } from 'rxjs';
|
||||
import type { ApiInterfaceRx } from '@polkadot/api/types';
|
||||
import type { BlockNumber } from '@polkadot/types/interfaces';
|
||||
|
||||
import { map } from 'rxjs';
|
||||
|
||||
import { memo } from '../util';
|
||||
import { unwrapNumber } from './util';
|
||||
|
||||
/**
|
||||
* @name bestNumberFinalized
|
||||
@@ -23,8 +21,5 @@ import { memo } from '../util';
|
||||
* ```
|
||||
*/
|
||||
export function bestNumberFinalized (instanceId: string, api: ApiInterfaceRx): () => Observable<BlockNumber> {
|
||||
return memo(instanceId, (): Observable<BlockNumber> =>
|
||||
api.rpc.chain.subscribeFinalizedHeads().pipe(
|
||||
map((header) => header.number.unwrap())
|
||||
));
|
||||
return unwrapNumber(instanceId, api.rpc.chain.subscribeFinalizedHeads);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,14 @@
|
||||
// Copyright 2017-2021 @polkadot/api-derive authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { Observable } from 'rxjs';
|
||||
import type { Option } 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())));
|
||||
}
|
||||
Reference in New Issue
Block a user