push-based-widget-module-pattern
它能做什么
这个技能展示如何搭建一个简单的小组件,它从调用方接收数据,并只更新发生变化的部分。适合构建经常刷新的侧边栏或面板的开发者。
适用场景
- ✓当侧边栏或小组件经常收到新数据,并且需要在不整体重绘的情况下更新时。
- ✓当数据来自小组件外部的控制器或事件系统时。
- ✓当你更喜欢用简单模块而不是类或框架组件时。
技能链接
https://github.com/HKUDS/AgentPilot/blob/main/openspace/legacy/v1/showcase/skills/push-based-widget-module-pattern/SKILL.md复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。
使用方法
最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。
可直接粘贴的消息
适用于 Claude Code、Codex 或 Cursor:在项目文件夹中运行,即可安装该技能。
下载 SKILL.md,放进智能体的技能文件夹。
可以这样问 AI
“请帮我做一个侧边栏小组件,每当我的应用发送新数据时它就自动更新。”
每周 5 个新技能,1 分钟读完。
工作原理
小组件保存了对其主要页面元素的引用。新数据到达时,它只修改这些元素,而不是重建整个小组件。
技术内容(原始 SKILL.md)
作者在 GitHub 上的英文原文。
Push-Based Widget Module Pattern
Use this pattern when a UI sidebar or widget component needs to be updated by its caller (push-based) rather than fetching data itself. Instead of a class or framework component, implement the widget as a plain module with singleton state, a factory function to create the DOM, and an update function to efficiently patch it.
When to Use
- A sidebar, panel, or widget is rendered once but updated frequently with new data.
- The data source lives outside the component (e.g., a parent controller, event bus, or store).
- You want lightweight partial re-renders without rebuilding the entire DOM tree.
- You prefer a simple module interface over a class-based or framework-based component.
Core Structure
widgets/
MyWidget.ts # The module (singleton state + exports)
MyWidget.types.ts # (Optional) Exported data interface
Step-by-Step Instructions
1. Define the Data Interface
Export a typed interface so callers know exactly what data to push.
// MyWidget.types.ts (or inline at top of MyWidget.ts)
export interface MyWidgetData {
title: string;
items: string[];
status: "idle" | "active" | "error";
// ... all fields the widget needs to render
}
2. Declare Module-Level Singleton Refs
At module scope, keep references to the container and any frequently updated child nodes. This avoids querying the DOM on every update.
// MyWidget.ts
import type { MyWidgetData } from "./MyWidget.types";
// Singleton DOM refs
let containerEl: HTMLElement | null = null;
let titleEl: HTMLElement | null = null;
let itemListEl: HTMLElement | null = null;
let statusEl: HTMLElement | null = null;
// Current state snapshot (optional — useful for diffing)
let currentData: MyWidgetData | null = null;
3. Export a Factory Function (createMyWidget)
The factory builds the full DOM tree once, stores refs to mutable nodes, and returns the root element to be mounted by the caller.
export function createMyWidget(): HTMLElement {
// Build the container
containerEl = document.createElement("aside");
containerEl.className = "my-widget";
// Build static structure; keep refs to dynamic parts
titleEl = document.createElement("h2");
titleEl.className = "my-widget__title";
itemListEl = document.createElement("ul");
itemListEl.className = "my-widget__list";
statusEl = document.createElement("span");
statusEl.className = "my-widget__status";
// Compose the tree
const header = document.createElement("header");
header.appendChild(titleEl);
header.appendChild(statusEl);
containerEl.appendChild(header);
containerEl.appendChild(itemListEl);
return containerEl;
}
Rule:
createMyWidgetshould be callable only once per page. If called again, consider returning the existingcontainerEl(idempotent factory).
4. Export an Update Function (updateMyWidget)
The update function receives the new data object and performs surgical DOM mutations on the stored refs. It does NOT rebuild the whole tree.
export function updateMyWidget(data: MyWidgetData): void {
if (!containerEl) {
console.warn("updateMyWidget called before createMyWidget");
return;
}
currentData = data;
// Update only what changed
if (titleEl) titleEl.textContent = data.title;
if (statusEl) {
statusEl.textContent = data.status;
statusEl.dataset.status = data.status; // for CSS hooks
}
// Rebuild only dynamic list items
if (itemListEl) {
itemListEl.innerHTML = ""; // clear previous items
data.items.forEach((item) => {
const li = document.createElement("li");
li.textContent = item;
itemListEl!.appendChild(li);
});
}
}
Optimization tip: For large lists, compare
currentDatatodatabefore clearing and rebuilding, or use a keyed reconciliation approach.
5. Caller Usage Pattern
The caller mounts the widget once and pushes updates whenever its data changes.
import { createMyWidget, updateMyWidget } from "./widgets/MyWidget";
import type { MyWidgetData } from "./widgets/MyWidget.types";
// Mount once
const sidebar = document.getElementById("sidebar")!;
sidebar.appendChild(createMyWidget());
// Push updates (e.g., from a store subscription, WebSocket, timer)
function onDataChange(newData: MyWidgetData) {
updateMyWidget(newData);
}
// Example: push an initial state immedi
常见问题
这里的推送式是什么意思?
意思是小组件等待应用的其他部分发送新数据,而不是自己去获取数据。
我需要框架吗?
不需要。这种模式使用纯 TypeScript 模块。
为什么只更新小组件的一部分?
局部更新可以保持页面速度,并在数据频繁变化时避免闪烁。
详情
作者的原始描述
How to implement sidebar/widget UI components that receive pushed data updates from a caller, using a singleton module pattern with exported factory and update functions plus module-level DOM refs for efficient partial re-renders.
repo: HKUDS/AgentPilot
热度
- GitHub 星标
- 7.8k
- 排名
- #13077
- 许可证
- MIT
- 更新时间
- 2026/11/1
评论
- …
同一仓库的更多技能
agentpilot
7.8k帮你的智能体准备任务并运行 AgentPilot 循环,逐步改进项目:循环会提出修改、测试修改并为其打分。
openspace
7.8k帮你的智能体设置 OpenSpace,这是一个用于存储和获取智能体技能的工具,并通过 MCP 将其连接到你的 AI 应用。
rsi-arena
7.8k帮你的智能体设置并运行标准 AI 基准测试,例如 AIME 和 Terminal-Bench,用来比较不同的改进方法。
experiment-guide
7.8k帮你的智能体规划小型实验、阅读实验结果并记录发现,同时研究如何改进一个程序。