UNPKG

4.46 kBJavaScriptView Raw
1"use strict";
2var __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})();
17Object.defineProperty(exports, "__esModule", { value: true });
18exports.JsonRpcBatchProvider = void 0;
19var properties_1 = require("@ethersproject/properties");
20var web_1 = require("@ethersproject/web");
21var json_rpc_provider_1 = require("./json-rpc-provider");
22// Experimental
23var JsonRpcBatchProvider = /** @class */ (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 // Schedule batch for next event loop + short duration
47 this._pendingBatchAggregator = setTimeout(function () {
48 // Get teh current batch and clear it, so new requests
49 // go into the next batch
50 var batch = _this._pendingBatch;
51 _this._pendingBatch = null;
52 _this._pendingBatchAggregator = null;
53 // Get the request as an array of requests
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 // For each result, feed it to the correct Promise, depending
68 // on whether it was a success or error
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));
98exports.JsonRpcBatchProvider = JsonRpcBatchProvider;
99//# sourceMappingURL=json-rpc-batch-provider.js.map
\No newline at end of file