chore(turbo-poll): convert to setInterval, turbo morph compatible.
Interval is not exponential anymore, so increase max checks.
This commit is contained in:
parent
4824363879
commit
c93eba38ea
1 changed files with 15 additions and 14 deletions
|
@ -3,8 +3,7 @@ import { httpRequest } from '@utils';
|
||||||
import { ApplicationController } from './application_controller';
|
import { ApplicationController } from './application_controller';
|
||||||
|
|
||||||
const DEFAULT_POLL_INTERVAL = 3000;
|
const DEFAULT_POLL_INTERVAL = 3000;
|
||||||
const DEFAULT_MAX_CHECKS = 10;
|
const DEFAULT_MAX_CHECKS = 20;
|
||||||
const DEFAULT_EXPONENTIAL_FACTOR = 1.2;
|
|
||||||
|
|
||||||
// Periodically check the state of a URL.
|
// Periodically check the state of a URL.
|
||||||
//
|
//
|
||||||
|
@ -27,10 +26,7 @@ export class TurboPollController extends ApplicationController {
|
||||||
#abortController?: AbortController;
|
#abortController?: AbortController;
|
||||||
|
|
||||||
connect(): void {
|
connect(): void {
|
||||||
const state = this.nextState();
|
this.schedule();
|
||||||
if (state) {
|
|
||||||
this.schedule(state);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
disconnect(): void {
|
disconnect(): void {
|
||||||
|
@ -38,7 +34,6 @@ export class TurboPollController extends ApplicationController {
|
||||||
}
|
}
|
||||||
|
|
||||||
refresh() {
|
refresh() {
|
||||||
this.cancel();
|
|
||||||
this.#abortController = new AbortController();
|
this.#abortController = new AbortController();
|
||||||
|
|
||||||
httpRequest(this.urlValue, { signal: this.#abortController.signal })
|
httpRequest(this.urlValue, { signal: this.#abortController.signal })
|
||||||
|
@ -46,15 +41,21 @@ export class TurboPollController extends ApplicationController {
|
||||||
.catch(() => null);
|
.catch(() => null);
|
||||||
}
|
}
|
||||||
|
|
||||||
private schedule(state: PollState): void {
|
private schedule(): void {
|
||||||
this.cancel();
|
this.cancel();
|
||||||
this.#timer = setTimeout(() => {
|
this.#timer = setInterval(() => {
|
||||||
|
const nextState = this.nextState();
|
||||||
|
|
||||||
|
if (!nextState) {
|
||||||
|
this.cancel();
|
||||||
|
} else {
|
||||||
this.refresh();
|
this.refresh();
|
||||||
}, state.interval);
|
}
|
||||||
|
}, this.intervalValue);
|
||||||
}
|
}
|
||||||
|
|
||||||
private cancel(): void {
|
private cancel(): void {
|
||||||
clearTimeout(this.#timer);
|
clearInterval(this.#timer);
|
||||||
this.#abortController?.abort();
|
this.#abortController?.abort();
|
||||||
this.#abortController = window.AbortController
|
this.#abortController = window.AbortController
|
||||||
? new AbortController()
|
? new AbortController()
|
||||||
|
@ -63,10 +64,11 @@ export class TurboPollController extends ApplicationController {
|
||||||
|
|
||||||
private nextState(): PollState | false {
|
private nextState(): PollState | false {
|
||||||
const state = pollers.get(this.urlValue);
|
const state = pollers.get(this.urlValue);
|
||||||
|
|
||||||
if (!state) {
|
if (!state) {
|
||||||
return this.resetState();
|
return this.resetState();
|
||||||
}
|
}
|
||||||
state.interval *= DEFAULT_EXPONENTIAL_FACTOR;
|
|
||||||
state.checks += 1;
|
state.checks += 1;
|
||||||
if (state.checks <= this.maxChecksValue) {
|
if (state.checks <= this.maxChecksValue) {
|
||||||
return state;
|
return state;
|
||||||
|
@ -87,7 +89,6 @@ export class TurboPollController extends ApplicationController {
|
||||||
}
|
}
|
||||||
|
|
||||||
type PollState = {
|
type PollState = {
|
||||||
interval: number;
|
|
||||||
checks: number;
|
checks: number;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue