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