@acala-network/chopsticks 0.3.7 → 0.3.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.HeadState = exports.randomId = void 0;
4
+ const rxjs_1 = require("rxjs");
4
5
  const util_1 = require("@polkadot/util");
5
6
  const logger_1 = require("../logger");
6
7
  const randomId = () => Math.random().toString(36).substring(2);
@@ -46,23 +47,27 @@ class HeadState {
46
47
  async setHead(head) {
47
48
  this.#head = head;
48
49
  for (const cb of Object.values(this.#headListeners)) {
49
- try {
50
- cb(head);
51
- }
52
- catch (error) {
53
- logger.error(error, 'callback');
54
- }
55
- }
56
- const diff = await this.#head.storageDiff();
57
- for (const [keys, cb] of Object.values(this.#storageListeners)) {
58
- const changed = keys.filter((key) => diff[key]).map((key) => [key, diff[key]]);
59
- if (changed.length > 0) {
50
+ rxjs_1.asapScheduler.schedule(() => {
60
51
  try {
61
- cb(head, changed);
52
+ cb(head);
62
53
  }
63
54
  catch (error) {
64
55
  logger.error(error, 'callback');
65
56
  }
57
+ });
58
+ }
59
+ const diff = await this.#head.storageDiff();
60
+ for (const [keys, cb] of Object.values(this.#storageListeners)) {
61
+ const changed = keys.filter((key) => diff[key]).map((key) => [key, diff[key]]);
62
+ if (changed.length > 0) {
63
+ rxjs_1.asapScheduler.schedule(() => {
64
+ try {
65
+ cb(head, changed);
66
+ }
67
+ catch (error) {
68
+ logger.error(error, 'callback');
69
+ }
70
+ });
66
71
  }
67
72
  }
68
73
  Object.assign(this.#oldValues, diff);
@@ -35,7 +35,7 @@ export declare class Blockchain {
35
35
  setHead(block: Block): Promise<void>;
36
36
  submitExtrinsic(extrinsic: HexString): Promise<HexString>;
37
37
  newBlock(params?: BuildBlockParams): Promise<Block>;
38
- upcomingBlock(count?: number): Promise<Block>;
38
+ upcomingBlock(skipCount?: number): Promise<Block>;
39
39
  dryRunExtrinsic(extrinsic: HexString | {
40
40
  call: HexString;
41
41
  address: string;
@@ -119,8 +119,8 @@ class Blockchain {
119
119
  await this.#txpool.buildBlock(params);
120
120
  return this.#head;
121
121
  }
122
- async upcomingBlock(count = 1) {
123
- return this.#txpool.upcomingBlock(count);
122
+ async upcomingBlock(skipCount = 0) {
123
+ return this.#txpool.upcomingBlock(skipCount);
124
124
  }
125
125
  async dryRunExtrinsic(extrinsic, at) {
126
126
  await this.api.isReady;
@@ -27,5 +27,5 @@ export declare class TxPool {
27
27
  get pendingExtrinsics(): HexString[];
28
28
  submitExtrinsic(extrinsic: HexString): void;
29
29
  buildBlock(params?: BuildBlockParams): Promise<void>;
30
- upcomingBlock(count?: number): Promise<Block>;
30
+ upcomingBlock(skipCount?: number): Promise<Block>;
31
31
  }
@@ -19,11 +19,11 @@ class TxPool {
19
19
  #pool = [];
20
20
  #mode;
21
21
  #inherentProvider;
22
+ #last;
22
23
  #lastBuildBlockPromise = Promise.resolve();
23
- #last = new rxjs_1.ReplaySubject(1);
24
- #upcoming = this.#last.pipe((0, rxjs_1.share)());
25
24
  constructor(chain, inherentProvider, mode = BuildBlockMode.Batch) {
26
25
  this.#chain = chain;
26
+ this.#last = new rxjs_1.BehaviorSubject(chain.head);
27
27
  this.#mode = mode;
28
28
  this.#inherentProvider = inherentProvider;
29
29
  }
@@ -51,15 +51,10 @@ class TxPool {
51
51
  await this.#lastBuildBlockPromise;
52
52
  this.#last.next(this.#chain.head);
53
53
  }
54
- async upcomingBlock(count = 1) {
55
- if (count < 1)
56
- throw new Error('count needs to be greater than 0');
57
- return new Promise((resolve) => {
58
- const sub = this.#upcoming.pipe((0, operators_1.skip)(count - 1)).subscribe((block) => {
59
- sub.unsubscribe();
60
- resolve(block);
61
- });
62
- });
54
+ async upcomingBlock(skipCount = 0) {
55
+ if (skipCount < 0)
56
+ throw new Error('skipCount needs to be greater or equal to 0');
57
+ return (0, rxjs_1.firstValueFrom)(this.#last.pipe((0, operators_1.skip)(1 + skipCount), (0, operators_1.take)(1)));
63
58
  }
64
59
  async #buildBlock(wait, params) {
65
60
  await this.#chain.api.isReady;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@acala-network/chopsticks",
3
- "version": "0.3.7",
3
+ "version": "0.3.9",
4
4
  "main": "./dist/index.js",
5
5
  "types": "./dist/index.d.ts",
6
6
  "author": "Bryan Chen <xlchen1291@gmail.com>",