mirror of
https://github.com/pierre42100/ComunicWeb
synced 2025-06-19 04:15:17 +00:00
Automatically reoopen conversations on page reload
This commit is contained in:
45
assets/js/common/customEvents.js
Normal file
45
assets/js/common/customEvents.js
Normal file
@ -0,0 +1,45 @@
|
||||
/**
|
||||
* Base class for events on custom class management
|
||||
*
|
||||
* @author Pierre Hubert
|
||||
*/
|
||||
|
||||
class CustomEvents {
|
||||
|
||||
constructor() {
|
||||
|
||||
/**
|
||||
* @type {Map<string, Array<(e) => any>>}
|
||||
*/
|
||||
this.evts = new Map();
|
||||
}
|
||||
|
||||
/**
|
||||
* Register to an event
|
||||
*
|
||||
* @param {string} evt The name of the event to register to
|
||||
* @param {(e) => any} cb Callback function
|
||||
*/
|
||||
on(evt, cb) {
|
||||
if(!this.evts.has(evt))
|
||||
this.evts.set(evt, []);
|
||||
|
||||
this.evts.get(evt).push(cb)
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Propagate a new event
|
||||
*
|
||||
* @param {string} evt The name of the event
|
||||
* @param {any} data Data associated with the event
|
||||
*/
|
||||
emitEvent(evt, data) {
|
||||
if(!this.evts.has(evt))
|
||||
this.evts.set(evt, []);
|
||||
|
||||
this.evts.get(evt).forEach((e) => {
|
||||
e(data)
|
||||
})
|
||||
}
|
||||
}
|
@ -42,7 +42,10 @@ class UserWebSocket {
|
||||
this.ws = new WebSocket(url);
|
||||
|
||||
// Wait for connection
|
||||
this.ws.addEventListener("open", () => console.log("Connected to websocket!"))
|
||||
this.ws.addEventListener("open", () => {
|
||||
console.info("Connected to websocket!");
|
||||
SendEvent("wsOpen")
|
||||
})
|
||||
this.ws.addEventListener("error", (e) => this.Error(e))
|
||||
this.ws.addEventListener("close", (e) => this.Closed(e));
|
||||
|
||||
|
Reference in New Issue
Block a user