mirror of
https://github.com/johannesjo/super-productivity.git
synced 2026-07-31 19:50:44 +00:00
fix: replace setInterval with lazy implementation everywhere #493
This commit is contained in:
parent
eca6af27c2
commit
cd8438db23
4 changed files with 29 additions and 9 deletions
|
|
@ -18,6 +18,7 @@ import { backupData } from './backup';
|
|||
import { JiraCfg } from '../src/app/features/issue/providers/jira/jira.model';
|
||||
import { KeyboardConfig } from '../src/app/features/config/global-config.model';
|
||||
import lockscreen from './lockscreen';
|
||||
import { lazySetInterval } from '../src/app/util/lazy-set-interval';
|
||||
|
||||
const ICONS_FOLDER = __dirname + '/assets/icons/';
|
||||
const IS_MAC = process.platform === 'darwin';
|
||||
|
|
@ -130,7 +131,7 @@ appIN.on('ready', () => {
|
|||
const checkIdle = () => sendIdleMsgIfOverMin(powerMonitor.getSystemIdleTime() * 1000);
|
||||
|
||||
// init time tracking interval
|
||||
setInterval(checkIdle, CONFIG.IDLE_PING_INTERVAL);
|
||||
lazySetInterval(checkIdle, CONFIG.IDLE_PING_INTERVAL);
|
||||
|
||||
powerMonitor.on('suspend', () => {
|
||||
isLocked = true;
|
||||
|
|
|
|||
|
|
@ -1,9 +1,10 @@
|
|||
/// <reference lib="webworker" />
|
||||
|
||||
import { ReminderCopy } from './reminder.model';
|
||||
import { lazySetInterval } from '../../util/lazy-set-interval';
|
||||
|
||||
const CHECK_INTERVAL_DURATION = 10000;
|
||||
let checkInterval: any;
|
||||
let cancelCheckInterval: (() => void) | undefined;
|
||||
|
||||
addEventListener('message', ({data}) => {
|
||||
// console.log('REMINDER WORKER', data);
|
||||
|
|
@ -11,14 +12,15 @@ addEventListener('message', ({data}) => {
|
|||
});
|
||||
|
||||
const reInitCheckInterval = (reminders: ReminderCopy[]) => {
|
||||
if (checkInterval as any) {
|
||||
clearInterval(checkInterval as any);
|
||||
if (cancelCheckInterval) {
|
||||
cancelCheckInterval();
|
||||
cancelCheckInterval = undefined;
|
||||
}
|
||||
if (!reminders || !reminders.length) {
|
||||
return;
|
||||
}
|
||||
|
||||
checkInterval = setInterval(() => {
|
||||
cancelCheckInterval = lazySetInterval(() => {
|
||||
const dueReminders = getDueReminders(reminders);
|
||||
if (dueReminders.length) {
|
||||
const oldest = dueReminders[0];
|
||||
|
|
|
|||
|
|
@ -14,6 +14,7 @@ import { ElectronService } from '../../core/electron/electron.service';
|
|||
import { UiHelperService } from '../ui-helper/ui-helper.service';
|
||||
import { WorkContextService } from '../work-context/work-context.service';
|
||||
import { ipcRenderer } from 'electron';
|
||||
import { lazySetInterval } from '../../util/lazy-set-interval';
|
||||
|
||||
const DEFAULT_MIN_IDLE_TIME = 60000;
|
||||
const IDLE_POLL_INTERVAL = 1000;
|
||||
|
|
@ -36,7 +37,7 @@ export class IdleService {
|
|||
|
||||
private lastCurrentTaskId?: string | null;
|
||||
private isIdleDialogOpen: boolean = false;
|
||||
private idlePollInterval?: number;
|
||||
private clearIdlePollInterval?: () => void;
|
||||
|
||||
constructor(
|
||||
private _chromeExtensionInterfaceService: ChromeExtensionInterfaceService,
|
||||
|
|
@ -146,15 +147,17 @@ export class IdleService {
|
|||
initIdlePoll(initialIdleTime: number) {
|
||||
const idleStart = Date.now();
|
||||
this._idleTime$.next(initialIdleTime);
|
||||
this.idlePollInterval = window.setInterval(() => {
|
||||
|
||||
this.clearIdlePollInterval = lazySetInterval(() => {
|
||||
const delta = Date.now() - idleStart;
|
||||
this._idleTime$.next(initialIdleTime + delta);
|
||||
}, IDLE_POLL_INTERVAL);
|
||||
}
|
||||
|
||||
cancelIdlePoll() {
|
||||
if (this.idlePollInterval) {
|
||||
window.clearInterval(this.idlePollInterval);
|
||||
if (this.clearIdlePollInterval) {
|
||||
this.clearIdlePollInterval();
|
||||
this.clearIdlePollInterval = undefined;
|
||||
this._idleTime$.next(0);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
14
src/app/util/lazy-set-interval.ts
Normal file
14
src/app/util/lazy-set-interval.ts
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
export const lazySetInterval = (func: () => void, intervalDuration: number): () => void => {
|
||||
let lastTimeoutId;
|
||||
|
||||
const interval = () => {
|
||||
lastTimeoutId = setTimeout(interval, intervalDuration);
|
||||
func.call(null);
|
||||
};
|
||||
|
||||
setTimeout(interval, intervalDuration);
|
||||
|
||||
return () => {
|
||||
clearTimeout(lastTimeoutId);
|
||||
};
|
||||
};
|
||||
Loading…
Add table
Add a link
Reference in a new issue