You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

57 lines
1.1 KiB

/**
* 状态管理模块
* 提供简单的状态存储和订阅机制
*/
const store = {
/**
* 状态数据
*/
state: {},
/**
* 订阅者列表
*/
subscribers: [],
/**
* 获取状态
* @param {string} key - 状态键名
* @returns {*} - 状态值
*/
get: function(key) {
return this.state[key];
},
/**
* 设置状态
* @param {string} key - 状态键名
* @param {*} value - 状态值
*/
set: function(key, value) {
this.state[key] = value;
this.notify();
},
/**
* 订阅状态变化
* @param {Function} callback - 回调函数
* @returns {Function} - 取消订阅函数
*/
subscribe: function(callback) {
this.subscribers.push(callback);
return () => {
this.subscribers = this.subscribers.filter(sub => sub !== callback);
};
},
/**
* 通知订阅者
*/
notify: function() {
this.subscribers.forEach(callback => callback(this.state));
}
};
// 导出状态管理模块
module.exports = store;