mirror of
https://github.com/itdoginfo/podkop.git
synced 2025-12-08 20:46:50 +03:00
feat: add pause for log watcher when tab not visible
This commit is contained in:
@@ -15,8 +15,16 @@ export class PodkopLogWatcher {
|
|||||||
private lastLines = new Set<string>();
|
private lastLines = new Set<string>();
|
||||||
private timer?: ReturnType<typeof setInterval>;
|
private timer?: ReturnType<typeof setInterval>;
|
||||||
private running = false;
|
private running = false;
|
||||||
|
private paused = false;
|
||||||
|
|
||||||
private constructor() {}
|
private constructor() {
|
||||||
|
if (typeof document !== 'undefined') {
|
||||||
|
document.addEventListener('visibilitychange', () => {
|
||||||
|
if (document.hidden) this.pause();
|
||||||
|
else this.resume();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
static getInstance(): PodkopLogWatcher {
|
static getInstance(): PodkopLogWatcher {
|
||||||
if (!PodkopLogWatcher.instance) {
|
if (!PodkopLogWatcher.instance) {
|
||||||
@@ -29,6 +37,10 @@ export class PodkopLogWatcher {
|
|||||||
this.fetcher = fetcher;
|
this.fetcher = fetcher;
|
||||||
this.onNewLog = options?.onNewLog;
|
this.onNewLog = options?.onNewLog;
|
||||||
this.intervalMs = options?.intervalMs ?? 5000;
|
this.intervalMs = options?.intervalMs ?? 5000;
|
||||||
|
logger.info(
|
||||||
|
'[PodkopLogWatcher]',
|
||||||
|
`initialized (interval: ${this.intervalMs}ms)`,
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
async checkOnce(): Promise<void> {
|
async checkOnce(): Promise<void> {
|
||||||
@@ -37,6 +49,11 @@ export class PodkopLogWatcher {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (this.paused) {
|
||||||
|
logger.debug('[PodkopLogWatcher]', 'skipped check — tab not visible');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const raw = await this.fetcher();
|
const raw = await this.fetcher();
|
||||||
const lines = raw.split('\n').filter(Boolean);
|
const lines = raw.split('\n').filter(Boolean);
|
||||||
@@ -44,10 +61,7 @@ export class PodkopLogWatcher {
|
|||||||
for (const line of lines) {
|
for (const line of lines) {
|
||||||
if (!this.lastLines.has(line)) {
|
if (!this.lastLines.has(line)) {
|
||||||
this.lastLines.add(line);
|
this.lastLines.add(line);
|
||||||
|
this.onNewLog?.(line);
|
||||||
if (this.onNewLog) {
|
|
||||||
this.onNewLog(line);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -63,14 +77,15 @@ export class PodkopLogWatcher {
|
|||||||
start(): void {
|
start(): void {
|
||||||
if (this.running) return;
|
if (this.running) return;
|
||||||
if (!this.fetcher) {
|
if (!this.fetcher) {
|
||||||
logger.warn('[PodkopLogWatcher]', 'Try to start without fetcher.');
|
logger.warn('[PodkopLogWatcher]', 'attempted to start without fetcher');
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
this.running = true;
|
this.running = true;
|
||||||
this.timer = setInterval(() => this.checkOnce(), this.intervalMs);
|
this.timer = setInterval(() => this.checkOnce(), this.intervalMs);
|
||||||
logger.info(
|
logger.info(
|
||||||
`[PodkopLogWatcher]', 'Started with interval ${this.intervalMs} ms`,
|
'[PodkopLogWatcher]',
|
||||||
|
`started (interval: ${this.intervalMs}ms)`,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -78,11 +93,24 @@ export class PodkopLogWatcher {
|
|||||||
if (!this.running) return;
|
if (!this.running) return;
|
||||||
this.running = false;
|
this.running = false;
|
||||||
if (this.timer) clearInterval(this.timer);
|
if (this.timer) clearInterval(this.timer);
|
||||||
logger.info('[PodkopLogWatcher]', 'Stopped');
|
logger.info('[PodkopLogWatcher]', 'stopped');
|
||||||
|
}
|
||||||
|
|
||||||
|
pause(): void {
|
||||||
|
if (!this.running || this.paused) return;
|
||||||
|
this.paused = true;
|
||||||
|
logger.info('[PodkopLogWatcher]', 'paused (tab not visible)');
|
||||||
|
}
|
||||||
|
|
||||||
|
resume(): void {
|
||||||
|
if (!this.running || !this.paused) return;
|
||||||
|
this.paused = false;
|
||||||
|
logger.info('[PodkopLogWatcher]', 'resumed (tab active)');
|
||||||
|
this.checkOnce(); // сразу проверить, не появились ли новые логи
|
||||||
}
|
}
|
||||||
|
|
||||||
reset(): void {
|
reset(): void {
|
||||||
this.lastLines.clear();
|
this.lastLines.clear();
|
||||||
logger.info('[PodkopLogWatcher]', 'logs history was reset');
|
logger.info('[PodkopLogWatcher]', 'log history reset');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1240,6 +1240,13 @@ var PodkopLogWatcher = class _PodkopLogWatcher {
|
|||||||
this.intervalMs = 5e3;
|
this.intervalMs = 5e3;
|
||||||
this.lastLines = /* @__PURE__ */ new Set();
|
this.lastLines = /* @__PURE__ */ new Set();
|
||||||
this.running = false;
|
this.running = false;
|
||||||
|
this.paused = false;
|
||||||
|
if (typeof document !== "undefined") {
|
||||||
|
document.addEventListener("visibilitychange", () => {
|
||||||
|
if (document.hidden) this.pause();
|
||||||
|
else this.resume();
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
static getInstance() {
|
static getInstance() {
|
||||||
if (!_PodkopLogWatcher.instance) {
|
if (!_PodkopLogWatcher.instance) {
|
||||||
@@ -1251,21 +1258,27 @@ var PodkopLogWatcher = class _PodkopLogWatcher {
|
|||||||
this.fetcher = fetcher;
|
this.fetcher = fetcher;
|
||||||
this.onNewLog = options?.onNewLog;
|
this.onNewLog = options?.onNewLog;
|
||||||
this.intervalMs = options?.intervalMs ?? 5e3;
|
this.intervalMs = options?.intervalMs ?? 5e3;
|
||||||
|
logger.info(
|
||||||
|
"[PodkopLogWatcher]",
|
||||||
|
`initialized (interval: ${this.intervalMs}ms)`
|
||||||
|
);
|
||||||
}
|
}
|
||||||
async checkOnce() {
|
async checkOnce() {
|
||||||
if (!this.fetcher) {
|
if (!this.fetcher) {
|
||||||
logger.warn("[PodkopLogWatcher]", "fetcher not found");
|
logger.warn("[PodkopLogWatcher]", "fetcher not found");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
if (this.paused) {
|
||||||
|
logger.debug("[PodkopLogWatcher]", "skipped check \u2014 tab not visible");
|
||||||
|
return;
|
||||||
|
}
|
||||||
try {
|
try {
|
||||||
const raw = await this.fetcher();
|
const raw = await this.fetcher();
|
||||||
const lines = raw.split("\n").filter(Boolean);
|
const lines = raw.split("\n").filter(Boolean);
|
||||||
for (const line of lines) {
|
for (const line of lines) {
|
||||||
if (!this.lastLines.has(line)) {
|
if (!this.lastLines.has(line)) {
|
||||||
this.lastLines.add(line);
|
this.lastLines.add(line);
|
||||||
if (this.onNewLog) {
|
this.onNewLog?.(line);
|
||||||
this.onNewLog(line);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (this.lastLines.size > 500) {
|
if (this.lastLines.size > 500) {
|
||||||
@@ -1279,24 +1292,36 @@ var PodkopLogWatcher = class _PodkopLogWatcher {
|
|||||||
start() {
|
start() {
|
||||||
if (this.running) return;
|
if (this.running) return;
|
||||||
if (!this.fetcher) {
|
if (!this.fetcher) {
|
||||||
logger.warn("[PodkopLogWatcher]", "Try to start without fetcher.");
|
logger.warn("[PodkopLogWatcher]", "attempted to start without fetcher");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
this.running = true;
|
this.running = true;
|
||||||
this.timer = setInterval(() => this.checkOnce(), this.intervalMs);
|
this.timer = setInterval(() => this.checkOnce(), this.intervalMs);
|
||||||
logger.info(
|
logger.info(
|
||||||
`[PodkopLogWatcher]', 'Started with interval ${this.intervalMs} ms`
|
"[PodkopLogWatcher]",
|
||||||
|
`started (interval: ${this.intervalMs}ms)`
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
stop() {
|
stop() {
|
||||||
if (!this.running) return;
|
if (!this.running) return;
|
||||||
this.running = false;
|
this.running = false;
|
||||||
if (this.timer) clearInterval(this.timer);
|
if (this.timer) clearInterval(this.timer);
|
||||||
logger.info("[PodkopLogWatcher]", "Stopped");
|
logger.info("[PodkopLogWatcher]", "stopped");
|
||||||
|
}
|
||||||
|
pause() {
|
||||||
|
if (!this.running || this.paused) return;
|
||||||
|
this.paused = true;
|
||||||
|
logger.info("[PodkopLogWatcher]", "paused (tab not visible)");
|
||||||
|
}
|
||||||
|
resume() {
|
||||||
|
if (!this.running || !this.paused) return;
|
||||||
|
this.paused = false;
|
||||||
|
logger.info("[PodkopLogWatcher]", "resumed (tab active)");
|
||||||
|
this.checkOnce();
|
||||||
}
|
}
|
||||||
reset() {
|
reset() {
|
||||||
this.lastLines.clear();
|
this.lastLines.clear();
|
||||||
logger.info("[PodkopLogWatcher]", "logs history was reset");
|
logger.info("[PodkopLogWatcher]", "log history reset");
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user