1 | "use strict";
|
2 | var __extends = (this && this.__extends) || (function () {
|
3 | var extendStatics = function (d, b) {
|
4 | extendStatics = Object.setPrototypeOf ||
|
5 | ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
|
6 | function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
|
7 | return extendStatics(d, b);
|
8 | };
|
9 | return function (d, b) {
|
10 | if (typeof b !== "function" && b !== null)
|
11 | throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
|
12 | extendStatics(d, b);
|
13 | function __() { this.constructor = d; }
|
14 | d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
15 | };
|
16 | })();
|
17 | Object.defineProperty(exports, "__esModule", { value: true });
|
18 | exports.JsonRpcBatchProvider = void 0;
|
19 | var properties_1 = require("@ethersproject/properties");
|
20 | var web_1 = require("@ethersproject/web");
|
21 | var json_rpc_provider_1 = require("./json-rpc-provider");
|
22 |
|
23 | var JsonRpcBatchProvider = (function (_super) {
|
24 | __extends(JsonRpcBatchProvider, _super);
|
25 | function JsonRpcBatchProvider() {
|
26 | return _super !== null && _super.apply(this, arguments) || this;
|
27 | }
|
28 | JsonRpcBatchProvider.prototype.send = function (method, params) {
|
29 | var _this = this;
|
30 | var request = {
|
31 | method: method,
|
32 | params: params,
|
33 | id: (this._nextId++),
|
34 | jsonrpc: "2.0"
|
35 | };
|
36 | if (this._pendingBatch == null) {
|
37 | this._pendingBatch = [];
|
38 | }
|
39 | var inflightRequest = { request: request, resolve: null, reject: null };
|
40 | var promise = new Promise(function (resolve, reject) {
|
41 | inflightRequest.resolve = resolve;
|
42 | inflightRequest.reject = reject;
|
43 | });
|
44 | this._pendingBatch.push(inflightRequest);
|
45 | if (!this._pendingBatchAggregator) {
|
46 |
|
47 | this._pendingBatchAggregator = setTimeout(function () {
|
48 |
|
49 |
|
50 | var batch = _this._pendingBatch;
|
51 | _this._pendingBatch = null;
|
52 | _this._pendingBatchAggregator = null;
|
53 |
|
54 | var request = batch.map(function (inflight) { return inflight.request; });
|
55 | _this.emit("debug", {
|
56 | action: "requestBatch",
|
57 | request: (0, properties_1.deepCopy)(request),
|
58 | provider: _this
|
59 | });
|
60 | return (0, web_1.fetchJson)(_this.connection, JSON.stringify(request)).then(function (result) {
|
61 | _this.emit("debug", {
|
62 | action: "response",
|
63 | request: request,
|
64 | response: result,
|
65 | provider: _this
|
66 | });
|
67 |
|
68 |
|
69 | batch.forEach(function (inflightRequest, index) {
|
70 | var payload = result[index];
|
71 | if (payload.error) {
|
72 | var error = new Error(payload.error.message);
|
73 | error.code = payload.error.code;
|
74 | error.data = payload.error.data;
|
75 | inflightRequest.reject(error);
|
76 | }
|
77 | else {
|
78 | inflightRequest.resolve(payload.result);
|
79 | }
|
80 | });
|
81 | }, function (error) {
|
82 | _this.emit("debug", {
|
83 | action: "response",
|
84 | error: error,
|
85 | request: request,
|
86 | provider: _this
|
87 | });
|
88 | batch.forEach(function (inflightRequest) {
|
89 | inflightRequest.reject(error);
|
90 | });
|
91 | });
|
92 | }, 10);
|
93 | }
|
94 | return promise;
|
95 | };
|
96 | return JsonRpcBatchProvider;
|
97 | }(json_rpc_provider_1.JsonRpcProvider));
|
98 | exports.JsonRpcBatchProvider = JsonRpcBatchProvider;
|
99 |
|
\ | No newline at end of file |