Skillpedia
المواقع والواجهات (frontend)7.8k · #13070

panel-data-resilient

ما الذي تفعله

توضح هذه المهارة كيف تُبنى لوحات تحكم بلغة TypeScript بلا إطار عمل، تجلب بياناتها بنفسها. تتوقف كل لوحة عن استدعاء خدمة متعطلة لفترة، ثم تحاول مجددًا. وهي مخصصة لمطوري لوحات الويب الحية.

متى تستخدمها

  • ✓عندما تريد لوحة تحكم لا تنهار عندما تتوقف خدمة بيانات على الإنترنت.
  • ✓عندما تضيف عنصرًا جديدًا يستدعي خدمة ويب خارجية ويحتاج إلى منطق إعادة محاولة.
  • ✓عندما تريد أن تعرض اللوحات حالة تحميل أو خطأ واضحة بدل أن تتجمد.

رابط المهارة

https://github.com/HKUDS/AgentPilot/blob/main/openspace/legacy/v1/showcase/skills/panel-data-resilient/SKILL.md

انسخه والصقه في الذكاء الاصطناعي الذي تستخدمه (Claude أو ChatGPT أو Cursor) واطلب منه اتباع التعليمات.

طريقة الاستخدام

أسهل طريقة: انسخ الرسالة والصقها في الذكاء الاصطناعي الذي تستخدمه. سيقرأ المهارة وينفّذ تعليماتها.

رسالة جاهزة للصق

استخدم هذه المهارة لمساعدتي: https://github.com/HKUDS/AgentPilot/blob/main/openspace/legacy/v1/showcase/skills/panel-data-resilient/SKILL.md اقرأ ملف SKILL.md في هذا الرابط ونفّذ تعليماته لمهمتي.
تريد أن يجد الذكاء الاصطناعي مهارات مثل هذه بنفسه؟ثبّت خادم Skillpedia MCP المجاني: سطر واحد، ويبحث في 15,000+ مهارة قبل كل مهمة.ثبّته في دقيقة واحدة →

مثال على ما تطلبه من الذكاء الاصطناعي

“هل تستطيع إنشاء لوحة تحكم تستمر في العمل حتى عندما تكون خدمة البيانات التي تستخدمها متوقفة؟”

5 مهارات جديدة كل أسبوع. قراءة في دقيقة واحدة.

كيف تعمل

تطلب اللوحة البيانات عبر دالة مساعدة تحسب الأعطال. وبعد عدة أعطال تنتظر قبل المحاولة من جديد، ويمكنها أن تعرض النتائج المحفوظة في هذه الأثناء.

المحتوى التقني (ملف SKILL.md الأصلي)

النص الأصلي من المؤلف، مباشرة من GitHub.

Resilient Panel Pattern

This skill merges the best of data-service and panel-component to create panels that are both resilient to API failures and easy to implement. Each panel manages its own data fetching with built-in circuit breaker patterns and renders content using vanilla TypeScript.

Architecture Overview

  1. Service Module: Each panel has a dedicated service module for data fetching, using circuit breakers for resilience.
  2. Panel Class: Extends a base Panel class with built-in loading/error states and content rendering.
  3. Data Flow: Panels automatically fetch data, handle errors, and retry as needed.

Implementation

1. Circuit Breaker Utility

Create src/utils/circuit-breaker.ts (same as in data-service but with enhanced logging):

interface CircuitState {
  failures: number;
  cooldownUntil: number;
}

interface CacheEntry<T> {
  data: T;
  timestamp: number;
}

export interface CircuitBreakerOptions {
  name: string;
  maxFailures?: number;
  cooldownMs?: number;
  cacheTtlMs?: number;
}

export class CircuitBreaker<T> {
  private state: CircuitState = { failures: 0, cooldownUntil: 0 };
  private cache: CacheEntry<T> | null = null;
  private name: string;
  private maxFailures: number;
  private cooldownMs: number;
  private cacheTtlMs: number;

  constructor(options: CircuitBreakerOptions) {
    this.name = options.name;
    this.maxFailures = options.maxFailures ?? 2;
    this.cooldownMs = options.cooldownMs ?? 5 * 60 * 1000;  // 5 minutes
    this.cacheTtlMs = options.cacheTtlMs ?? 10 * 60 * 1000; // 10 minutes
  }

  isOnCooldown(): boolean {
    if (Date.now() < this.state.cooldownUntil) return true;
    if (this.state.cooldownUntil > 0) {
      this.state = { failures: 0, cooldownUntil: 0 };
    }
    return false;
  }

  getCached(): T | null {
    if (this.cache && Date.now() - this.cache.timestamp < this.cacheTtlMs) {
      return this.cache.data;
    }
    return null;
  }

  recordSuccess(data: T): void {
    this.state = { failures: 0, cooldownUntil: 0 };
    this.cache = { data, timestamp: Date.now() };
  }

  recordFailure(error?: string): void {
    this.state.failures++;
    if (this.state.failures >= this.maxFailures) {
      this.state.cooldownUntil = Date.now() + this.cooldownMs;
      console.warn(`[${this.name}] Cooldown for ${this.cooldownMs / 1000}s`);
    }
  }

  async execute<R extends T>(fn: () => Promise<R>, defaultValue: R): Promise<R> {
    if (this.isOnCooldown()) {
      const cached = this.getCached();
      return (cached as R) ?? defaultValue;
    }
    const cached = this.getCached();
    if (cached !== null) return cached as R;
    try {
      const result = await fn();
      this.recordSuccess(result);
      return result;
    } catch (e) {
      console.error(`[${this.name}] Failed:`, e);
      this.recordFailure(String(e));
      return defaultValue;
    }
  }
}

export function createCircuitBreaker<T>(options: CircuitBreakerOptions): CircuitBreaker<T> {
  return new CircuitBreaker<T>(options);
}

2. Base Panel Class

Create src/components/Panel.ts with enhanced error handling and retry logic:

export interface PanelOptions {
  id: string;
  title: string;
  showCount?: boolean;
  className?: string;
}

export class Panel {
  protected element: HTMLElement;
  protected content: HTMLElement;
  protected header: HTMLElement;
  protected countEl: HTMLElement | null = null;
  protected panelId: string;
  private _fetching = false;

  constructor(options: PanelOptions) {
    this.panelId = options.id;
    this.element = document.createElement('div');
    this.element.className = `panel ${options.className || ''}`;
    this.element.dataset.panel = options.id;

    // Header
    this.header = document.createElement('div');
    this.header.className = 'panel-header';

    const headerLeft = document.createElement('div');
    headerLeft.className = 'panel-header-left';

    const title = document.createElement('span');
    title.className = 'panel-title';
    title.textContent = options.title;
    headerLeft.appendChild(title);
    this.header.appendChild(headerLeft);

    // Count badge (optional)
    if (options.showCount) {
      this.countEl = document.createElement('span');
      this.countEl.className = 'panel-count';
      this.countEl.textContent = '0';
      this.header.appendChild(this.countEl);
    }

    // Content area
    this.content = document.createElement('div');
    this.content.cl
الأسئلة الشائعة

هل أحتاج إلى React أو إطار عمل آخر لاستخدام هذا؟

لا، يستخدم النمط لغة TypeScript العادية وميزات الصفحة المدمجة في المتصفح.

ما قاطع الدائرة بكلمات بسيطة؟

هو مفتاح يوقف الطلبات المتكررة إلى خدمة متعطلة لفترة قصيرة، ثم يسمح بمحاولات جديدة.

هل يعمل هذا مع أي مصدر بيانات على الويب؟

نعم، طالما تستطيع اللوحة طلب البيانات عبر الويب. الأمثلة عامة، فيمكنك توجيه اللوحة إلى مصدرك الخاص.

التفاصيل

الوصف الأصلي من المؤلف

Create resilient, data-driven dashboard panels with integrated API handling and UI components, combining circuit breaker patterns with vanilla TypeScript panel architecture.

repo: HKUDS/AgentPilot

الشعبية

نجوم GitHub
7.8k
الترتيب
#13070
الترخيص
MIT
آخر تحديث
1‏/11‏/2026

هل هذه المهارة مفيدة؟

0

سجّل الدخول للتصويت.

التعليقات

  • …

المزيد من المهارات من هذا المستودع

مهارات مشابهة