switch to polling every monitor interval

nxapi-presence
Rosalina 2024-01-13 02:10:38 -05:00 committed by imspace
parent 21b02fb44d
commit cbe7a5424a
1 changed files with 9 additions and 13 deletions

View File

@ -164,7 +164,7 @@ function progress({ total, currentUrl, done }: StepProgress): Progress {
export class App { export class App {
profile: Profile; profile: Profile;
env: Env; env: Env;
isSplatoon3Active = false; splatoon3PreviouslyActive = false;
constructor(public opts: Opts) { constructor(public opts: Opts) {
const stateBackend = opts.stateBackend ?? const stateBackend = opts.stateBackend ??
@ -401,24 +401,20 @@ export class App {
async monitorWithNxapi() { async monitorWithNxapi() {
this.env.logger.debug("Monitoring with nxapi presence"); this.env.logger.debug("Monitoring with nxapi presence");
await this.exportOnce(); await this.exportOnce();
this.env.logger.debug("Connecting to event stream");
const nxapiEventStream = new EventSource(this.opts.nxapiPresenceUrl!);
nxapiEventStream.addEventListener("title", (event) => {
this.env.logger.debug("Received a new title event", event, event.data)
const eventData = JSON.parse(event.data)
const newStatus = eventData.id === SPLATOON3_TITLE_ID;
if (this.isSplatoon3Active !== newStatus) {
this.env.logger.log("Splatoon 3 presence changed, new status: ", newStatus ? "active" : "inactive");
}
this.isSplatoon3Active = newStatus;
})
while (true) { while (true) {
await this.countDown(this.profile.state.monitorInterval); await this.countDown(this.profile.state.monitorInterval);
if (this.isSplatoon3Active) { const nxapiResponse = await fetch(this.opts.nxapiPresenceUrl!);
const nxapiData = await nxapiResponse.json();
const isSplatoon3Active = nxapiData.title?.id === SPLATOON3_TITLE_ID;
if (isSplatoon3Active || this.splatoon3PreviouslyActive) {
this.env.logger.log("Splatoon 3 is active, exporting data"); this.env.logger.log("Splatoon 3 is active, exporting data");
await this.exportOnce(); await this.exportOnce();
} }
if (isSplatoon3Active !== this.splatoon3PreviouslyActive) {
this.env.logger.debug("Splatoon 3 status has changed from", this.splatoon3PreviouslyActive, "to", isSplatoon3Active)
}
this.splatoon3PreviouslyActive = isSplatoon3Active
} }
} }
async monitor() { async monitor() {