mirror of
https://github.com/pierre42100/ComunicWeb
synced 2024-11-22 20:19:21 +00:00
Auto-register posts for updates
This commit is contained in:
parent
27d7c526b8
commit
f3cf290822
@ -136,7 +136,7 @@ class UserWebSocket {
|
|||||||
static SendRequest(title, data) {
|
static SendRequest(title, data) {
|
||||||
// Send request
|
// Send request
|
||||||
return new Promise((res, err) => {
|
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!");
|
throw new Error("WebSocket is not open!");
|
||||||
|
|
||||||
// Determine unique request ID
|
// Determine unique request ID
|
||||||
|
@ -4,7 +4,7 @@
|
|||||||
* @author Pierre HUBERT
|
* @author Pierre HUBERT
|
||||||
*/
|
*/
|
||||||
|
|
||||||
ComunicWeb.components.posts.interface = {
|
const PostsInterface = {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get user posts
|
* Get user posts
|
||||||
@ -227,6 +227,49 @@ ComunicWeb.components.posts.interface = {
|
|||||||
//Perform the request
|
//Perform the request
|
||||||
ComunicWeb.common.api.makeAPIrequest(apiURI, params, true, callback);
|
ComunicWeb.common.api.makeAPIrequest(apiURI, params, true, callback);
|
||||||
|
|
||||||
}
|
},
|
||||||
|
|
||||||
}
|
_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 = {}
|
||||||
|
});
|
@ -12,7 +12,7 @@ ComunicWeb.components.posts.ui = {
|
|||||||
* @param {Object} infos Informations about the post
|
* @param {Object} infos Informations about the post
|
||||||
* @param {HTMLElement} target The target for 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
|
//Check if it is required to create a post root element or not
|
||||||
if(target.className.includes("post"))
|
if(target.className.includes("post"))
|
||||||
@ -900,6 +900,20 @@ ComunicWeb.components.posts.ui = {
|
|||||||
//Load comments (if possible)
|
//Load comments (if possible)
|
||||||
if(info.comments != null)
|
if(info.comments != null)
|
||||||
ComunicWeb.components.comments.ui.display(info.comments, info.ID, postRoot);
|
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);
|
||||||
},
|
},
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Loading…
Reference in New Issue
Block a user