Rework derives to ensure cached registries (#4021)
This commit is contained in:
@@ -5,7 +5,7 @@ import type { Observable } from 'rxjs';
|
||||
import type { ApiInterfaceRx } from '@polkadot/api/types';
|
||||
import type { SignedBlockExtended } from '../type/types';
|
||||
|
||||
import { catchError, combineLatest, map, of } from 'rxjs';
|
||||
import { catchError, combineLatest, map, of, switchMap } from 'rxjs';
|
||||
|
||||
import { createSignedBlockExtended } from '../type';
|
||||
import { memo } from '../util';
|
||||
@@ -25,15 +25,19 @@ import { memo } from '../util';
|
||||
*/
|
||||
export function getBlock (instanceId: string, api: ApiInterfaceRx): (hash: Uint8Array | string) => Observable<SignedBlockExtended | undefined> {
|
||||
return memo(instanceId, (hash: Uint8Array | string): Observable<SignedBlockExtended | undefined> =>
|
||||
combineLatest([
|
||||
api.rpc.chain.getBlock(hash),
|
||||
api.query.system.events.at(hash),
|
||||
api.query.session
|
||||
? api.query.session.validators.at(hash)
|
||||
: of([])
|
||||
]).pipe(
|
||||
map(([signedBlock, events, validators]): SignedBlockExtended =>
|
||||
createSignedBlockExtended(api.registry, signedBlock, events, validators)
|
||||
// we get the block first, setting up the registry
|
||||
api.rpc.chain.getBlock(hash).pipe(
|
||||
switchMap((signedBlock) =>
|
||||
combineLatest([
|
||||
api.query.system.events.at(hash),
|
||||
api.query.session
|
||||
? api.query.session.validators.at(hash)
|
||||
: of([])
|
||||
]).pipe(
|
||||
map(([events, validators]) =>
|
||||
createSignedBlockExtended(api.registry, signedBlock, events, validators)
|
||||
)
|
||||
)
|
||||
),
|
||||
catchError((): Observable<undefined> =>
|
||||
// where rpc.chain.getHeader throws, we will land here - it can happen that
|
||||
|
||||
@@ -3,13 +3,20 @@
|
||||
|
||||
import type { Observable } from 'rxjs';
|
||||
import type { ApiInterfaceRx } from '@polkadot/api/types';
|
||||
import type { AccountId } from '@polkadot/types/interfaces';
|
||||
import type { HeaderExtended } from '../type/types';
|
||||
|
||||
import { catchError, combineLatest, map, of } from 'rxjs';
|
||||
import { catchError, map, of, switchMap } from 'rxjs';
|
||||
|
||||
import { createHeaderExtended } from '../type';
|
||||
import { memo } from '../util';
|
||||
|
||||
function _getValidators (api: ApiInterfaceRx, hash: Uint8Array | string): Observable<AccountId[]> {
|
||||
return api.query.session
|
||||
? api.query.session.validators.at(hash)
|
||||
: of([] as AccountId[]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @name getHeader
|
||||
* @param {( Uint8Array | string )} hash - A block hash as U8 array or string.
|
||||
@@ -26,14 +33,14 @@ import { memo } from '../util';
|
||||
*/
|
||||
export function getHeader (instanceId: string, api: ApiInterfaceRx): (hash: Uint8Array | string) => Observable<HeaderExtended | undefined> {
|
||||
return memo(instanceId, (hash: Uint8Array | string): Observable<HeaderExtended | undefined> =>
|
||||
combineLatest([
|
||||
api.rpc.chain.getHeader(hash),
|
||||
api.query.session
|
||||
? api.query.session.validators.at(hash)
|
||||
: of([])
|
||||
]).pipe(
|
||||
map(([header, validators]): HeaderExtended =>
|
||||
createHeaderExtended(header.registry, header, validators)
|
||||
// we get the header first, setting up the registry
|
||||
api.rpc.chain.getHeader(hash).pipe(
|
||||
switchMap((header) =>
|
||||
_getValidators(api, hash).pipe(
|
||||
map((validators) =>
|
||||
createHeaderExtended(header.registry, header, validators)
|
||||
)
|
||||
)
|
||||
),
|
||||
catchError((): Observable<undefined> =>
|
||||
// where rpc.chain.getHeader throws, we will land here - it can happen that
|
||||
|
||||
@@ -3,11 +3,9 @@
|
||||
|
||||
import type { Observable } from 'rxjs';
|
||||
import type { ApiInterfaceRx } from '@polkadot/api/types';
|
||||
import type { Vec } from '@polkadot/types';
|
||||
import type { EventRecord, SignedBlock } from '@polkadot/types/interfaces';
|
||||
import type { HeaderExtended, SignedBlockExtended } from '../type/types';
|
||||
import type { SignedBlockExtended } from '../type/types';
|
||||
|
||||
import { combineLatest, map, of, switchMap } from 'rxjs';
|
||||
import { map, switchMap } from 'rxjs';
|
||||
|
||||
import { createSignedBlockExtended } from '../type';
|
||||
import { memo } from '../util';
|
||||
@@ -19,18 +17,20 @@ import { memo } from '../util';
|
||||
export function subscribeNewBlocks (instanceId: string, api: ApiInterfaceRx): () => Observable<SignedBlockExtended> {
|
||||
return memo(instanceId, (): Observable<SignedBlockExtended> =>
|
||||
api.derive.chain.subscribeNewHeads().pipe(
|
||||
switchMap((header): Observable<[SignedBlock, Vec<EventRecord>, HeaderExtended]> => {
|
||||
switchMap((header) => {
|
||||
const blockHash = header.createdAtHash || header.hash;
|
||||
|
||||
return combineLatest(
|
||||
api.rpc.chain.getBlock(blockHash),
|
||||
api.query.system.events.at(blockHash),
|
||||
of(header)
|
||||
// we get the block first, setting up the registry
|
||||
return api.rpc.chain.getBlock(blockHash).pipe(
|
||||
switchMap((block) =>
|
||||
api.query.system.events.at(blockHash).pipe(
|
||||
map((events) =>
|
||||
createSignedBlockExtended(block.registry, block, events, header.validators)
|
||||
)
|
||||
)
|
||||
)
|
||||
);
|
||||
}),
|
||||
map(([block, events, header]) =>
|
||||
createSignedBlockExtended(block.registry, block, events, header.validators)
|
||||
)
|
||||
})
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user