实现一个简单的订阅与发布模式的代码块,和redux
2018-03-11 18:25
711 查看
/** * Created by Mrzou on 2018/3/11. */ //实现简单的订阅与发布模式的代码块 export function pattern() { let currentListeners = [] function subscribe(type, listener) { if ((typeof listener !== 'function') || (typeof listener !== 'string')) { throw new Error('参数类型错误') } else { currentListeners.push({type, listener}) } } function dispatch(type, value) { currentListeners.forEach(v=> { (v.type === type) && v.listener(value) }) } return {subscribe, dispatch} } //实现一个简单的redux export function createStore(reducer) { let currentState = {} let currentListeners = [] function getState() { return currentState } function subscribe(listener) { currentListeners.push(listener) } function dispatch(action) { currentState = reducer(currentState, action) currentListeners.forEach(v=>v()) return action } dispatch({type: '@@MONI_REDUX/INITIALSTATE'}) return {getState, subscribe, dispatch} }
相关文章推荐
- 实现一个简单的事件订阅通知机制(Observer模式实现)
- 自实现观察者模式(发布/订阅模式)的一个隐藏bug
- 学习笔记-js发布/订阅模式的简单实现
- JavaScript简单实现发布订阅模式
- DAO设计模式---实现一个简单的注册(下)
- 一个简单地MVVM模式实现,直接上码
- 简单工厂模式---一个简单计算器的实现
- 一个java实现的简单工厂模式
- 一个简单观察者模式的实现 cocos2dx
- JavaScript中发布/订阅模式的简单实例
- C#实现发布订阅模式
- 使用EF6和MVC5实现一个简单的选课系统--使用EF6异步编程模式和存储过程(9/12)
- 从零开始打造一个新闻订阅APP之爬虫篇(二、实现一个简单的爬虫系统)
- 一个简单的态机模式工作流演示(2)(代码与实现)
- JavaScript中发布/订阅模式的简单实例
- C++ 使用模版范式写一个单例模式的类--简单实现单例模式
- Spring Data Redis实现一个订阅/发布系统
- 一个用简单的JavaScript及Css实现的模式层
- Shuttle ESB(五)——发布订阅模式实例实现(2)
- DAO设计模式---实现一个简单的注册(中)