zignal
v1.0.2
Published
#### 介绍 简单的signal实现 *A simple signal implementation*
Readme
Zignal
介绍
简单的signal实现
A simple signal implementation
import { zignal, computed } from './zignal.js';
// 创建普通信号
// Create a regular signal
const count = zignal(0);
// 创建计算信号
// Create a computed signal
const doubleCount = computed(() => count.get() * 2, [count]);
// 订阅普通信号
// Subscribe to the regular signal
const unsubscribeCount = count.subscribe((newValue) => {
console.log(`count 变更为 ${newValue}`);
// Output: count changed to ${newValue}
});
// 订阅计算信号
// Subscribe to the computed signal
const unsubscribeDouble = doubleCount.subscribe((newValue) => {
console.log(`doubleCount 变更为 ${newValue}`);
// Output: doubleCount changed to ${newValue}
});
// 更新普通信号
// Update the regular signal
count.set(1);
// 输出 count 变更为 1, doubleCount 变更为 2
// Logs: count changed to 1, doubleCount changed to 2
// 取消订阅
// Unsubscribe
unsubscribeCount();
unsubscribeDouble();
// 销毁计算信号
// Dispose of the computed signal
doubleCount.dispose();