订阅者观察者模式代码

这个就是一个实现了
function Store() {
const event = {};
// 添加事件在某个事件里面
const addEvent = ({ type, payload }) => {
event[type] = event[type] || [];
event[type].push(payload);
}
// 转发这个行为
// 是哪个行为就在那个行为里面进行函数调用
const dispatch = (type, …args) => {
event[type] && event[type].forEach(item => item(…args))
}
// 获取state数据
const getState = () => event;
return {
getState,
dispatch,
addEvent
}
}

module.exports = Store();

猜你喜欢

转载自blog.csdn.net/qq_32798243/article/details/80174172