refresh-scheduler
Skill này làm gì
Skill này xây dựng một bộ lập lịch làm mới dữ liệu bảng điều khiển theo khoảng thời gian đặt sẵn. Nó tạm dừng khi tab trình duyệt bị ẩn và làm mới dữ liệu cũ khi người dùng quay lại. Skill dành cho lập trình viên làm bảng điều khiển trực tiếp gọi nhiều nguồn dữ liệu.
Khi nào nên dùng
- ✓Khi bạn muốn dữ liệu bảng điều khiển tự làm mới vài phút một lần mà không cần tải lại thủ công.
- ✓Khi bạn muốn việc làm mới dừng lại lúc tab chạy ở nền.
- ✓Khi nhiều panel gọi dịch vụ bên ngoài và các yêu cầu cần được trải đều.
Liên kết skill
https://github.com/HKUDS/AgentPilot/blob/main/openspace/legacy/v1/showcase/skills/refresh-scheduler/SKILL.mdSao chép rồi dán vào AI của bạn (Claude, ChatGPT hoặc Cursor) và nhờ nó làm theo hướng dẫn.
Cách dùng
Cách dễ nhất: sao chép tin nhắn và dán vào AI của bạn. AI sẽ đọc skill và làm theo hướng dẫn.
Tin nhắn có sẵn để dán
Dành cho Claude Code, Codex hoặc Cursor: chạy lệnh trong thư mục dự án của bạn và skill sẽ được cài đặt.
Tải tệp SKILL.md về và thả vào thư mục skill của agent.
Ví dụ cách nhờ AI
“Bạn có thể làm mới dữ liệu bảng điều khiển của tôi vài phút một lần nhưng dừng khi tab chạy ở nền không?”
5 skill mới mỗi tuần. Đọc chỉ 1 phút.
Cách hoạt động
Bộ lập lịch tạo một bộ đếm thời gian cho mỗi panel. Khi trang bị ẩn, nó tạm dừng mọi bộ đếm, rồi khởi động lại và làm mới dữ liệu cũ khi trang hiển thị trở lại.
Nội dung kỹ thuật (SKILL.md gốc)
Lấy nguyên văn từ GitHub, do tác giả viết bằng tiếng Anh.
Refresh Scheduler Pattern
The refresh scheduler manages periodic data fetching for all panels, pausing when the tab is hidden and flushing stale data when the user returns.
Implementation
Create src/services/refresh-scheduler.ts:
export interface RefreshRegistration {
name: string;
fn: () => Promise<void>;
intervalMs: number;
condition?: () => boolean;
}
interface RunnerEntry {
timer: ReturnType<typeof setInterval> | null;
intervalMs: number;
fn: () => Promise<void>;
condition?: () => boolean;
lastRun: number;
}
export class RefreshScheduler {
private runners = new Map<string, RunnerEntry>();
private inFlight = new Set<string>();
private hiddenSince = 0;
private visibilityHandler: (() => void) | null = null;
constructor() {
this.visibilityHandler = () => this.onVisibilityChange();
document.addEventListener('visibilitychange', this.visibilityHandler);
}
private onVisibilityChange(): void {
if (document.hidden) {
this.hiddenSince = Date.now();
// Pause all timers
for (const entry of this.runners.values()) {
if (entry.timer) {
clearInterval(entry.timer);
entry.timer = null;
}
}
} else {
// Resume and flush stale
this.flushStaleRefreshes();
for (const [name, entry] of this.runners) {
if (!entry.timer) {
entry.timer = setInterval(() => this.runRefresh(name), entry.intervalMs);
}
}
}
}
private async runRefresh(name: string): Promise<void> {
const entry = this.runners.get(name);
if (!entry) return;
if (this.inFlight.has(name)) return;
if (entry.condition && !entry.condition()) return;
this.inFlight.add(name);
try {
await entry.fn();
entry.lastRun = Date.now();
} catch (err) {
console.error(`[Refresh] ${name} failed:`, err);
} finally {
this.inFlight.delete(name);
}
}
scheduleRefresh(
name: string,
fn: () => Promise<void>,
intervalMs: number,
condition?: () => boolean,
): void {
// Clean up existing
const existing = this.runners.get(name);
if (existing?.timer) clearInterval(existing.timer);
const entry: RunnerEntry = {
timer: null,
intervalMs,
fn,
condition,
lastRun: 0,
};
if (!document.hidden) {
entry.timer = setInterval(() => this.runRefresh(name), intervalMs);
}
this.runners.set(name, entry);
}
/**
* After returning from hidden, flush all runners whose data
* is older than their interval. Stagger to avoid API burst.
*/
flushStaleRefreshes(): void {
if (!this.hiddenSince) return;
const hiddenMs = Date.now() - this.hiddenSince;
this.hiddenSince = 0;
let stagger = 0;
for (const [name, entry] of this.runners) {
if (hiddenMs < entry.intervalMs) continue;
const delay = stagger;
stagger += 150; // 150ms between each flush
setTimeout(() => this.runRefresh(name), delay);
}
}
registerAll(registrations: RefreshRegistration[]): void {
for (const reg of registrations) {
this.scheduleRefresh(reg.name, reg.fn, reg.intervalMs, reg.condition);
}
}
/** Manually trigger a specific refresh immediately */
trigger(name: string): void {
this.runRefresh(name);
}
destroy(): void {
if (this.visibilityHandler) {
document.removeEventListener('visibilitychange', this.visibilityHandler);
}
for (const entry of this.runners.values()) {
if (entry.timer) clearInterval(entry.timer);
}
this.runners.clear();
this.inFlight.clear();
}
}
Usage
// src/main.ts
import { RefreshScheduler } from './services/refresh-scheduler';
const scheduler = new RefreshScheduler();
// Register panel refresh intervals
scheduler.registerAll([
{ name: 'stocks', fn: () => stockPanel.refresh(), intervalMs: 60_000 },
{ name: 'news', fn: () => newsPanel.refresh(), intervalMs: 5 * 60_000 },
{ name: 'email', fn: () => emailPanel.refresh(), intervalMs: 2 * 60_000 },
{ name: 'calendar', fn: () => calendarPanel.refresh(), intervalMs: 5 * 60_000 },
]);
Key Behaviors
- Pause when hidden — stops all intervals when tab is not visible
- Flush on return — refreshes stale panels with 150ms stagger to avoid API burst
- In-flight guard — prevents duplicate requests for the same panel
- Configurable intervals — each panel has its own refresh rate
- **Condi
Câu hỏi thường gặp
Mọi panel có làm mới cùng một lúc không?
Không. Bộ lập lịch lệch thời điểm làm mới để nhiều yêu cầu không bị gửi cùng một lúc.
Điều gì xảy ra nếu một lần làm mới vẫn đang chạy?
Bộ lập lịch theo dõi các yêu cầu đang chạy, nên sẽ không khởi động cùng một làm mới hai lần.
Mỗi panel có thể có khoảng thời gian riêng không?
Có. Mỗi lần đăng ký đặt khoảng thời gian làm mới riêng.
Chi tiết
Mô tả gốc của tác giả
Manage timed data refresh intervals for all dashboard panels. Handles pause-when-hidden, stale-data flush on visibility restore, and staggered refresh to avoid API bursts.
repo: HKUDS/AgentPilot
Độ phổ biến
- Sao GitHub
- 7.8k
- Vị trí xếp hạng
- #13079
- Giấy phép
- MIT
- Cập nhật
- 1/11/2026
Bình luận
- …
Thêm skill từ kho mã này
agentpilot
7.8kGiúp agent của bạn cải thiện dự án từng bước bằng cách chuẩn bị tác vụ và chạy vòng lặp AgentPilot, gồm đề xuất, kiểm thử và chấm điểm thay đổi.
openspace
7.8kGiúp agent của bạn thiết lập OpenSpace, công cụ lưu và lấy lại skill của agent, rồi kết nối nó với ứng dụng AI của bạn qua MCP.
rsi-arena
7.8kGiúp agent của bạn thiết lập và chạy các bài kiểm thử benchmark AI chuẩn, như AIME và Terminal-Bench, để so sánh các phương pháp cải tiến khác nhau.
experiment-guide
7.8kGiúp agent của bạn lên kế hoạch các thí nghiệm nhỏ, đọc kết quả và ghi lại phát hiện khi tìm cách cải thiện một chương trình.
Skill tương tự
angular-developer
280kGiúp agent của bạn viết code Angular và tư vấn cấu trúc dự án, hữu ích khi bắt đầu dự án mới hoặc cải thiện hiệu suất.
frontend-a11y
280kGiúp agent của bạn làm cho giao diện web dễ tiếp cận với mọi người. Đảm bảo website và ứng dụng hoạt động tốt với trình đọc màn hình và điều hướng bằng bàn phím.
frontend-patterns
280kGiúp agent của bạn áp dụng các mẫu hiện đại trong phát triển frontend. Tối ưu hiệu năng, quản lý trạng thái và tuân theo thực hành tốt nhất với React và Next.js.