65 lines
1.4 KiB
TypeScript
65 lines
1.4 KiB
TypeScript
import * as vscode from 'vscode';
|
|
import { getVisitorCount } from './http';
|
|
import { initStatusBar, refresh } from './statusBar';
|
|
|
|
const OPEN = 4;
|
|
const CLOSE = 23;
|
|
const INTERVAL_MS = 2 * 60 * 1000;
|
|
|
|
let timer: NodeJS.Timeout | undefined;
|
|
|
|
export async function activate(context: vscode.ExtensionContext) {
|
|
const subscriptions = context.subscriptions;
|
|
const commandId = 'vsc-adda.refresh';
|
|
|
|
const item = initStatusBar(getVisitorCount, commandId);
|
|
subscriptions.push(item);
|
|
|
|
subscriptions.push(
|
|
vscode.commands.registerCommand(commandId, async () => {
|
|
await refresh();
|
|
})
|
|
);
|
|
|
|
await refresh();
|
|
scheduleNext();
|
|
|
|
subscriptions.push({ dispose: () => timer && clearTimeout(timer) });
|
|
}
|
|
|
|
export function deactivate() {
|
|
if (timer) {
|
|
clearTimeout(timer);
|
|
}
|
|
}
|
|
|
|
function isOpen(d: Date = new Date()): boolean {
|
|
return d.getHours() >= OPEN && d.getHours() < CLOSE;
|
|
}
|
|
|
|
function timeUntilOpenMs(from: Date = new Date()): number {
|
|
const next = new Date(from);
|
|
if (from.getHours() < OPEN) {
|
|
next.setHours(OPEN, 0, 0, 0);
|
|
} else {
|
|
next.setDate(next.getDate() + 1);
|
|
next.setHours(OPEN, 0, 0, 0);
|
|
}
|
|
return next.getTime() - from.getTime();
|
|
}
|
|
|
|
function scheduleNext(): void {
|
|
if (timer) {
|
|
clearTimeout(timer);
|
|
}
|
|
|
|
if (isOpen()) {
|
|
timer = setTimeout(() => {
|
|
refresh().finally(scheduleNext);
|
|
}, INTERVAL_MS);
|
|
} else {
|
|
timer = setTimeout(() => {
|
|
refresh().finally(scheduleNext);
|
|
}, timeUntilOpenMs());
|
|
}
|
|
} |