Skip to content

Commit 895ed80

Browse files
authored
subscribeNewHeads (#1338)
* subscribeNewHeads * Update CHANGELOG * Rebuild interfaces * Sort interfaces, really expose plural * Fix mocks
1 parent 1ef5675 commit 895ed80

File tree

23 files changed

+49
-44
lines changed

23 files changed

+49
-44
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
If you are upgrading form an older version, use the CHANGELOG hand-in-hand with the [migration guide](UPGRADING.md).
44

5+
- **Breaking change** `api.rpc.chain.subscribeNewHeads` is now available as opposed to the old `subscribeNewHead`. This aligns with the Substrate implementation.
56
- **Breaking change** Substrate 2.x & Polkadot master has moved both `BlockNumber` & `Index` to `u32`. The API tracks these closely. If you are running a chain with `u64` values (for either), pass the type override to the API on creation, e.g. `ApiPromise.create({ types: { BlockNumber: 'u64', Index: 'u64' } })` to avoid getting warnings about mismatched types.
67
- **Breaking change** `Api.create(...)` and `new Api(...)` now only takes an options Object, so if you passed the provider directly previously, you need to swap the use to `Api.create({ provider: ... })`
78
- **Breaking change** Runtime types have been extended and moved to definitions instead of classes

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import { drr } from '../util/drr';
2424
*/
2525
export function bestNumber (api: ApiInterfaceRx): () => Observable<BlockNumber> {
2626
return (): Observable<BlockNumber> =>
27-
api.rpc.chain.subscribeNewHead().pipe(
27+
api.rpc.chain.subscribeNewHeads().pipe(
2828
map((header): BlockNumber => header.number.unwrap()),
2929
drr()
3030
);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { ApiInterfaceRx } from '@polkadot/api/types';
88

99
import { HeaderExtended } from '../type';
1010
import { drr } from '../util/drr';
11-
import { HeaderAndValidators } from './subscribeNewHead';
11+
import { HeaderAndValidators } from './subscribeNewHeads';
1212

1313
/**
1414
* @name bestNumberFinalized

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@ export * from './bestNumber';
66
export * from './bestNumberFinalized';
77
export * from './bestNumberLag';
88
export * from './getHeader';
9-
export * from './subscribeNewHead';
9+
export * from './subscribeNewHeads';

packages/api-derive/src/chain/subscribeNewHead.ts renamed to packages/api-derive/src/chain/subscribeNewHeads.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,21 +14,21 @@ import { drr } from '../util/drr';
1414
export type HeaderAndValidators = [Header, AccountId[]];
1515

1616
/**
17-
* @name subscribeNewHead
17+
* @name subscribeNewHeads
1818
* @returns An array containing the block header and the block author
1919
* @description An observable of the current block header and it's author
2020
* @example
2121
* <BR>
2222
*
2323
* ```javascript
24-
* api.derive.chain.subscribeNewHead(({ author, blockNumber }) => {
24+
* api.derive.chain.subscribeNewHeads(({ author, blockNumber }) => {
2525
* console.log(`block #${blockNumber} was authored by ${author}`);
2626
* });
2727
* ```
2828
*/
29-
export function subscribeNewHead (api: ApiInterfaceRx): () => Observable<HeaderExtended> {
29+
export function subscribeNewHeads (api: ApiInterfaceRx): () => Observable<HeaderExtended> {
3030
return (): Observable<HeaderExtended> =>
31-
api.rpc.chain.subscribeNewHead().pipe(
31+
api.rpc.chain.subscribeNewHeads().pipe(
3232
switchMap((header: Header): Observable<HeaderAndValidators> =>
3333
(combineLatest([
3434
of(header),

packages/api/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ import { ApiPromise } from '@polkadot/api';
4545
const api = await ApiPromise.create();
4646

4747
// make a call to retrieve the current network head
48-
api.rpc.chain.subscribeNewHead((header) => {
48+
api.rpc.chain.subscribeNewHeads((header) => {
4949
console.log(`Chain is at #${header.blockNumber}`);
5050
});
5151
```
@@ -59,7 +59,7 @@ import { ApiRx } from '@polkadot/api';
5959
const api = await ApiRx.create().toPromise();
6060

6161
// make a call to retrieve the current network head
62-
api.rpc.chain.subscribeNewHead().subscribe((header) => {
62+
api.rpc.chain.subscribeNewHeads().subscribe((header) => {
6363
console.log(`Chain is at #${header.blockNumber}`);
6464
});
6565
```

packages/api/src/base/Decorate.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ export default abstract class Decorate<ApiType> extends Events {
8282
*
8383
* const api = new Api().isReady();
8484
*
85-
* api.rpc.subscribeNewHead((header) => {
85+
* api.rpc.subscribeNewHeads((header) => {
8686
* console.log(`new block #${header.number.toNumber()}`);
8787
* });
8888
* ```

packages/api/src/base/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ export default abstract class ApiBase<ApiType> extends Init<ApiType> {
4747
*
4848
* const api = new Api().isReady();
4949
*
50-
* api.rpc.subscribeNewHead((header) => {
50+
* api.rpc.subscribeNewHeads((header) => {
5151
* console.log(`new block #${header.number.toNumber()}`);
5252
* });
5353
* ```
@@ -169,7 +169,7 @@ export default abstract class ApiBase<ApiType> extends Init<ApiType> {
169169
* <BR>
170170
*
171171
* ```javascript
172-
* api.rpc.chain.subscribeNewHead((header) => {
172+
* api.rpc.chain.subscribeNewHeads((header) => {
173173
* console.log('new header', header);
174174
* });
175175
* ```

packages/api/src/checkTypes.manual.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ async function consts (api: ApiPromise): Promise<void> {
2424
}
2525

2626
async function derive (api: ApiPromise): Promise<void> {
27-
await api.derive.chain.subscribeNewHead((header: HeaderExtended): void => {
27+
await api.derive.chain.subscribeNewHeads((header: HeaderExtended): void => {
2828
console.log('current author:', header.author);
2929
});
3030

@@ -49,7 +49,7 @@ async function query (api: ApiPromise, keyring: TestKeyringMap): Promise<void> {
4949
}
5050

5151
async function rpc (api: ApiPromise): Promise<void> {
52-
await api.rpc.chain.subscribeNewHead((header: Header): void => {
52+
await api.rpc.chain.subscribeNewHeads((header: Header): void => {
5353
console.log('current header:', header);
5454
});
5555

packages/api/src/promise/Api.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ function promiseTracker (resolve: (value: () => void) => void, reject: (value: E
8282
* const api = await ApiPromise.create();
8383
*
8484
* // make a subscription to the network head
85-
* api.rpc.chain.subscribeNewHead((header) => {
85+
* api.rpc.chain.subscribeNewHeads((header) => {
8686
* console.log(`Chain is at #${header.number}`);
8787
* });
8888
* ```
@@ -176,7 +176,7 @@ export default class ApiPromise extends ApiBase<'promise'> {
176176
* import Api from '@polkadot/api/promise';
177177
*
178178
* new Api().isReady.then((api) => {
179-
* api.rpc.subscribeNewHead((header) => {
179+
* api.rpc.subscribeNewHeads((header) => {
180180
* console.log(`new block #${header.number.toNumber()}`);
181181
* });
182182
* });
@@ -221,7 +221,7 @@ export default class ApiPromise extends ApiBase<'promise'> {
221221
*
222222
* // combines values from balance & nonce as it updates
223223
* api.combineLatest([
224-
* api.rpc.chain.subscribeNewHead,
224+
* api.rpc.chain.subscribeNewHeads,
225225
* [api.query.balances.freeBalance, address],
226226
* (cb) => api.query.system.accountNonce(address, cb)
227227
* ], ([head, balance, nonce]) => {

0 commit comments

Comments
 (0)