universe-code 0.0.82 → 0.0.84

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 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,7 @@
1
- // IndexedDB exports
2
1
  const indexdb = require("./indexdb");
3
-
4
- // (Socket will be added later)
5
- // const socket = require("./socket");
2
+ const socket = require("./socket");
6
3
 
7
4
  module.exports = {
8
- ...indexdb
9
- // ...socket
10
- };
5
+ ...indexdb,
6
+ ...socket
7
+ };
@@ -0,0 +1,174 @@
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
+ "token",
33
+ ];
34
+
35
+ required.forEach((key) => {
36
+ if (config[key] === undefined) {
37
+ throw new Error(`Missing required WebSocket config: ${key}`);
38
+ }
39
+ });
40
+
41
+ this.config = config;
42
+
43
+ this.createSocket();
44
+ }
45
+
46
+ /** 🔑 Token update */
47
+ setToken(token) {
48
+ if (!this.config) throw new Error("WebSocket not initialized");
49
+
50
+ this.config.token = token;
51
+
52
+ if (!token) this.disconnect();
53
+ else this.connect();
54
+ }
55
+
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
+ });
73
+
74
+ this.bindCoreListeners();
75
+
76
+ if (this.config.autoConnect) {
77
+ this.connect();
78
+ }
79
+ }
80
+
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");
85
+
86
+ if (!this.socket) this.createSocket();
87
+
88
+ if (this.socket && !this.socket.connected) {
89
+ this.socket.connect();
90
+ }
91
+ }
92
+
93
+ /** ❌ Disconnect */
94
+ disconnect() {
95
+ if (!this.socket) return;
96
+
97
+ this.socket.removeAllListeners();
98
+ this.socket.disconnect();
99
+
100
+ this.socket = null;
101
+ this.coreListenersBound = false;
102
+ this.lastPayloads = null;
103
+ this.lastSubscribeKey = "";
104
+ }
105
+
106
+ /** 🧠 internal */
107
+ bindCoreListeners() {
108
+ if (!this.socket || this.coreListenersBound) return;
109
+
110
+ this.coreListenersBound = true;
111
+
112
+ this.socket.on("connect", () => {
113
+ this.resubscribeIfNeeded();
114
+
115
+ if (this.pendingListeners.length) {
116
+ this.pendingListeners.forEach(({ event, handler }) => {
117
+ this.socket.on(event, handler);
118
+ });
119
+ this.pendingListeners = [];
120
+ }
121
+ });
122
+ }
123
+
124
+ resubscribeIfNeeded() {
125
+ if (this.lastPayloads && this.socket) {
126
+ this.socket.emit("subscribeMarket", this.lastPayloads);
127
+ }
128
+ }
129
+
130
+ /** 📩 Subscribe */
131
+ subscribeMarket(payload) {
132
+ if (!this.socket) throw new Error("Socket not initialized");
133
+
134
+ const key = JSON.stringify(payload);
135
+ if (key === this.lastSubscribeKey) return;
136
+
137
+ this.lastSubscribeKey = key;
138
+ this.lastPayloads = payload;
139
+
140
+ if (!this.socket.connected) {
141
+ this.connect();
142
+ return;
143
+ }
144
+
145
+ this.socket.emit("subscribeMarket", payload);
146
+ }
147
+
148
+ /** 🗑️ Unsubscribe */
149
+ unsubscribeMarket(payload) {
150
+ if (!this.socket) throw new Error("Socket not initialized");
151
+
152
+ const key = JSON.stringify(payload);
153
+ if (key !== this.lastSubscribeKey) return;
154
+
155
+ this.lastSubscribeKey = "";
156
+ this.socket.emit("unsubscribeMarket", payload);
157
+ }
158
+
159
+ /** 👂 Listen */
160
+ onEvent(eventName, handler) {
161
+ if (!this.socket) throw new Error("Socket not initialized");
162
+
163
+ const wrapped = (data) => handler(data);
164
+ this.socket.on(eventName, wrapped);
165
+
166
+ return () => this.socket.off(eventName, wrapped);
167
+ }
168
+
169
+ /** 📤 Emit */
170
+ sendMessage(event, data) {
171
+ if (!this.socket) throw new Error("Socket not initialized");
172
+ this.socket.emit(event, data);
173
+ }
174
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "universe-code",
3
- "version": "0.0.82",
3
+ "version": "0.0.84",
4
4
  "description": "Universal utility functions for all JS frameworks",
5
5
  "license": "ISC",
6
6
  "type": "module",
@@ -54,7 +54,8 @@
54
54
  "dependencies": {
55
55
  "@angular/common": "^21.0.6",
56
56
  "@angular/core": "^21.0.6",
57
- "react": "^19.2.3"
57
+ "react": "^19.2.3",
58
+ "socket.io-client": "^4.8.3"
58
59
  },
59
60
  "keywords": [
60
61
  "universe-code",