@xelis/sdk 0.5.7 → 0.5.8
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.
- package/lib/websocket.d.ts +6 -0
- package/lib/websocket.js +15 -9
- package/package.json +2 -2
package/lib/websocket.d.ts
CHANGED
|
@@ -9,13 +9,19 @@ export declare class WS {
|
|
|
9
9
|
reconnectOnConnectionLoss: boolean;
|
|
10
10
|
maxConnectionTries: number;
|
|
11
11
|
connectionTries: number;
|
|
12
|
+
methodIdIncrement: number;
|
|
12
13
|
private events;
|
|
13
14
|
constructor();
|
|
14
15
|
connect(endpoint: string): Promise<unknown>;
|
|
15
16
|
tryReconnect(): void;
|
|
17
|
+
close(): void;
|
|
16
18
|
private clearEvent;
|
|
17
19
|
closeAllListens(event: string): Promise<void>;
|
|
18
20
|
listenEvent<T>(event: string, onData: (msgEvent: MessageEvent, data?: T, err?: Error) => void): Promise<() => Promise<void>>;
|
|
19
21
|
call<T>(method: string, params?: any): Promise<RPCResponse<T>>;
|
|
20
22
|
dataCall<T>(method: string, params?: any): Promise<T>;
|
|
23
|
+
createRequestMethod(method: string, params?: any): {
|
|
24
|
+
data: string;
|
|
25
|
+
id: number;
|
|
26
|
+
};
|
|
21
27
|
}
|
package/lib/websocket.js
CHANGED
|
@@ -1,16 +1,9 @@
|
|
|
1
1
|
import WebSocket from 'isomorphic-ws';
|
|
2
2
|
import to from 'await-to-js';
|
|
3
|
-
function createRequestMethod(method, params) {
|
|
4
|
-
const id = Math.floor(Date.now() * Math.random());
|
|
5
|
-
const request = { id: id, jsonrpc: `2.0`, method };
|
|
6
|
-
if (params)
|
|
7
|
-
request.params = params;
|
|
8
|
-
const data = JSON.stringify(request);
|
|
9
|
-
return { data, id };
|
|
10
|
-
}
|
|
11
3
|
export class WS {
|
|
12
4
|
constructor() {
|
|
13
5
|
this.connectionTries = 0;
|
|
6
|
+
this.methodIdIncrement = 0;
|
|
14
7
|
this.endpoint = "";
|
|
15
8
|
this.timeout = 3000;
|
|
16
9
|
this.events = {};
|
|
@@ -58,6 +51,11 @@ export class WS {
|
|
|
58
51
|
this.tryReconnect();
|
|
59
52
|
});
|
|
60
53
|
}
|
|
54
|
+
close() {
|
|
55
|
+
if (!this.socket)
|
|
56
|
+
return;
|
|
57
|
+
this.socket.close();
|
|
58
|
+
}
|
|
61
59
|
clearEvent(event) {
|
|
62
60
|
this.events[event].listeners.forEach(listener => {
|
|
63
61
|
this.socket && this.socket.removeEventListener(`message`, listener);
|
|
@@ -147,7 +145,7 @@ export class WS {
|
|
|
147
145
|
}
|
|
148
146
|
call(method, params) {
|
|
149
147
|
return new Promise((resolve, reject) => {
|
|
150
|
-
const { data, id } = createRequestMethod(method, params);
|
|
148
|
+
const { data, id } = this.createRequestMethod(method, params);
|
|
151
149
|
let timeoutId = null;
|
|
152
150
|
const onMessage = (msgEvent) => {
|
|
153
151
|
if (typeof msgEvent.data === `string`) {
|
|
@@ -181,4 +179,12 @@ export class WS {
|
|
|
181
179
|
return resolve(res.result);
|
|
182
180
|
});
|
|
183
181
|
}
|
|
182
|
+
createRequestMethod(method, params) {
|
|
183
|
+
const id = this.methodIdIncrement++;
|
|
184
|
+
const request = { id: id, jsonrpc: `2.0`, method };
|
|
185
|
+
if (params)
|
|
186
|
+
request.params = params;
|
|
187
|
+
const data = JSON.stringify(request);
|
|
188
|
+
return { data, id };
|
|
189
|
+
}
|
|
184
190
|
}
|
package/package.json
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
{
|
|
2
|
-
"version": "0.5.
|
|
2
|
+
"version": "0.5.8",
|
|
3
3
|
"name": "@xelis/sdk",
|
|
4
4
|
"description": "Xelis software development kit for JS",
|
|
5
5
|
"repository": {
|
|
@@ -9,7 +9,7 @@
|
|
|
9
9
|
"homepage": "https://github.com/xelis-project/xelis-js-sdk#readme",
|
|
10
10
|
"scripts": {
|
|
11
11
|
"test": "jest",
|
|
12
|
-
"build": "npx tsc --declaration && cp package.json ./dist && cp README.md ./dist",
|
|
12
|
+
"build": "rm -r ./dist && npx tsc --declaration && cp package.json ./dist && cp README.md ./dist",
|
|
13
13
|
"publish": "cd ./dist && npm publish"
|
|
14
14
|
},
|
|
15
15
|
"devDependencies": {
|