From dc14eb9ca7f8b544d6e75b457221ab3e975dc294 Mon Sep 17 00:00:00 2001 From: Ryan Tharp Date: Mon, 10 Feb 2020 16:43:30 -0800 Subject: [PATCH] open(), logging, stop() clears timers --- js/modules/loki_app_dot_net_api.js | 65 ++++++++++++++++++++++++++---- 1 file changed, 58 insertions(+), 7 deletions(-) diff --git a/js/modules/loki_app_dot_net_api.js b/js/modules/loki_app_dot_net_api.js index fab433da6..a3aca1d10 100644 --- a/js/modules/loki_app_dot_net_api.js +++ b/js/modules/loki_app_dot_net_api.js @@ -34,8 +34,17 @@ class LokiAppDotNetServerAPI { log.info(`LokiAppDotNetAPI registered server ${url}`); } + async open() { + // check token, we're not sure how long we were asleep, token may have expired + await this.getOrRefreshServerToken(); + // now that we have a working token, start up pollers + this.channels.forEach(channel => channel.open()); + } + async close() { this.channels.forEach(channel => channel.stop()); + // match sure our pending requests are finished + // in case it's still starting up if (this.tokenPromise) { await this.tokenPromise; } @@ -70,6 +79,7 @@ class LokiAppDotNetServerAPI { } async partChannel(channelId) { + log.info('partChannel', channelId, 'from', this.baseServerUrl); await this.serverRequest(`channels/${channelId}/subscribe`, { method: 'DELETE', }); @@ -78,6 +88,7 @@ class LokiAppDotNetServerAPI { // deallocate resources channel uses unregisterChannel(channelId) { + log.info('unregisterChannel', channelId, 'from', this.baseServerUrl); let thisChannel; let i = 0; for (; i < this.channels.length; i += 1) { @@ -189,6 +200,7 @@ class LokiAppDotNetServerAPI { if (!token) { // if we haven't forced it if (!forceRefresh) { + // try one more time with requesting a fresh token token = await this.getOrRefreshServerToken(true); } return token; @@ -898,7 +910,6 @@ class LokiPublicChannelAPI { this.modStatus = false; this.deleteLastId = 1; this.timers = {}; - this.running = true; this.myPrivateKey = false; // can escalated to SQL if it start uses too much memory this.logMop = {}; @@ -914,12 +925,7 @@ class LokiPublicChannelAPI { }` ); // start polling - this.pollForMessages(); - this.pollForDeletions(); - this.pollForChannel(); - this.pollForModerators(); - - // TODO: poll for group members here? + this.open(); } async getPrivateKey() { @@ -948,19 +954,64 @@ class LokiPublicChannelAPI { return true; } + open() { + log.info( + `LokiPublicChannel open ${this.channelId} on ${ + this.serverAPI.baseServerUrl + }` + ) + if (this.running) { + log.warn( + `LokiPublicChannel already open ${this.channelId} on ${ + this.serverAPI.baseServerUrl + }` + ) + } + this.running = true; + if (!this.timers.channel) { + this.pollForChannel(); + } + if (!this.timers.moderator) { + this.pollForModerators(); + } + if (!this.timers.delete) { + this.pollForDeletions(); + } + if (!this.timers.message) { + this.pollForMessages(); + } + // TODO: poll for group members here? + } + stop() { + log.info( + `LokiPublicChannel close ${this.channelId} on ${ + this.serverAPI.baseServerUrl + }` + ) + if (!this.running) { + log.warn( + `LokiPublicChannel already open ${this.channelId} on ${ + this.serverAPI.baseServerUrl + }` + ) + } this.running = false; if (this.timers.channel) { clearTimeout(this.timers.channel); + this.timers.channel = false; } if (this.timers.moderator) { clearTimeout(this.timers.moderator); + this.timers.moderator = false; } if (this.timers.delete) { clearTimeout(this.timers.delete); + this.timers.delete = false; } if (this.timers.message) { clearTimeout(this.timers.message); + this.timers.message = false; } }