diff --git a/assets/js/common/ws.js b/assets/js/common/ws.js index 08b22145..bc193dd3 100644 --- a/assets/js/common/ws.js +++ b/assets/js/common/ws.js @@ -136,7 +136,7 @@ class UserWebSocket { static SendRequest(title, data) { // Send request return new Promise((res, err) => { - if(this.ws.readyState != WebSocket.OPEN) + if(!this.hasOwnProperty("ws") || this.ws.readyState != WebSocket.OPEN) throw new Error("WebSocket is not open!"); // Determine unique request ID diff --git a/assets/js/components/posts/interface.js b/assets/js/components/posts/interface.js index 071d9715..8c95377c 100644 --- a/assets/js/components/posts/interface.js +++ b/assets/js/components/posts/interface.js @@ -4,7 +4,7 @@ * @author Pierre HUBERT */ -ComunicWeb.components.posts.interface = { +const PostsInterface = { /** * Get user posts @@ -227,6 +227,49 @@ ComunicWeb.components.posts.interface = { //Perform the request ComunicWeb.common.api.makeAPIrequest(apiURI, params, true, callback); - } + }, -} \ No newline at end of file + _registerCount: {}, + + /** + * Register for post updates + * + * @param {Number} postID Target post ID + */ + register: async function(postID) { + if(!this._registerCount.hasOwnProperty(postID)) { + await ws("$main/register_post", {postID: postID}); + this._registerCount[postID] = 1; + } + else + this._registerCount[postID]++; + }, + + /** + * Unregister of post updates + * + * @param {Number} postID Target post ID + */ + unregister: async function(postID) { + + // Auto unregister all remaining registered posts if websocket is closed + if(!UserWebSocket.IsConnected) + this._registerCount = {} + + if(!this._registerCount.hasOwnProperty(postID)) + return; + + this._registerCount[postID]--; + + if(this._registerCount[postID] == 0) { + await ws("$main/unregister_post", {postID: postID}); + delete this._registerCount[postID]; + } + }, +} + +ComunicWeb.components.posts.interface = PostsInterface; + +document.addEventListener("wsClosed", () => { + PostsInterface._registerCount = {} +}); \ No newline at end of file diff --git a/assets/js/components/posts/ui.js b/assets/js/components/posts/ui.js index 980f26a0..56776bcb 100644 --- a/assets/js/components/posts/ui.js +++ b/assets/js/components/posts/ui.js @@ -12,7 +12,7 @@ ComunicWeb.components.posts.ui = { * @param {Object} infos Informations about the post * @param {HTMLElement} target The target for the post */ - display_post: function(info, target){ + display_post: function(info, target) { //Check if it is required to create a post root element or not if(target.className.includes("post")) @@ -900,6 +900,20 @@ ComunicWeb.components.posts.ui = { //Load comments (if possible) if(info.comments != null) ComunicWeb.components.comments.ui.display(info.comments, info.ID, postRoot); + + // Register for post updates + PostsInterface.register(info.ID); + + // Auto-unregister when the post goes out of scope + const ev = async (e) => { + if(postRoot.isConnected) + return; + + document.removeEventListener("openPage", ev); + + PostsInterface.unregister(info.ID); + } + document.addEventListener("openPage", ev); }, /**