订阅者观察者模式代码

xiaoxiao2021-02-28  73

这个就是一个实现了 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();

转载请注明原文地址: https://www.6miu.com/read-2627621.html

最新回复(0)