open(), logging, stop() clears timers

pull/835/head
Ryan Tharp 5 years ago
parent eb263a9ee7
commit dc14eb9ca7

@ -34,8 +34,17 @@ class LokiAppDotNetServerAPI {
log.info(`LokiAppDotNetAPI registered server ${url}`); 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() { async close() {
this.channels.forEach(channel => channel.stop()); this.channels.forEach(channel => channel.stop());
// match sure our pending requests are finished
// in case it's still starting up
if (this.tokenPromise) { if (this.tokenPromise) {
await this.tokenPromise; await this.tokenPromise;
} }
@ -70,6 +79,7 @@ class LokiAppDotNetServerAPI {
} }
async partChannel(channelId) { async partChannel(channelId) {
log.info('partChannel', channelId, 'from', this.baseServerUrl);
await this.serverRequest(`channels/${channelId}/subscribe`, { await this.serverRequest(`channels/${channelId}/subscribe`, {
method: 'DELETE', method: 'DELETE',
}); });
@ -78,6 +88,7 @@ class LokiAppDotNetServerAPI {
// deallocate resources channel uses // deallocate resources channel uses
unregisterChannel(channelId) { unregisterChannel(channelId) {
log.info('unregisterChannel', channelId, 'from', this.baseServerUrl);
let thisChannel; let thisChannel;
let i = 0; let i = 0;
for (; i < this.channels.length; i += 1) { for (; i < this.channels.length; i += 1) {
@ -189,6 +200,7 @@ class LokiAppDotNetServerAPI {
if (!token) { if (!token) {
// if we haven't forced it // if we haven't forced it
if (!forceRefresh) { if (!forceRefresh) {
// try one more time with requesting a fresh token
token = await this.getOrRefreshServerToken(true); token = await this.getOrRefreshServerToken(true);
} }
return token; return token;
@ -898,7 +910,6 @@ class LokiPublicChannelAPI {
this.modStatus = false; this.modStatus = false;
this.deleteLastId = 1; this.deleteLastId = 1;
this.timers = {}; this.timers = {};
this.running = true;
this.myPrivateKey = false; this.myPrivateKey = false;
// can escalated to SQL if it start uses too much memory // can escalated to SQL if it start uses too much memory
this.logMop = {}; this.logMop = {};
@ -914,12 +925,7 @@ class LokiPublicChannelAPI {
}` }`
); );
// start polling // start polling
this.pollForMessages(); this.open();
this.pollForDeletions();
this.pollForChannel();
this.pollForModerators();
// TODO: poll for group members here?
} }
async getPrivateKey() { async getPrivateKey() {
@ -948,19 +954,64 @@ class LokiPublicChannelAPI {
return true; 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() { 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; this.running = false;
if (this.timers.channel) { if (this.timers.channel) {
clearTimeout(this.timers.channel); clearTimeout(this.timers.channel);
this.timers.channel = false;
} }
if (this.timers.moderator) { if (this.timers.moderator) {
clearTimeout(this.timers.moderator); clearTimeout(this.timers.moderator);
this.timers.moderator = false;
} }
if (this.timers.delete) { if (this.timers.delete) {
clearTimeout(this.timers.delete); clearTimeout(this.timers.delete);
this.timers.delete = false;
} }
if (this.timers.message) { if (this.timers.message) {
clearTimeout(this.timers.message); clearTimeout(this.timers.message);
this.timers.message = false;
} }
} }

Loading…
Cancel
Save