@rabby-wallet/hyperliquid-sdk 1.0.0-beta.4 → 1.0.0-beta.5
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.
|
@@ -18,7 +18,6 @@ class WebSocketClient {
|
|
|
18
18
|
constructor(config) {
|
|
19
19
|
this.ws = null;
|
|
20
20
|
this.subscriptions = new Map();
|
|
21
|
-
this.subscriptionId = 0;
|
|
22
21
|
this.reconnectAttempts = 0;
|
|
23
22
|
this.isConnecting = false;
|
|
24
23
|
this.pendingSubscriptions = [];
|
|
@@ -57,13 +56,13 @@ class WebSocketClient {
|
|
|
57
56
|
this.ws.onopen = () => {
|
|
58
57
|
this.isConnecting = false;
|
|
59
58
|
this.reconnectAttempts = 0;
|
|
60
|
-
console.log('WebSocket connected');
|
|
59
|
+
console.log('hyperliquid sdk WebSocket connected');
|
|
61
60
|
// Resubscribe to pending subscriptions
|
|
62
|
-
this.pendingSubscriptions.forEach(({
|
|
61
|
+
this.pendingSubscriptions.forEach(({ message, callback }) => {
|
|
63
62
|
var _a;
|
|
64
|
-
this.subscriptions.set(id, callback);
|
|
65
63
|
if (((_a = this.ws) === null || _a === void 0 ? void 0 : _a.readyState) === WebSocket.OPEN) {
|
|
66
|
-
this.
|
|
64
|
+
this.subscriptions.set(message.subscription.type, callback);
|
|
65
|
+
this.ws.send(JSON.stringify(Object.assign(Object.assign({}, message), { method: 'subscribe' })));
|
|
67
66
|
}
|
|
68
67
|
});
|
|
69
68
|
this.pendingSubscriptions = [];
|
|
@@ -72,12 +71,10 @@ class WebSocketClient {
|
|
|
72
71
|
this.ws.onmessage = (event) => {
|
|
73
72
|
try {
|
|
74
73
|
const data = JSON.parse(event.data);
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
callback(data.data);
|
|
80
|
-
}
|
|
74
|
+
const type = data.channel;
|
|
75
|
+
const callback = this.subscriptions.get(type);
|
|
76
|
+
if (callback) {
|
|
77
|
+
callback(data.data);
|
|
81
78
|
}
|
|
82
79
|
}
|
|
83
80
|
catch (error) {
|
|
@@ -117,15 +114,13 @@ class WebSocketClient {
|
|
|
117
114
|
*/
|
|
118
115
|
subscribe(message, callback) {
|
|
119
116
|
var _a;
|
|
120
|
-
const id = ++this.subscriptionId;
|
|
121
|
-
const subscriptionMessage = Object.assign(Object.assign({}, message), { id });
|
|
122
117
|
if (((_a = this.ws) === null || _a === void 0 ? void 0 : _a.readyState) === WebSocket.OPEN) {
|
|
123
|
-
this.subscriptions.set(
|
|
124
|
-
this.ws.send(JSON.stringify(Object.assign(Object.assign({},
|
|
118
|
+
this.subscriptions.set(message.subscription.type, callback);
|
|
119
|
+
this.ws.send(JSON.stringify(Object.assign(Object.assign({}, message), { method: 'subscribe' })));
|
|
125
120
|
}
|
|
126
121
|
else {
|
|
127
122
|
// Store pending subscription for when connection is established
|
|
128
|
-
this.pendingSubscriptions.push({
|
|
123
|
+
this.pendingSubscriptions.push({ message, callback });
|
|
129
124
|
// Auto-connect if not connected
|
|
130
125
|
if (!this.isConnecting && (!this.ws || this.ws.readyState === WebSocket.CLOSED)) {
|
|
131
126
|
this.connect().catch(console.error);
|
|
@@ -134,12 +129,12 @@ class WebSocketClient {
|
|
|
134
129
|
return {
|
|
135
130
|
unsubscribe: () => {
|
|
136
131
|
var _a;
|
|
137
|
-
this.subscriptions.delete(
|
|
132
|
+
this.subscriptions.delete(message.subscription.type);
|
|
138
133
|
if (((_a = this.ws) === null || _a === void 0 ? void 0 : _a.readyState) === WebSocket.OPEN) {
|
|
139
|
-
this.ws.send(JSON.stringify(Object.assign(Object.assign({},
|
|
134
|
+
this.ws.send(JSON.stringify(Object.assign(Object.assign({}, message), { method: 'unsubscribe' })));
|
|
140
135
|
}
|
|
141
136
|
// Remove from pending subscriptions
|
|
142
|
-
this.pendingSubscriptions = this.pendingSubscriptions.filter(sub => sub.
|
|
137
|
+
this.pendingSubscriptions = this.pendingSubscriptions.filter(sub => sub.message.subscription.type !== message.subscription.type);
|
|
143
138
|
},
|
|
144
139
|
};
|
|
145
140
|
}
|
package/package.json
CHANGED