sockr-client 1.0.0
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/README.md +239 -0
- package/dist/index.d.mts +129 -0
- package/dist/index.d.ts +129 -0
- package/dist/index.js +682 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +634 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +65 -0
package/dist/index.js
ADDED
|
@@ -0,0 +1,682 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
+
};
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "default"), secondTarget && __copyProps(secondTarget, mod, "default"));
|
|
21
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
22
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
23
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
24
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
25
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
26
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
27
|
+
mod
|
|
28
|
+
));
|
|
29
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
30
|
+
|
|
31
|
+
// src/index.ts
|
|
32
|
+
var index_exports = {};
|
|
33
|
+
__export(index_exports, {
|
|
34
|
+
ConnectionManager: () => ConnectionManager,
|
|
35
|
+
ConnectionState: () => ConnectionState,
|
|
36
|
+
EventEmitter: () => EventEmitter,
|
|
37
|
+
SocketClient: () => SocketClient,
|
|
38
|
+
SocketProvider: () => SocketProvider,
|
|
39
|
+
useMessages: () => useMessages,
|
|
40
|
+
usePresence: () => usePresence,
|
|
41
|
+
useSendMessage: () => useSendMessage,
|
|
42
|
+
useSocket: () => useSocket,
|
|
43
|
+
useSocketContext: () => useSocketContext,
|
|
44
|
+
useSocketEvent: () => useSocketEvent,
|
|
45
|
+
useTypingIndicator: () => useTypingIndicator
|
|
46
|
+
});
|
|
47
|
+
module.exports = __toCommonJS(index_exports);
|
|
48
|
+
|
|
49
|
+
// src/core/SocketClient.ts
|
|
50
|
+
var import_socket = require("socket.io-client");
|
|
51
|
+
var import_sockr_shared = require("sockr-shared");
|
|
52
|
+
|
|
53
|
+
// src/core/EventEmitter.ts
|
|
54
|
+
var EventEmitter = class {
|
|
55
|
+
constructor() {
|
|
56
|
+
this.events = /* @__PURE__ */ new Map();
|
|
57
|
+
}
|
|
58
|
+
on(event, handler) {
|
|
59
|
+
if (!this.events.has(event)) {
|
|
60
|
+
this.events.set(event, []);
|
|
61
|
+
}
|
|
62
|
+
const handlers = this.events.get(event);
|
|
63
|
+
handlers.push(handler);
|
|
64
|
+
return () => this.off(event, handler);
|
|
65
|
+
}
|
|
66
|
+
off(event, handler) {
|
|
67
|
+
const handlers = this.events.get(event);
|
|
68
|
+
if (!handlers) return;
|
|
69
|
+
const index = handlers.indexOf(handler);
|
|
70
|
+
if (index !== -1) {
|
|
71
|
+
handlers.splice(index, 1);
|
|
72
|
+
}
|
|
73
|
+
if (handlers.length === 0) {
|
|
74
|
+
this.events.delete(event);
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
emit(event, ...args) {
|
|
78
|
+
const handlers = this.events.get(event);
|
|
79
|
+
if (!handlers) return;
|
|
80
|
+
handlers.forEach((handler) => {
|
|
81
|
+
try {
|
|
82
|
+
handler(...args);
|
|
83
|
+
} catch (error) {
|
|
84
|
+
console.error(`Error in event handler for ${event}:`, error);
|
|
85
|
+
}
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
removeAllListeners(event) {
|
|
89
|
+
if (event) {
|
|
90
|
+
this.events.delete(event);
|
|
91
|
+
} else {
|
|
92
|
+
this.events.clear();
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
};
|
|
96
|
+
|
|
97
|
+
// src/core/ConnectionManager.ts
|
|
98
|
+
var ConnectionState = /* @__PURE__ */ ((ConnectionState2) => {
|
|
99
|
+
ConnectionState2["DISCONNECTED"] = "disconnected";
|
|
100
|
+
ConnectionState2["CONNECTING"] = "connecting";
|
|
101
|
+
ConnectionState2["CONNECTED"] = "connected";
|
|
102
|
+
ConnectionState2["AUTHENTICATED"] = "authenticated";
|
|
103
|
+
ConnectionState2["ERROR"] = "error";
|
|
104
|
+
ConnectionState2["RECONNECTING"] = "reconnecting";
|
|
105
|
+
return ConnectionState2;
|
|
106
|
+
})(ConnectionState || {});
|
|
107
|
+
var ConnectionManager = class {
|
|
108
|
+
constructor(maxReconnectAttempts = 5) {
|
|
109
|
+
this.state = "disconnected" /* DISCONNECTED */;
|
|
110
|
+
this.listeners = /* @__PURE__ */ new Set();
|
|
111
|
+
this.reconnectAttempts = 0;
|
|
112
|
+
this.maxReconnectAttempts = maxReconnectAttempts;
|
|
113
|
+
}
|
|
114
|
+
getState() {
|
|
115
|
+
return this.state;
|
|
116
|
+
}
|
|
117
|
+
setState(state) {
|
|
118
|
+
if (this.state !== state) {
|
|
119
|
+
this.state = state;
|
|
120
|
+
this.notifyListeners();
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
isConnected() {
|
|
124
|
+
return this.state === "connected" /* CONNECTED */ || this.state === "authenticated" /* AUTHENTICATED */;
|
|
125
|
+
}
|
|
126
|
+
isAuthenticated() {
|
|
127
|
+
return this.state === "authenticated" /* AUTHENTICATED */;
|
|
128
|
+
}
|
|
129
|
+
onStateChange(listener) {
|
|
130
|
+
this.listeners.add(listener);
|
|
131
|
+
return () => this.listeners.delete(listener);
|
|
132
|
+
}
|
|
133
|
+
notifyListeners() {
|
|
134
|
+
this.listeners.forEach((listener) => {
|
|
135
|
+
try {
|
|
136
|
+
listener(this.state);
|
|
137
|
+
} catch (error) {
|
|
138
|
+
console.error("Error in connection state listener:", error);
|
|
139
|
+
}
|
|
140
|
+
});
|
|
141
|
+
}
|
|
142
|
+
incrementReconnectAttempts() {
|
|
143
|
+
this.reconnectAttempts++;
|
|
144
|
+
return this.reconnectAttempts;
|
|
145
|
+
}
|
|
146
|
+
resetReconnectAttempts() {
|
|
147
|
+
this.reconnectAttempts = 0;
|
|
148
|
+
}
|
|
149
|
+
getReconnectAttempts() {
|
|
150
|
+
return this.reconnectAttempts;
|
|
151
|
+
}
|
|
152
|
+
canReconnect() {
|
|
153
|
+
return this.reconnectAttempts < this.maxReconnectAttempts;
|
|
154
|
+
}
|
|
155
|
+
reset() {
|
|
156
|
+
this.state = "disconnected" /* DISCONNECTED */;
|
|
157
|
+
this.reconnectAttempts = 0;
|
|
158
|
+
this.listeners.clear();
|
|
159
|
+
}
|
|
160
|
+
};
|
|
161
|
+
|
|
162
|
+
// src/core/SocketClient.ts
|
|
163
|
+
var SocketClient = class {
|
|
164
|
+
constructor(config) {
|
|
165
|
+
this.socket = null;
|
|
166
|
+
this.userId = null;
|
|
167
|
+
this.reconnectTimer = null;
|
|
168
|
+
this.config = {
|
|
169
|
+
autoConnect: true,
|
|
170
|
+
reconnection: true,
|
|
171
|
+
reconnectionAttempts: 5,
|
|
172
|
+
reconnectionDelay: 1e3,
|
|
173
|
+
timeout: 2e4,
|
|
174
|
+
transports: ["websocket", "polling"],
|
|
175
|
+
...config
|
|
176
|
+
};
|
|
177
|
+
this.eventEmitter = new EventEmitter();
|
|
178
|
+
this.connectionManager = new ConnectionManager(
|
|
179
|
+
this.config.reconnectionAttempts
|
|
180
|
+
);
|
|
181
|
+
if (this.config.autoConnect) {
|
|
182
|
+
this.connect();
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
connect() {
|
|
186
|
+
if (this.socket?.connected) {
|
|
187
|
+
console.warn("Socket already connected");
|
|
188
|
+
return;
|
|
189
|
+
}
|
|
190
|
+
this.connectionManager.setState("connecting" /* CONNECTING */);
|
|
191
|
+
this.socket = (0, import_socket.io)(this.config.url, {
|
|
192
|
+
autoConnect: true,
|
|
193
|
+
reconnection: false,
|
|
194
|
+
// We'll handle reconnection ourselves
|
|
195
|
+
timeout: this.config.timeout,
|
|
196
|
+
transports: this.config.transports
|
|
197
|
+
});
|
|
198
|
+
this.setupSocketListeners();
|
|
199
|
+
}
|
|
200
|
+
setupSocketListeners() {
|
|
201
|
+
if (!this.socket) return;
|
|
202
|
+
this.socket.on("connect", () => {
|
|
203
|
+
console.log("Socket connected");
|
|
204
|
+
this.connectionManager.setState("connected" /* CONNECTED */);
|
|
205
|
+
this.connectionManager.resetReconnectAttempts();
|
|
206
|
+
this.eventEmitter.emit("connect");
|
|
207
|
+
});
|
|
208
|
+
this.socket.on("disconnect", (reason) => {
|
|
209
|
+
console.log("Socket disconnected:", reason);
|
|
210
|
+
this.connectionManager.setState("disconnected" /* DISCONNECTED */);
|
|
211
|
+
this.userId = null;
|
|
212
|
+
this.eventEmitter.emit("disconnect", reason);
|
|
213
|
+
if (this.config.reconnection && this.shouldReconnect(reason)) {
|
|
214
|
+
this.scheduleReconnect();
|
|
215
|
+
}
|
|
216
|
+
});
|
|
217
|
+
this.socket.on("connect_error", (error) => {
|
|
218
|
+
console.error("Connection error:", error);
|
|
219
|
+
this.connectionManager.setState("error" /* ERROR */);
|
|
220
|
+
this.eventEmitter.emit("error", error);
|
|
221
|
+
if (this.config.reconnection) {
|
|
222
|
+
this.scheduleReconnect();
|
|
223
|
+
}
|
|
224
|
+
});
|
|
225
|
+
this.socket.on(
|
|
226
|
+
import_sockr_shared.SocketEvent.AUTHENTICATED,
|
|
227
|
+
(data) => {
|
|
228
|
+
console.log("Authenticated:", data.userId);
|
|
229
|
+
this.userId = data.userId;
|
|
230
|
+
this.connectionManager.setState("authenticated" /* AUTHENTICATED */);
|
|
231
|
+
this.eventEmitter.emit("authenticated", data);
|
|
232
|
+
}
|
|
233
|
+
);
|
|
234
|
+
this.socket.on(
|
|
235
|
+
import_sockr_shared.SocketEvent.AUTH_ERROR,
|
|
236
|
+
(data) => {
|
|
237
|
+
console.error("Authentication error:", data.message);
|
|
238
|
+
this.eventEmitter.emit("auth_error", data);
|
|
239
|
+
}
|
|
240
|
+
);
|
|
241
|
+
this.socket.on(
|
|
242
|
+
import_sockr_shared.SocketEvent.USER_ONLINE,
|
|
243
|
+
(data) => {
|
|
244
|
+
this.eventEmitter.emit("user_online", data);
|
|
245
|
+
}
|
|
246
|
+
);
|
|
247
|
+
this.socket.on(
|
|
248
|
+
import_sockr_shared.SocketEvent.USER_OFFLINE,
|
|
249
|
+
(data) => {
|
|
250
|
+
this.eventEmitter.emit("user_offline", data);
|
|
251
|
+
}
|
|
252
|
+
);
|
|
253
|
+
this.socket.on(
|
|
254
|
+
import_sockr_shared.SocketEvent.ONLINE_STATUS,
|
|
255
|
+
(data) => {
|
|
256
|
+
this.eventEmitter.emit("online_status", data);
|
|
257
|
+
}
|
|
258
|
+
);
|
|
259
|
+
this.socket.on(
|
|
260
|
+
import_sockr_shared.SocketEvent.RECEIVE_MESSAGE,
|
|
261
|
+
(data) => {
|
|
262
|
+
this.eventEmitter.emit("message", data);
|
|
263
|
+
}
|
|
264
|
+
);
|
|
265
|
+
this.socket.on(
|
|
266
|
+
import_sockr_shared.SocketEvent.MESSAGE_DELIVERED,
|
|
267
|
+
(data) => {
|
|
268
|
+
this.eventEmitter.emit("message_delivered", data);
|
|
269
|
+
}
|
|
270
|
+
);
|
|
271
|
+
this.socket.on(
|
|
272
|
+
import_sockr_shared.SocketEvent.MESSAGE_ERROR,
|
|
273
|
+
(data) => {
|
|
274
|
+
this.eventEmitter.emit("message_error", data);
|
|
275
|
+
}
|
|
276
|
+
);
|
|
277
|
+
this.socket.on(
|
|
278
|
+
import_sockr_shared.SocketEvent.TYPING_START,
|
|
279
|
+
(data) => {
|
|
280
|
+
this.eventEmitter.emit("typing_start", data);
|
|
281
|
+
}
|
|
282
|
+
);
|
|
283
|
+
this.socket.on(
|
|
284
|
+
import_sockr_shared.SocketEvent.TYPING_STOP,
|
|
285
|
+
(data) => {
|
|
286
|
+
this.eventEmitter.emit("typing_stop", data);
|
|
287
|
+
}
|
|
288
|
+
);
|
|
289
|
+
}
|
|
290
|
+
shouldReconnect(reason) {
|
|
291
|
+
return reason !== "io client disconnect" && this.connectionManager.canReconnect();
|
|
292
|
+
}
|
|
293
|
+
scheduleReconnect() {
|
|
294
|
+
if (this.reconnectTimer) {
|
|
295
|
+
clearTimeout(this.reconnectTimer);
|
|
296
|
+
}
|
|
297
|
+
const attempt = this.connectionManager.incrementReconnectAttempts();
|
|
298
|
+
const delay = this.config.reconnectionDelay * attempt;
|
|
299
|
+
console.log(`Scheduling reconnect attempt ${attempt} in ${delay}ms`);
|
|
300
|
+
this.connectionManager.setState("reconnecting" /* RECONNECTING */);
|
|
301
|
+
this.reconnectTimer = setTimeout(() => {
|
|
302
|
+
console.log(`Reconnect attempt ${attempt}`);
|
|
303
|
+
this.connect();
|
|
304
|
+
}, delay);
|
|
305
|
+
}
|
|
306
|
+
authenticate(token) {
|
|
307
|
+
if (!this.socket?.connected) {
|
|
308
|
+
throw new Error("Socket not connected. Call connect() first.");
|
|
309
|
+
}
|
|
310
|
+
this.socket.emit(import_sockr_shared.SocketEvent.AUTHENTICATE, {
|
|
311
|
+
token
|
|
312
|
+
});
|
|
313
|
+
}
|
|
314
|
+
sendMessage(to, content, metadata) {
|
|
315
|
+
if (!this.isAuthenticated()) {
|
|
316
|
+
throw new Error("Not authenticated. Call authenticate() first.");
|
|
317
|
+
}
|
|
318
|
+
this.socket.emit(import_sockr_shared.SocketEvent.SEND_MESSAGE, {
|
|
319
|
+
to,
|
|
320
|
+
content,
|
|
321
|
+
metadata
|
|
322
|
+
});
|
|
323
|
+
}
|
|
324
|
+
getOnlineStatus(userIds) {
|
|
325
|
+
if (!this.isConnected()) {
|
|
326
|
+
throw new Error("Not connected.");
|
|
327
|
+
}
|
|
328
|
+
this.socket.emit(import_sockr_shared.SocketEvent.GET_ONLINE_STATUS, {
|
|
329
|
+
userIds
|
|
330
|
+
});
|
|
331
|
+
}
|
|
332
|
+
startTyping(to) {
|
|
333
|
+
if (!this.isAuthenticated()) return;
|
|
334
|
+
this.socket.emit(import_sockr_shared.SocketEvent.TYPING_START, { to });
|
|
335
|
+
}
|
|
336
|
+
stopTyping(to) {
|
|
337
|
+
if (!this.isAuthenticated()) return;
|
|
338
|
+
this.socket.emit(import_sockr_shared.SocketEvent.TYPING_STOP, { to });
|
|
339
|
+
}
|
|
340
|
+
on(event, handler) {
|
|
341
|
+
return this.eventEmitter.on(event, handler);
|
|
342
|
+
}
|
|
343
|
+
off(event, handler) {
|
|
344
|
+
this.eventEmitter.off(event, handler);
|
|
345
|
+
}
|
|
346
|
+
disconnect() {
|
|
347
|
+
if (this.reconnectTimer) {
|
|
348
|
+
clearTimeout(this.reconnectTimer);
|
|
349
|
+
this.reconnectTimer = null;
|
|
350
|
+
}
|
|
351
|
+
if (this.socket) {
|
|
352
|
+
this.socket.disconnect();
|
|
353
|
+
this.socket = null;
|
|
354
|
+
}
|
|
355
|
+
this.connectionManager.reset();
|
|
356
|
+
this.userId = null;
|
|
357
|
+
}
|
|
358
|
+
isConnected() {
|
|
359
|
+
return this.connectionManager.isConnected();
|
|
360
|
+
}
|
|
361
|
+
isAuthenticated() {
|
|
362
|
+
return this.connectionManager.isAuthenticated();
|
|
363
|
+
}
|
|
364
|
+
getConnectionState() {
|
|
365
|
+
return this.connectionManager.getState();
|
|
366
|
+
}
|
|
367
|
+
getUserId() {
|
|
368
|
+
return this.userId;
|
|
369
|
+
}
|
|
370
|
+
onStateChange(listener) {
|
|
371
|
+
return this.connectionManager.onStateChange(listener);
|
|
372
|
+
}
|
|
373
|
+
};
|
|
374
|
+
|
|
375
|
+
// src/react/context/SocketContext.tsx
|
|
376
|
+
var import_react = __toESM(require("react"));
|
|
377
|
+
var SocketContext = (0, import_react.createContext)(void 0);
|
|
378
|
+
var SocketProvider = ({
|
|
379
|
+
config,
|
|
380
|
+
token,
|
|
381
|
+
children
|
|
382
|
+
}) => {
|
|
383
|
+
const clientRef = (0, import_react.useRef)(null);
|
|
384
|
+
const [isConnected, setIsConnected] = (0, import_react.useState)(false);
|
|
385
|
+
const [isAuthenticated, setIsAuthenticated] = (0, import_react.useState)(false);
|
|
386
|
+
const [connectionState, setConnectionState] = (0, import_react.useState)(
|
|
387
|
+
"disconnected" /* DISCONNECTED */
|
|
388
|
+
);
|
|
389
|
+
const [userId, setUserId] = (0, import_react.useState)(null);
|
|
390
|
+
(0, import_react.useEffect)(() => {
|
|
391
|
+
const client = new SocketClient(config);
|
|
392
|
+
clientRef.current = client;
|
|
393
|
+
const unsubscribeConnect = client.on("connect", () => {
|
|
394
|
+
setIsConnected(true);
|
|
395
|
+
if (token) {
|
|
396
|
+
client.authenticate(token);
|
|
397
|
+
}
|
|
398
|
+
});
|
|
399
|
+
const unsubscribeDisconnect = client.on("disconnect", () => {
|
|
400
|
+
setIsConnected(false);
|
|
401
|
+
setIsAuthenticated(false);
|
|
402
|
+
setUserId(null);
|
|
403
|
+
});
|
|
404
|
+
const unsubscribeAuthenticated = client.on("authenticated", (data) => {
|
|
405
|
+
setIsAuthenticated(true);
|
|
406
|
+
setUserId(data.userId);
|
|
407
|
+
});
|
|
408
|
+
const unsubscribeAuthError = client.on("auth_error", () => {
|
|
409
|
+
setIsAuthenticated(false);
|
|
410
|
+
setUserId(null);
|
|
411
|
+
});
|
|
412
|
+
const unsubscribeStateChange = client.onStateChange((state) => {
|
|
413
|
+
setConnectionState(state);
|
|
414
|
+
});
|
|
415
|
+
return () => {
|
|
416
|
+
unsubscribeConnect();
|
|
417
|
+
unsubscribeDisconnect();
|
|
418
|
+
unsubscribeAuthenticated();
|
|
419
|
+
unsubscribeAuthError();
|
|
420
|
+
unsubscribeStateChange();
|
|
421
|
+
client.disconnect();
|
|
422
|
+
};
|
|
423
|
+
}, [config.url, token]);
|
|
424
|
+
const value = {
|
|
425
|
+
client: clientRef.current,
|
|
426
|
+
isConnected,
|
|
427
|
+
isAuthenticated,
|
|
428
|
+
connectionState,
|
|
429
|
+
userId
|
|
430
|
+
};
|
|
431
|
+
return /* @__PURE__ */ import_react.default.createElement(SocketContext.Provider, { value }, children);
|
|
432
|
+
};
|
|
433
|
+
var useSocketContext = () => {
|
|
434
|
+
const context = (0, import_react.useContext)(SocketContext);
|
|
435
|
+
if (!context) {
|
|
436
|
+
throw new Error("useSocketContext must be used within SocketProvider");
|
|
437
|
+
}
|
|
438
|
+
return context;
|
|
439
|
+
};
|
|
440
|
+
|
|
441
|
+
// src/react/hooks/useSocket.ts
|
|
442
|
+
var useSocket = () => {
|
|
443
|
+
const context = useSocketContext();
|
|
444
|
+
return {
|
|
445
|
+
client: context.client,
|
|
446
|
+
isConnected: context.isConnected,
|
|
447
|
+
isAuthenticated: context.isAuthenticated,
|
|
448
|
+
connectionState: context.connectionState,
|
|
449
|
+
userId: context.userId
|
|
450
|
+
};
|
|
451
|
+
};
|
|
452
|
+
|
|
453
|
+
// src/react/hooks/useSocketEvent.ts
|
|
454
|
+
var import_react2 = require("react");
|
|
455
|
+
var useSocketEvent = (event, handler, deps = []) => {
|
|
456
|
+
const { client } = useSocketContext();
|
|
457
|
+
(0, import_react2.useEffect)(() => {
|
|
458
|
+
if (!client) return;
|
|
459
|
+
const unsubscribe = client.on(event, handler);
|
|
460
|
+
return () => {
|
|
461
|
+
unsubscribe();
|
|
462
|
+
};
|
|
463
|
+
}, [client, event, ...deps]);
|
|
464
|
+
};
|
|
465
|
+
|
|
466
|
+
// src/react/hooks/useMessages.ts
|
|
467
|
+
var import_react3 = require("react");
|
|
468
|
+
var useMessages = () => {
|
|
469
|
+
const [messages, setMessages] = (0, import_react3.useState)([]);
|
|
470
|
+
const addMessage = (0, import_react3.useCallback)((message) => {
|
|
471
|
+
setMessages((prev) => [...prev, message]);
|
|
472
|
+
}, []);
|
|
473
|
+
const clearMessages = (0, import_react3.useCallback)(() => {
|
|
474
|
+
setMessages([]);
|
|
475
|
+
}, []);
|
|
476
|
+
useSocketEvent(
|
|
477
|
+
"message",
|
|
478
|
+
(data) => {
|
|
479
|
+
addMessage({
|
|
480
|
+
id: data.messageId,
|
|
481
|
+
from: data.from,
|
|
482
|
+
content: data.content,
|
|
483
|
+
timestamp: data.timestamp,
|
|
484
|
+
metadata: data.metadata
|
|
485
|
+
});
|
|
486
|
+
}
|
|
487
|
+
);
|
|
488
|
+
return {
|
|
489
|
+
messages,
|
|
490
|
+
addMessage,
|
|
491
|
+
clearMessages
|
|
492
|
+
};
|
|
493
|
+
};
|
|
494
|
+
|
|
495
|
+
// src/react/hooks/useSendMessage.ts
|
|
496
|
+
var import_react4 = require("react");
|
|
497
|
+
var useSendMessage = () => {
|
|
498
|
+
const { client, isAuthenticated } = useSocket();
|
|
499
|
+
const [isSending, setIsSending] = (0, import_react4.useState)(false);
|
|
500
|
+
const [error, setError] = (0, import_react4.useState)(null);
|
|
501
|
+
const [pendingMessages, setPendingMessages] = (0, import_react4.useState)(/* @__PURE__ */ new Set());
|
|
502
|
+
const sendMessage = (0, import_react4.useCallback)(
|
|
503
|
+
(to, content, metadata) => {
|
|
504
|
+
if (!client || !isAuthenticated) {
|
|
505
|
+
setError("Not authenticated");
|
|
506
|
+
return;
|
|
507
|
+
}
|
|
508
|
+
setIsSending(true);
|
|
509
|
+
setError(null);
|
|
510
|
+
try {
|
|
511
|
+
client.sendMessage(to, content, metadata);
|
|
512
|
+
} catch (err) {
|
|
513
|
+
setError(err instanceof Error ? err.message : "Failed to send message");
|
|
514
|
+
setIsSending(false);
|
|
515
|
+
}
|
|
516
|
+
},
|
|
517
|
+
[client, isAuthenticated]
|
|
518
|
+
);
|
|
519
|
+
useSocketEvent(
|
|
520
|
+
"message_delivered",
|
|
521
|
+
(data) => {
|
|
522
|
+
setIsSending(false);
|
|
523
|
+
setError(null);
|
|
524
|
+
}
|
|
525
|
+
);
|
|
526
|
+
useSocketEvent(
|
|
527
|
+
"message_error",
|
|
528
|
+
(data) => {
|
|
529
|
+
setIsSending(false);
|
|
530
|
+
setError(data.error);
|
|
531
|
+
}
|
|
532
|
+
);
|
|
533
|
+
return {
|
|
534
|
+
sendMessage,
|
|
535
|
+
isSending,
|
|
536
|
+
error
|
|
537
|
+
};
|
|
538
|
+
};
|
|
539
|
+
|
|
540
|
+
// src/react/hooks/usePresence.ts
|
|
541
|
+
var import_react5 = require("react");
|
|
542
|
+
var usePresence = () => {
|
|
543
|
+
const { client } = useSocket();
|
|
544
|
+
const [onlineUsers, setOnlineUsers] = (0, import_react5.useState)(/* @__PURE__ */ new Set());
|
|
545
|
+
const isUserOnline = (0, import_react5.useCallback)(
|
|
546
|
+
(userId) => {
|
|
547
|
+
return onlineUsers.has(userId);
|
|
548
|
+
},
|
|
549
|
+
[onlineUsers]
|
|
550
|
+
);
|
|
551
|
+
const checkOnlineStatus = (0, import_react5.useCallback)(
|
|
552
|
+
(userIds) => {
|
|
553
|
+
if (!client) return;
|
|
554
|
+
client.getOnlineStatus(userIds);
|
|
555
|
+
},
|
|
556
|
+
[client]
|
|
557
|
+
);
|
|
558
|
+
useSocketEvent(
|
|
559
|
+
"user_online",
|
|
560
|
+
(data) => {
|
|
561
|
+
setOnlineUsers((prev) => new Set(prev).add(data.userId));
|
|
562
|
+
}
|
|
563
|
+
);
|
|
564
|
+
useSocketEvent(
|
|
565
|
+
"user_offline",
|
|
566
|
+
(data) => {
|
|
567
|
+
setOnlineUsers((prev) => {
|
|
568
|
+
const next = new Set(prev);
|
|
569
|
+
next.delete(data.userId);
|
|
570
|
+
return next;
|
|
571
|
+
});
|
|
572
|
+
}
|
|
573
|
+
);
|
|
574
|
+
useSocketEvent(
|
|
575
|
+
"online_status",
|
|
576
|
+
(data) => {
|
|
577
|
+
setOnlineUsers((prev) => {
|
|
578
|
+
const next = new Set(prev);
|
|
579
|
+
Object.entries(data.statuses).forEach(([userId, isOnline]) => {
|
|
580
|
+
if (isOnline) {
|
|
581
|
+
next.add(userId);
|
|
582
|
+
} else {
|
|
583
|
+
next.delete(userId);
|
|
584
|
+
}
|
|
585
|
+
});
|
|
586
|
+
return next;
|
|
587
|
+
});
|
|
588
|
+
}
|
|
589
|
+
);
|
|
590
|
+
return {
|
|
591
|
+
onlineUsers,
|
|
592
|
+
isUserOnline,
|
|
593
|
+
checkOnlineStatus
|
|
594
|
+
};
|
|
595
|
+
};
|
|
596
|
+
|
|
597
|
+
// src/react/hooks/useTypingIndicator.ts
|
|
598
|
+
var import_react6 = require("react");
|
|
599
|
+
var useTypingIndicator = (typingTimeout = 3e3) => {
|
|
600
|
+
const { client } = useSocket();
|
|
601
|
+
const [usersTyping, setUsersTyping] = (0, import_react6.useState)(/* @__PURE__ */ new Set());
|
|
602
|
+
const timeoutsRef = (0, import_react6.useRef)(/* @__PURE__ */ new Map());
|
|
603
|
+
const startTyping = (0, import_react6.useCallback)(
|
|
604
|
+
(to) => {
|
|
605
|
+
if (!client) return;
|
|
606
|
+
client.startTyping(to);
|
|
607
|
+
},
|
|
608
|
+
[client]
|
|
609
|
+
);
|
|
610
|
+
const stopTyping = (0, import_react6.useCallback)(
|
|
611
|
+
(to) => {
|
|
612
|
+
if (!client) return;
|
|
613
|
+
client.stopTyping(to);
|
|
614
|
+
},
|
|
615
|
+
[client]
|
|
616
|
+
);
|
|
617
|
+
const clearTypingTimeout = (0, import_react6.useCallback)((userId) => {
|
|
618
|
+
const timeout = timeoutsRef.current.get(userId);
|
|
619
|
+
if (timeout) {
|
|
620
|
+
clearTimeout(timeout);
|
|
621
|
+
timeoutsRef.current.delete(userId);
|
|
622
|
+
}
|
|
623
|
+
}, []);
|
|
624
|
+
useSocketEvent(
|
|
625
|
+
"typing_start",
|
|
626
|
+
(data) => {
|
|
627
|
+
setUsersTyping((prev) => new Set(prev).add(data.from));
|
|
628
|
+
clearTypingTimeout(data.from);
|
|
629
|
+
const timeout = setTimeout(() => {
|
|
630
|
+
setUsersTyping((prev) => {
|
|
631
|
+
const next = new Set(prev);
|
|
632
|
+
next.delete(data.from);
|
|
633
|
+
return next;
|
|
634
|
+
});
|
|
635
|
+
timeoutsRef.current.delete(data.from);
|
|
636
|
+
}, typingTimeout);
|
|
637
|
+
timeoutsRef.current.set(data.from, timeout);
|
|
638
|
+
}
|
|
639
|
+
);
|
|
640
|
+
useSocketEvent(
|
|
641
|
+
"typing_stop",
|
|
642
|
+
(data) => {
|
|
643
|
+
clearTypingTimeout(data.from);
|
|
644
|
+
setUsersTyping((prev) => {
|
|
645
|
+
const next = new Set(prev);
|
|
646
|
+
next.delete(data.from);
|
|
647
|
+
return next;
|
|
648
|
+
});
|
|
649
|
+
}
|
|
650
|
+
);
|
|
651
|
+
(0, import_react6.useEffect)(() => {
|
|
652
|
+
return () => {
|
|
653
|
+
timeoutsRef.current.forEach((timeout) => clearTimeout(timeout));
|
|
654
|
+
timeoutsRef.current.clear();
|
|
655
|
+
};
|
|
656
|
+
}, []);
|
|
657
|
+
return {
|
|
658
|
+
startTyping,
|
|
659
|
+
stopTyping,
|
|
660
|
+
usersTyping
|
|
661
|
+
};
|
|
662
|
+
};
|
|
663
|
+
|
|
664
|
+
// src/index.ts
|
|
665
|
+
__reExport(index_exports, require("sockr-shared"), module.exports);
|
|
666
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
667
|
+
0 && (module.exports = {
|
|
668
|
+
ConnectionManager,
|
|
669
|
+
ConnectionState,
|
|
670
|
+
EventEmitter,
|
|
671
|
+
SocketClient,
|
|
672
|
+
SocketProvider,
|
|
673
|
+
useMessages,
|
|
674
|
+
usePresence,
|
|
675
|
+
useSendMessage,
|
|
676
|
+
useSocket,
|
|
677
|
+
useSocketContext,
|
|
678
|
+
useSocketEvent,
|
|
679
|
+
useTypingIndicator,
|
|
680
|
+
...require("sockr-shared")
|
|
681
|
+
});
|
|
682
|
+
//# sourceMappingURL=index.js.map
|