* Bump common (w/ rxjs 7) * Adjust * pre bump * Cleanup imports * Add missing switchMap * CHANGELOG * Remove duplicate imports * Bump common * Direct rxjs import
42 lines
1.3 KiB
TypeScript
42 lines
1.3 KiB
TypeScript
// 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 { HeaderExtended } from '../type/types';
|
|
|
|
import { combineLatest, map, of } from 'rxjs';
|
|
|
|
import { createHeaderExtended } from '../type';
|
|
import { memo } from '../util';
|
|
|
|
/**
|
|
* @name subscribeNewHeads
|
|
* @returns A header with the current header (including extracted author)
|
|
* @description An observable of the current block header and it's author
|
|
* @example
|
|
* <BR>
|
|
*
|
|
* ```javascript
|
|
* api.derive.chain.subscribeNewHeads((header) => {
|
|
* console.log(`block #${header.number} was authored by ${header.author}`);
|
|
* });
|
|
* ```
|
|
*/
|
|
export function subscribeNewHeads (instanceId: string, api: ApiInterfaceRx): () => Observable<HeaderExtended> {
|
|
return memo(instanceId, (): Observable<HeaderExtended> =>
|
|
combineLatest([
|
|
api.rpc.chain.subscribeNewHeads(),
|
|
api.query.session
|
|
? api.query.session.validators()
|
|
: of(undefined)
|
|
]).pipe(
|
|
map(([header, validators]): HeaderExtended => {
|
|
header.createdAtHash = header.hash;
|
|
|
|
return createHeaderExtended(header.registry, header, validators);
|
|
})
|
|
)
|
|
);
|
|
}
|