zoukankan      html  css  js  c++  java
  • 实现一个简单的订阅与发布模式的代码块,和redux

    /**
    * 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}
    }
  • 相关阅读:
    常用纹理数据库
    开源许可协议
    3TB-GPT-MBR
    ubuntu配置cudnn
    神经网络模型种类
    What is R语言
    DataWindow.NET 控件 实现点击列头排序
    PB调用C# Windows窗体
    工商银行卡网上查询开户行
    【DevExpress】1、SearchLookUpEdit详解
  • 原文地址:https://www.cnblogs.com/MrZouJian/p/8545238.html
Copyright © 2011-2022 走看看