universe-code 0.0.83 → 0.0.85
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/dist/index.d.ts +2 -1
- package/dist/index.js +5 -7
- package/dist/socket/index.js +186 -0
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -3,4 +3,5 @@ declare module 'universe-code/core';
|
|
|
3
3
|
declare module 'universe-code/indexdb';
|
|
4
4
|
declare module 'universe-code/react';
|
|
5
5
|
declare module 'universe-code/angular';
|
|
6
|
-
declare module 'universe-code/uiux';
|
|
6
|
+
declare module 'universe-code/uiux';
|
|
7
|
+
declare module 'universe-code/socket';
|
package/dist/index.js
CHANGED
|
@@ -1,10 +1,8 @@
|
|
|
1
|
-
|
|
1
|
+
/* parent file */
|
|
2
2
|
const indexdb = require("./indexdb");
|
|
3
|
-
|
|
4
|
-
// (Socket will be added later)
|
|
5
|
-
// const socket = require("./socket");
|
|
3
|
+
const socket = require("./socket");
|
|
6
4
|
|
|
7
5
|
module.exports = {
|
|
8
|
-
...indexdb
|
|
9
|
-
|
|
10
|
-
};
|
|
6
|
+
...indexdb,
|
|
7
|
+
...socket
|
|
8
|
+
};
|
package/dist/socket/index.js
CHANGED
|
@@ -0,0 +1,186 @@
|
|
|
1
|
+
import { io } from "socket.io-client";
|
|
2
|
+
|
|
3
|
+
export class WebSocketService {
|
|
4
|
+
static _instance;
|
|
5
|
+
|
|
6
|
+
socket = null;
|
|
7
|
+
config = null;
|
|
8
|
+
coreListenersBound = false;
|
|
9
|
+
lastPayloads = null;
|
|
10
|
+
lastSubscribeKey = "";
|
|
11
|
+
pendingListeners = [];
|
|
12
|
+
|
|
13
|
+
constructor() {}
|
|
14
|
+
|
|
15
|
+
static get instance() {
|
|
16
|
+
if (!WebSocketService._instance) {
|
|
17
|
+
WebSocketService._instance = new WebSocketService();
|
|
18
|
+
}
|
|
19
|
+
return WebSocketService._instance;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/** 🚨 REQUIRED strict config */
|
|
23
|
+
init(config) {
|
|
24
|
+
const required = [
|
|
25
|
+
"baseUrl",
|
|
26
|
+
"path",
|
|
27
|
+
"transports",
|
|
28
|
+
"autoConnect",
|
|
29
|
+
"reconnection",
|
|
30
|
+
"reconnectionAttempts",
|
|
31
|
+
"reconnectionDelay",
|
|
32
|
+
"auth",
|
|
33
|
+
];
|
|
34
|
+
|
|
35
|
+
required.forEach((key) => {
|
|
36
|
+
if (config[key] === undefined) {
|
|
37
|
+
throw new Error(`Missing required WebSocket config: ${key}`);
|
|
38
|
+
}
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
if (!config.auth || config.auth.token === undefined) {
|
|
42
|
+
throw new Error("Missing required WebSocket auth.token");
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
this.config = config;
|
|
46
|
+
|
|
47
|
+
this.createSocket();
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/** 🔑 Token update */
|
|
51
|
+
setToken(token) {
|
|
52
|
+
if (!this.config) throw new Error("WebSocket not initialized");
|
|
53
|
+
|
|
54
|
+
if (!this.config.auth) this.config.auth = {};
|
|
55
|
+
|
|
56
|
+
this.config.auth.token = token;
|
|
57
|
+
|
|
58
|
+
if (!token) this.disconnect();
|
|
59
|
+
else this.connect();
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/** 🚀 Create */
|
|
63
|
+
createSocket() {
|
|
64
|
+
if (this.socket) return;
|
|
65
|
+
|
|
66
|
+
if (!this.config) throw new Error("WebSocket config not initialized");
|
|
67
|
+
|
|
68
|
+
this.socket = io(this.config.baseUrl, {
|
|
69
|
+
path: this.config.path,
|
|
70
|
+
transports: this.config.transports,
|
|
71
|
+
autoConnect: this.config.autoConnect,
|
|
72
|
+
reconnection: this.config.reconnection,
|
|
73
|
+
reconnectionAttempts: this.config.reconnectionAttempts,
|
|
74
|
+
reconnectionDelay: this.config.reconnectionDelay,
|
|
75
|
+
|
|
76
|
+
// ⬇️ token now from auth object
|
|
77
|
+
auth:
|
|
78
|
+
this.config.auth?.token
|
|
79
|
+
? { token: this.config.auth.token }
|
|
80
|
+
: undefined,
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
this.bindCoreListeners();
|
|
84
|
+
|
|
85
|
+
if (this.config.autoConnect) {
|
|
86
|
+
this.connect();
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/** 🔌 Connect */
|
|
91
|
+
connect() {
|
|
92
|
+
if (!this.config) throw new Error("WebSocket config not initialized");
|
|
93
|
+
|
|
94
|
+
if (!this.config.auth?.token) {
|
|
95
|
+
throw new Error("Token missing in auth.token: cannot connect WebSocket");
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
if (!this.socket) this.createSocket();
|
|
99
|
+
|
|
100
|
+
if (this.socket && !this.socket.connected) {
|
|
101
|
+
this.socket.connect();
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/** ❌ Disconnect */
|
|
106
|
+
disconnect() {
|
|
107
|
+
if (!this.socket) return;
|
|
108
|
+
|
|
109
|
+
this.socket.removeAllListeners();
|
|
110
|
+
this.socket.disconnect();
|
|
111
|
+
|
|
112
|
+
this.socket = null;
|
|
113
|
+
this.coreListenersBound = false;
|
|
114
|
+
this.lastPayloads = null;
|
|
115
|
+
this.lastSubscribeKey = "";
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/** 🧠 internal */
|
|
119
|
+
bindCoreListeners() {
|
|
120
|
+
if (!this.socket || this.coreListenersBound) return;
|
|
121
|
+
|
|
122
|
+
this.coreListenersBound = true;
|
|
123
|
+
|
|
124
|
+
this.socket.on("connect", () => {
|
|
125
|
+
this.resubscribeIfNeeded();
|
|
126
|
+
|
|
127
|
+
if (this.pendingListeners.length) {
|
|
128
|
+
this.pendingListeners.forEach(({ event, handler }) => {
|
|
129
|
+
this.socket.on(event, handler);
|
|
130
|
+
});
|
|
131
|
+
this.pendingListeners = [];
|
|
132
|
+
}
|
|
133
|
+
});
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
resubscribeIfNeeded() {
|
|
137
|
+
if (this.lastPayloads && this.socket) {
|
|
138
|
+
this.socket.emit("subscribeMarket", this.lastPayloads);
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
/** 📩 Subscribe */
|
|
143
|
+
subscribeMarket(payload) {
|
|
144
|
+
if (!this.socket) throw new Error("Socket not initialized");
|
|
145
|
+
|
|
146
|
+
const key = JSON.stringify(payload);
|
|
147
|
+
if (key === this.lastSubscribeKey) return;
|
|
148
|
+
|
|
149
|
+
this.lastSubscribeKey = key;
|
|
150
|
+
this.lastPayloads = payload;
|
|
151
|
+
|
|
152
|
+
if (!this.socket.connected) {
|
|
153
|
+
this.connect();
|
|
154
|
+
return;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
this.socket.emit("subscribeMarket", payload);
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
/** 🗑️ Unsubscribe */
|
|
161
|
+
unsubscribeMarket(payload) {
|
|
162
|
+
if (!this.socket) throw new Error("Socket not initialized");
|
|
163
|
+
|
|
164
|
+
const key = JSON.stringify(payload);
|
|
165
|
+
if (key !== this.lastSubscribeKey) return;
|
|
166
|
+
|
|
167
|
+
this.lastSubscribeKey = "";
|
|
168
|
+
this.socket.emit("unsubscribeMarket", payload);
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
/** 👂 Listen */
|
|
172
|
+
onEvent(eventName, handler) {
|
|
173
|
+
if (!this.socket) throw new Error("Socket not initialized");
|
|
174
|
+
|
|
175
|
+
const wrapped = (data) => handler(data);
|
|
176
|
+
this.socket.on(eventName, wrapped);
|
|
177
|
+
|
|
178
|
+
return () => this.socket.off(eventName, wrapped);
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
/** 📤 Emit */
|
|
182
|
+
sendMessage(event, data) {
|
|
183
|
+
if (!this.socket) throw new Error("Socket not initialized");
|
|
184
|
+
this.socket.emit(event, data);
|
|
185
|
+
}
|
|
186
|
+
}
|