@xelis/sdk 0.10.12 → 0.11.1

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.
@@ -27,65 +27,11 @@ var DaemonMethods = /** @class */ (function () {
27
27
  DaemonMethods.prototype.dataCall = function (method, params) {
28
28
  return this.ws.dataCall(this.prefix + method, params);
29
29
  };
30
- DaemonMethods.prototype.onNewBlock = function (onData) {
31
- return this.ws.listenEvent(this.prefix + types_1.RPCEvent.NewBlock, onData);
30
+ DaemonMethods.prototype.closeListener = function (event, listener) {
31
+ this.ws.closeListener(event, listener);
32
32
  };
33
- DaemonMethods.prototype.onBlockOrdered = function (onData) {
34
- return this.ws.listenEvent(this.prefix + types_1.RPCEvent.BlockOrdered, onData);
35
- };
36
- DaemonMethods.prototype.onBlockOrphaned = function (onData) {
37
- return this.ws.listenEvent(this.prefix + types_1.RPCEvent.BlockOrphaned, onData);
38
- };
39
- DaemonMethods.prototype.onStableHeightChanged = function (onData) {
40
- return this.ws.listenEvent(this.prefix + types_1.RPCEvent.StableHeightChanged, onData);
41
- };
42
- DaemonMethods.prototype.onStableTopoHeightChanged = function (onData) {
43
- return this.ws.listenEvent(this.prefix + types_1.RPCEvent.StableTopoHeightChanged, onData);
44
- };
45
- DaemonMethods.prototype.onTransactionOrphaned = function (onData) {
46
- return this.ws.listenEvent(this.prefix + types_1.RPCEvent.TransactionOrphaned, onData);
47
- };
48
- DaemonMethods.prototype.onTransactionAddedInMempool = function (onData) {
49
- return this.ws.listenEvent(this.prefix + types_1.RPCEvent.TransactionAddedInMempool, onData);
50
- };
51
- DaemonMethods.prototype.onTransactionExecuted = function (onData) {
52
- return this.ws.listenEvent(this.prefix + types_1.RPCEvent.TransactionExecuted, onData);
53
- };
54
- DaemonMethods.prototype.onInvokeContract = function (contract, onData) {
55
- var _a;
56
- return this.ws.listenEvent((_a = {}, _a[this.prefix + types_1.RPCEvent.InvokeContract] = { contract: contract }, _a), onData);
57
- };
58
- DaemonMethods.prototype.onContractTransfer = function (address, onData) {
59
- var _a;
60
- return this.ws.listenEvent((_a = {}, _a[this.prefix + types_1.RPCEvent.ContractTransfer] = { address: address }, _a), onData);
61
- };
62
- DaemonMethods.prototype.onContractEvent = function (contract, id, onData) {
63
- var _a;
64
- return this.ws.listenEvent((_a = {}, _a[this.prefix + types_1.RPCEvent.ContractEvent] = { contract: contract, id: id }, _a), onData);
65
- };
66
- DaemonMethods.prototype.onDeployContract = function (onData) {
67
- return this.ws.listenEvent(this.prefix + types_1.RPCEvent.DeployContract, onData);
68
- };
69
- DaemonMethods.prototype.onNewAsset = function (onData) {
70
- return this.ws.listenEvent(this.prefix + types_1.RPCEvent.NewAsset, onData);
71
- };
72
- DaemonMethods.prototype.onPeerConnected = function (onData) {
73
- return this.ws.listenEvent(this.prefix + types_1.RPCEvent.PeerConnected, onData);
74
- };
75
- DaemonMethods.prototype.onPeerDisconnected = function (onData) {
76
- return this.ws.listenEvent(this.prefix + types_1.RPCEvent.PeerDisconnected, onData);
77
- };
78
- DaemonMethods.prototype.onPeerStateUpdated = function (onData) {
79
- return this.ws.listenEvent(this.prefix + types_1.RPCEvent.PeerStateUpdated, onData);
80
- };
81
- DaemonMethods.prototype.onPeerPeerListUpdated = function (onData) {
82
- return this.ws.listenEvent(this.prefix + types_1.RPCEvent.PeerPeerListUpdated, onData);
83
- };
84
- DaemonMethods.prototype.onPeerPeerDisconnected = function (onData) {
85
- return this.ws.listenEvent(this.prefix + types_1.RPCEvent.PeerPeerDisconnected, onData);
86
- };
87
- DaemonMethods.prototype.onNewBlockTemplate = function (onData) {
88
- return this.ws.listenEvent(this.prefix + types_1.RPCEvent.NewBlockTemplate, onData);
33
+ DaemonMethods.prototype.listen = function (event, listener) {
34
+ this.ws.listen(this.prefix + event, listener);
89
35
  };
90
36
  DaemonMethods.prototype.getVersion = function () {
91
37
  return this.dataCall(types_1.RPCMethod.GetVersion);
@@ -296,8 +242,8 @@ var DaemonMethods = /** @class */ (function () {
296
242
  exports.DaemonMethods = DaemonMethods;
297
243
  var WS = /** @class */ (function (_super) {
298
244
  __extends(WS, _super);
299
- function WS() {
300
- var _this = _super.call(this) || this;
245
+ function WS(endpoint) {
246
+ var _this = _super.call(this, endpoint) || this;
301
247
  _this.methods = new DaemonMethods(_this);
302
248
  return _this;
303
249
  }
@@ -22,42 +22,6 @@ var __importStar = (this && this.__importStar) || function (mod) {
22
22
  __setModuleDefault(result, mod);
23
23
  return result;
24
24
  };
25
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
26
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
27
- return new (P || (P = Promise))(function (resolve, reject) {
28
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
29
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
30
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
31
- step((generator = generator.apply(thisArg, _arguments || [])).next());
32
- });
33
- };
34
- var __generator = (this && this.__generator) || function (thisArg, body) {
35
- var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
36
- return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
37
- function verb(n) { return function (v) { return step([n, v]); }; }
38
- function step(op) {
39
- if (f) throw new TypeError("Generator is already executing.");
40
- while (g && (g = 0, op[0] && (_ = 0)), _) try {
41
- if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
42
- if (y = 0, t) op = [op[0] & 2, t.value];
43
- switch (op[0]) {
44
- case 0: case 1: t = op; break;
45
- case 4: _.label++; return { value: op[1], done: false };
46
- case 5: _.label++; y = op[1]; op = [0]; continue;
47
- case 7: op = _.ops.pop(); _.trys.pop(); continue;
48
- default:
49
- if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
50
- if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
51
- if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
52
- if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
53
- if (t[2]) _.ops.pop();
54
- _.trys.pop(); continue;
55
- }
56
- op = body.call(thisArg, _);
57
- } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
58
- if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
59
- }
60
- };
61
25
  var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
62
26
  if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
63
27
  if (ar || !(i in from)) {
@@ -73,49 +37,34 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
73
37
  exports.__esModule = true;
74
38
  exports.useNodeSocket = exports.useNodeSocketSubscribe = exports.NodeSocketProvider = exports.INITIATING = void 0;
75
39
  var react_1 = __importStar(require("react"));
76
- var await_to_js_1 = require("await-to-js");
77
40
  var websocket_1 = __importDefault(require("../daemon/websocket"));
78
41
  exports.INITIATING = -1;
79
- var daemon = new websocket_1["default"]();
80
42
  var Context = (0, react_1.createContext)({
81
43
  err: undefined,
82
- daemon: daemon,
44
+ daemon: undefined,
83
45
  readyState: exports.INITIATING
84
46
  });
85
47
  var NodeSocketProvider = function (props) {
86
48
  var children = props.children, endpoint = props.endpoint, timeout = props.timeout;
87
49
  var _a = (0, react_1.useState)(exports.INITIATING), readyState = _a[0], setReadyState = _a[1];
88
50
  var _b = (0, react_1.useState)(), err = _b[0], setErr = _b[1];
51
+ var _c = (0, react_1.useState)(), daemon = _c[0], setDaemon = _c[1];
89
52
  (0, react_1.useEffect)(function () {
90
- var connect = function () { return __awaiter(void 0, void 0, void 0, function () {
91
- var _a, err, _;
92
- return __generator(this, function (_b) {
93
- switch (_b.label) {
94
- case 0:
95
- setErr(undefined);
96
- return [4 /*yield*/, (0, await_to_js_1.to)(daemon.connect(endpoint))];
97
- case 1:
98
- _a = _b.sent(), err = _a[0], _ = _a[1];
99
- if (err)
100
- setErr(err);
101
- return [2 /*return*/];
102
- }
103
- });
104
- }); };
105
- connect();
53
+ var daemon = new websocket_1["default"](endpoint);
54
+ setDaemon(daemon);
55
+ return function () { return daemon.socket.close(); };
106
56
  }, [endpoint]);
107
57
  (0, react_1.useEffect)(function () {
108
58
  if (!timeout)
109
59
  return;
110
- daemon.timeout = timeout;
60
+ if (daemon)
61
+ daemon.callTimeout = timeout;
111
62
  }, [timeout]);
112
63
  (0, react_1.useEffect)(function () {
113
- if (!daemon.socket)
64
+ if (!daemon)
114
65
  return;
115
66
  setReadyState(daemon.socket.readyState);
116
67
  var onOpen = function () {
117
- if (!daemon.socket)
118
- return;
119
68
  setReadyState(daemon.socket.readyState);
120
69
  setErr(undefined);
121
70
  };
@@ -141,7 +90,7 @@ var NodeSocketProvider = function (props) {
141
90
  daemon.socket.removeEventListener("close", onClose);
142
91
  daemon.socket.removeEventListener("error", onError);
143
92
  };
144
- }, [daemon.socket]);
93
+ }, [daemon]);
145
94
  return react_1["default"].createElement(Context.Provider, { value: { daemon: daemon, err: err, readyState: readyState } }, children);
146
95
  };
147
96
  exports.NodeSocketProvider = NodeSocketProvider;
@@ -149,24 +98,17 @@ var useNodeSocketSubscribe = function (_a, dependencies) {
149
98
  var event = _a.event, onLoad = _a.onLoad, onData = _a.onData;
150
99
  var nodeSocket = (0, exports.useNodeSocket)();
151
100
  (0, react_1.useEffect)(function () {
101
+ if (!nodeSocket.daemon)
102
+ return;
152
103
  if (nodeSocket.readyState !== WebSocket.OPEN)
153
104
  return;
154
105
  if (typeof onLoad === "function")
155
106
  onLoad();
156
- var closeEvent;
157
- var listen = function () { return __awaiter(void 0, void 0, void 0, function () {
158
- return __generator(this, function (_a) {
159
- switch (_a.label) {
160
- case 0: return [4 /*yield*/, nodeSocket.daemon.listenEvent(event, onData)];
161
- case 1:
162
- closeEvent = _a.sent();
163
- return [2 /*return*/];
164
- }
165
- });
166
- }); };
167
- listen();
107
+ nodeSocket.daemon.listen(event, onData);
168
108
  return function () {
169
- closeEvent && closeEvent();
109
+ if (!nodeSocket.daemon)
110
+ return;
111
+ nodeSocket.daemon.closeListener(event, onData);
170
112
  };
171
113
  }, __spreadArray([nodeSocket], dependencies, true));
172
114
  };
@@ -40,254 +40,119 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
40
40
  };
41
41
  exports.__esModule = true;
42
42
  exports.WSRPC = void 0;
43
- var isomorphic_ws_1 = __importDefault(require("isomorphic-ws"));
44
- var await_to_js_1 = require("await-to-js");
43
+ var await_to_js_1 = __importDefault(require("await-to-js"));
45
44
  var parse_json_1 = require("./parse_json/parse_json");
45
+ var isomorphic_ws_1 = __importDefault(require("isomorphic-ws"));
46
46
  var WSRPC = /** @class */ (function () {
47
- function WSRPC(options) {
48
- this.connectionTries = 0;
49
- this.methodIdIncrement = 0;
50
- this.endpoint = "";
51
- this.timeout = 15000; // default to 15s
52
- this.events = new Map();
53
- this.unsubscribeSuspense = 1000;
54
- this.maxConnectionTries = 3;
55
- this.reconnectOnConnectionLoss = true;
56
- this.options = options;
57
- }
58
- WSRPC.prototype.connect = function (endpoint) {
47
+ function WSRPC(endpoint, options) {
59
48
  var _this = this;
60
- // force disconnect if already connected
61
- if (this.socket && this.socket.readyState === isomorphic_ws_1["default"].OPEN) {
62
- this.socket.close();
63
- }
49
+ this.socket = new isomorphic_ws_1["default"](endpoint, options);
50
+ this.methodIdIncrement = 0;
51
+ this.callTimeout = 3000;
64
52
  this.events = new Map();
65
- this.connectionTries = 0;
66
- return new Promise(function (resolve, reject) {
67
- _this.socket = new isomorphic_ws_1["default"](endpoint, _this.options);
68
- _this.endpoint = endpoint;
69
- _this.socket.addEventListener("open", function (event) {
70
- resolve(event);
71
- });
72
- _this.socket.addEventListener("close", function (event) {
73
- if (_this.reconnectOnConnectionLoss && !event.wasClean) {
74
- _this.tryReconnect();
75
- reject(new Error("Unhandled close. Reconnecting..."));
76
- }
77
- else {
78
- reject(event);
79
- }
80
- });
81
- _this.socket.addEventListener("error", function (err) {
82
- reject(err);
83
- });
53
+ this.socket.addEventListener("close", function () {
54
+ _this.events = new Map();
84
55
  });
85
- };
86
- WSRPC.prototype.tryReconnect = function () {
87
- var _this = this;
88
- this.connectionTries++;
89
- if (this.connectionTries > this.maxConnectionTries) {
90
- return;
91
- }
92
- this.socket = new isomorphic_ws_1["default"](this.endpoint, this.options);
93
- this.socket.addEventListener("open", function () {
94
- _this.connectionTries = 0;
95
- });
96
- this.socket.addEventListener("close", function (event) {
97
- _this.tryReconnect();
98
- });
99
- };
100
- WSRPC.prototype.close = function () {
101
- if (!this.socket)
102
- return;
103
- this.socket.close();
104
- };
105
- WSRPC.prototype.clearEvent = function (event) {
106
- var _this = this;
107
- var eventData = this.events.get(event);
108
- if (eventData) {
109
- eventData.listeners.forEach(function (listener) {
110
- _this.socket && _this.socket.removeEventListener("message", listener);
111
- });
112
- this.events["delete"](event);
113
- }
114
- };
115
- WSRPC.prototype.closeAllListens = function (event) {
56
+ }
57
+ WSRPC.prototype.closeListener = function (event, listener) {
116
58
  return __awaiter(this, void 0, void 0, function () {
117
- var _a, err, _;
118
- return __generator(this, function (_b) {
119
- switch (_b.label) {
120
- case 0:
121
- if (!this.events.has(event)) return [3 /*break*/, 2];
122
- return [4 /*yield*/, (0, await_to_js_1.to)(this.dataCall("unsubscribe", { notify: event }))];
123
- case 1:
124
- _a = _b.sent(), err = _a[0], _ = _a[1];
125
- if (err)
126
- return [2 /*return*/, Promise.reject(err)];
127
- this.clearEvent(event);
128
- _b.label = 2;
129
- case 2: return [2 /*return*/, Promise.resolve()];
59
+ var eventData;
60
+ return __generator(this, function (_a) {
61
+ eventData = this.events.get(event);
62
+ if (eventData) {
63
+ if (eventData.listeners.length > 1) {
64
+ eventData.listeners = eventData.listeners.filter(function (l) { return l !== listener; });
65
+ this.events.set(event, eventData);
66
+ }
67
+ else {
68
+ this.events["delete"](event);
69
+ this.socket.removeEventListener("message", eventData.onMessage);
70
+ this.dataCall("unsubscribe", { notify: event });
71
+ }
130
72
  }
73
+ return [2 /*return*/];
131
74
  });
132
75
  });
133
76
  };
134
- WSRPC.prototype.listenEvent = function (event, onData) {
77
+ WSRPC.prototype.listen = function (event, listener) {
135
78
  return __awaiter(this, void 0, void 0, function () {
136
- var onMessage, eventData, idRefObject, _a, err, _, eventData_1, closeListen;
79
+ var eventData, idRefObject_1, onMessage;
137
80
  var _this = this;
138
- return __generator(this, function (_b) {
139
- switch (_b.label) {
140
- case 0:
141
- onMessage = function (msgEvent) {
142
- var eventData = _this.events.get(event);
143
- if (eventData && typeof msgEvent.data === "string") {
144
- try {
145
- var data = (0, parse_json_1.parseJSON)(msgEvent.data);
146
- if (data.id === eventData.id) {
147
- if (data.error) {
148
- onData(msgEvent, undefined, new Error(data.error.message));
81
+ return __generator(this, function (_a) {
82
+ eventData = this.events.get(event);
83
+ if (eventData) {
84
+ eventData.listeners.push(listener);
85
+ this.events.set(event, eventData);
86
+ }
87
+ else {
88
+ idRefObject_1 = {};
89
+ this.dataCall("subscribe", { notify: event }, idRefObject_1);
90
+ onMessage = function (msgEvent) {
91
+ var eventData = _this.events.get(event);
92
+ if (eventData && typeof msgEvent.data === "string") {
93
+ try {
94
+ var data_1 = (0, parse_json_1.parseJSON)(msgEvent.data);
95
+ if (data_1.id === idRefObject_1.id) {
96
+ eventData.listeners.forEach(function (listener) {
97
+ if (data_1.error) {
98
+ listener(undefined, new Error(data_1.error.message));
149
99
  }
150
100
  else {
151
- onData(msgEvent, data.result, undefined);
101
+ listener(data_1.result, undefined);
152
102
  }
153
- }
154
- }
155
- catch (_a) {
156
- // can't parse json -- do nothing
103
+ });
157
104
  }
158
105
  }
159
- };
160
- eventData = this.events.get(event);
161
- if (!eventData) return [3 /*break*/, 1];
162
- if (eventData.unsubscribeTimeoutId) {
163
- // clear timeout to unsubscribe
164
- // because we got a new registered event and want to cancel the pending unsubscribe grace period
165
- clearTimeout(eventData.unsubscribeTimeoutId);
166
- }
167
- eventData.listeners.push(onMessage);
168
- return [3 /*break*/, 3];
169
- case 1:
170
- // important if multiple listenEvent are called without await at least we store listener before getting id
171
- // avoid trying to subscribe the same event multiple times
172
- this.events.set(event, { listeners: [onMessage] });
173
- idRefObject = {};
174
- return [4 /*yield*/, (0, await_to_js_1.to)(this.dataCall("subscribe", { notify: event }, idRefObject))];
175
- case 2:
176
- _a = _b.sent(), err = _a[0], _ = _a[1];
177
- if (err) {
178
- this.clearEvent(event);
179
- return [2 /*return*/, Promise.reject(err)];
180
- }
181
- eventData_1 = this.events.get(event);
182
- if (eventData_1)
183
- eventData_1.id = idRefObject.id;
184
- _b.label = 3;
185
- case 3:
186
- this.socket && this.socket.addEventListener("message", onMessage);
187
- closeListen = function () {
188
- var eventData = _this.events.get(event);
189
- if (eventData) {
190
- var listeners = eventData.listeners;
191
- for (var i = 0; i < listeners.length; i++) {
192
- if (listeners[i] === onMessage) {
193
- listeners.splice(i, 1);
194
- break;
195
- }
196
- }
197
- // no more listener so we unsubscribe from daemon websocket if socket still open
198
- if (listeners.length === 0) {
199
- // we use a grace period to unsubscribe (mostly because of react useEffect and avoid unecessary subscribe)
200
- eventData.unsubscribeTimeoutId = setTimeout(function () { return __awaiter(_this, void 0, void 0, function () {
201
- return __generator(this, function (_a) {
202
- if (this.socket && this.socket.readyState === isomorphic_ws_1["default"].OPEN) {
203
- this.dataCall("unsubscribe", { notify: event });
204
- }
205
- this.events["delete"](event);
206
- return [2 /*return*/];
207
- });
208
- }); }, _this.unsubscribeSuspense);
209
- }
106
+ catch (_a) {
107
+ // can't parse json -- do nothing
210
108
  }
211
- _this.socket && _this.socket.removeEventListener("message", onMessage);
212
- return Promise.resolve();
213
- };
214
- return [2 /*return*/, Promise.resolve(closeListen)];
109
+ }
110
+ };
111
+ this.socket.addEventListener("message", onMessage);
112
+ this.events.set(event, { onMessage: onMessage, listeners: [listener] });
215
113
  }
114
+ return [2 /*return*/];
216
115
  });
217
116
  });
218
117
  };
219
- WSRPC.prototype.batchCall = function (requests) {
220
- var _this = this;
221
- return new Promise(function (resolve, reject) { return __awaiter(_this, void 0, void 0, function () {
222
- var id, data, _a, err, res, items;
223
- return __generator(this, function (_b) {
224
- switch (_b.label) {
225
- case 0:
226
- id = this.methodIdIncrement++;
227
- requests.forEach(function (request) {
228
- request.id = id;
229
- request.jsonrpc = "2.0";
230
- });
231
- data = JSON.stringify(requests);
232
- return [4 /*yield*/, (0, await_to_js_1.to)(this.rawCall(id, data))];
233
- case 1:
234
- _a = _b.sent(), err = _a[0], res = _a[1];
235
- if (err)
236
- return [2 /*return*/, reject(err)];
237
- items = [];
238
- res.forEach(function (v) {
239
- if (v.error) {
240
- items.push(new Error(v.error.message));
241
- }
242
- else {
243
- items.push(v.result);
244
- }
245
- });
246
- return [2 /*return*/, resolve(items)];
247
- }
248
- });
249
- }); });
250
- };
251
118
  WSRPC.prototype.rawCall = function (id, body) {
252
119
  var _this = this;
253
120
  return new Promise(function (resolve, reject) {
254
- if (!_this.socket)
255
- return reject(new Error("Socket is not initialized."));
256
121
  if (_this.socket.readyState !== isomorphic_ws_1["default"].OPEN)
257
122
  return reject(new Error("Can't send msg. Socket is not opened."));
258
123
  var timeoutId = null;
259
124
  var onMessage = function (msgEvent) {
260
125
  if (typeof msgEvent.data === "string") {
261
- var data_1 = (0, parse_json_1.parseJSON)(msgEvent.data);
126
+ var data_2 = (0, parse_json_1.parseJSON)(msgEvent.data);
262
127
  var evaluate_data = function () {
263
128
  clearTimeout(timeoutId);
264
- _this.socket && _this.socket.removeEventListener("message", onMessage);
265
- if (data_1.error && data_1.error.message) {
266
- reject(new Error(data_1.error.message));
129
+ _this.socket.removeEventListener("message", onMessage);
130
+ if (data_2.error && data_2.error.message) {
131
+ reject(new Error(data_2.error.message));
267
132
  return;
268
133
  }
269
- return resolve(data_1);
134
+ return resolve(data_2);
270
135
  };
271
136
  // this is for batch call we match the id with first item id
272
- if (Array.isArray(data_1) && data_1.length > 0 && data_1[0].id === id) {
137
+ if (Array.isArray(data_2) && data_2.length > 0 && data_2[0].id === id) {
273
138
  return evaluate_data();
274
139
  }
275
140
  // the msg id is matching so we can evaluate the data
276
- if (data_1.id === id) {
141
+ if (data_2.id === id) {
277
142
  return evaluate_data();
278
143
  }
279
144
  // special XSWD case - sending first call will return null id
280
- if (data_1.id === null && id === 0) {
145
+ if (data_2.id === null && id === 0) {
281
146
  return evaluate_data();
282
147
  }
283
148
  }
284
149
  };
285
150
  _this.socket.addEventListener("message", onMessage);
286
- if (_this.timeout > 0) {
151
+ if (_this.callTimeout > 0) {
287
152
  timeoutId = setTimeout(function () {
288
153
  _this.socket && _this.socket.removeEventListener("message", onMessage);
289
154
  reject(new Error("timeout"));
290
- }, _this.timeout);
155
+ }, _this.callTimeout);
291
156
  }
292
157
  if (_this.socket.readyState === isomorphic_ws_1["default"].OPEN) {
293
158
  _this.socket.send(body);
@@ -308,7 +173,7 @@ var WSRPC = /** @class */ (function () {
308
173
  if (params)
309
174
  request.params = params;
310
175
  data = JSON.stringify(request);
311
- return [4 /*yield*/, (0, await_to_js_1.to)(this.rawCall(id, data))];
176
+ return [4 /*yield*/, (0, await_to_js_1["default"])(this.rawCall(id, data))];
312
177
  case 1:
313
178
  _a = _b.sent(), err = _a[0], res = _a[1];
314
179
  if (err)
@@ -321,6 +186,38 @@ var WSRPC = /** @class */ (function () {
321
186
  });
322
187
  }); });
323
188
  };
189
+ WSRPC.prototype.batchCall = function (requests) {
190
+ var _this = this;
191
+ return new Promise(function (resolve, reject) { return __awaiter(_this, void 0, void 0, function () {
192
+ var id, data, _a, err, res, items;
193
+ return __generator(this, function (_b) {
194
+ switch (_b.label) {
195
+ case 0:
196
+ id = this.methodIdIncrement++;
197
+ requests.forEach(function (request) {
198
+ request.id = id;
199
+ request.jsonrpc = "2.0";
200
+ });
201
+ data = JSON.stringify(requests);
202
+ return [4 /*yield*/, (0, await_to_js_1["default"])(this.rawCall(id, data))];
203
+ case 1:
204
+ _a = _b.sent(), err = _a[0], res = _a[1];
205
+ if (err)
206
+ return [2 /*return*/, reject(err)];
207
+ items = [];
208
+ res.forEach(function (v) {
209
+ if (v.error) {
210
+ items.push(new Error(v.error.message));
211
+ }
212
+ else {
213
+ items.push(v.result);
214
+ }
215
+ });
216
+ return [2 /*return*/, resolve(items)];
217
+ }
218
+ });
219
+ }); });
220
+ };
324
221
  return WSRPC;
325
222
  }());
326
223
  exports.WSRPC = WSRPC;
@@ -27,29 +27,11 @@ var WalletMethods = /** @class */ (function () {
27
27
  WalletMethods.prototype.dataCall = function (method, params) {
28
28
  return this.ws.dataCall(this.prefix + method, params);
29
29
  };
30
- WalletMethods.prototype.onNewTopoheight = function (onData) {
31
- return this.ws.listenEvent(this.prefix + types_1.RPCEvent.NewTopoheight, onData);
30
+ WalletMethods.prototype.closeListener = function (event, listener) {
31
+ this.ws.closeListener(event, listener);
32
32
  };
33
- WalletMethods.prototype.onNewAsset = function (onData) {
34
- return this.ws.listenEvent(this.prefix + types_1.RPCEvent.NewAsset, onData);
35
- };
36
- WalletMethods.prototype.onNewTransaction = function (onData) {
37
- return this.ws.listenEvent(this.prefix + types_1.RPCEvent.NewTransaction, onData);
38
- };
39
- WalletMethods.prototype.onBalanceChanged = function (onData) {
40
- return this.ws.listenEvent(this.prefix + types_1.RPCEvent.BalanceChanged, onData);
41
- };
42
- WalletMethods.prototype.onRescan = function (onData) {
43
- return this.ws.listenEvent(this.prefix + types_1.RPCEvent.Rescan, onData);
44
- };
45
- WalletMethods.prototype.onHistorySynced = function (onData) {
46
- return this.ws.listenEvent(this.prefix + types_1.RPCEvent.HistorySynced, onData);
47
- };
48
- WalletMethods.prototype.onOnline = function (onData) {
49
- return this.ws.listenEvent(this.prefix + types_1.RPCEvent.Online, onData);
50
- };
51
- WalletMethods.prototype.onOffline = function (onData) {
52
- return this.ws.listenEvent(this.prefix + types_1.RPCEvent.Offline, onData);
33
+ WalletMethods.prototype.listen = function (event, listener) {
34
+ this.ws.listen(this.prefix + event, listener);
53
35
  };
54
36
  WalletMethods.prototype.getVersion = function () {
55
37
  return this.dataCall(types_1.RPCMethod.GetVersion);
@@ -177,8 +159,8 @@ var WalletMethods = /** @class */ (function () {
177
159
  exports.WalletMethods = WalletMethods;
178
160
  var WS = /** @class */ (function (_super) {
179
161
  __extends(WS, _super);
180
- function WS(username, password) {
181
- var _this = _super.call(this, { auth: "".concat(username, ":").concat(password) }) || this;
162
+ function WS(endpoint, username, password) {
163
+ var _this = _super.call(this, endpoint, { auth: "".concat(username, ":").concat(password) }) || this;
182
164
  _this.methods = new WalletMethods(_this);
183
165
  return _this;
184
166
  }