JS 发布订阅模式

首先声明,本文并不是原创。原文请点击这里,本文是在原文的基础上加入一些本身的一些东西,方便之后本身理解与查看。git

发布订阅模式

事件发布/订阅模式 (PubSub) 在异步编程中帮助咱们完成更松的解耦,甚至在 MVC、MVVC 的架构中以及设计模式中也少不了发布-订阅模式的参与。github

优势:在异步编程中实现更深的解耦编程

缺点:若是过多的使用发布订阅模式,会增长维护的难度设计模式

实现发布订阅模式

var Event = function() {
    this.obj = {}
}

Event.prototype.on = function(eventType,fn) {
    if(!this.obj[eventType]) {
        this.obj[eventType] = []
    }
    this.obj[eventType].push(fn)
}

Event.prototype.emit = function() {
    // 取第一个参数,做为eventType
    var eventType = Array.prototype.shift.call(arguments);
    //  获取事件数组
    var arr = this.obj[eventType];
    var len = arr.length;
    // 循环数组,一次执行其中的函数
    for(var i=0;i<len;i++) {
        // 直接调用arr[i],其this指向为undefined(严格模式下)
        // 所以用apply将this指向arr[i]
        // 数组shift函数取出第一个参数,将剩下的参数传入函数中
        arr[i].apply(arr[i],arguments)
    }
}

var ev = new Event()
ev.on('click',function(a) {  // 订阅
    console.log(a)
})

ev.emit('click',1)   // 发布

以上代码只能实现先订阅,再发布。直接发布就会报错。如何实现能够先发布,而后订阅?数组

var Event = function() {
    this.obj = {};
    this.cacheList = [];
}

Event.prototype.emit = function() {
    const args = arguments;  //函数参数
    const that = this;  //this指向,保持cache函数的this指向
    function cache() {
        var eventType = Array.prototype.shift.call(arg)
        var arr = that.obj[eventType]
        for (let i = 0; i < arr.length; i++) {
          arr[i].apply(arr[i], arg)
        }
    }
    this.cacheList.push(cache)  // 采用闭包,保持对emit函数中参数和that的引用
}

Event.prototype.on = function(eventType,fn) {
    if(!this.obj[eventType]) {
        this.obj[eventType] = []
    }
    this.obj[eventType].push(fn)
    // 在订阅函数中执行emit函数中缓存的函数
    for (let i = 0; i < this.cacheList.length; i++) {
        this.cacheList[i]()
    }
}

改为这样后就实现了先发布函数,再订阅的过程。可是也只能先发布,而后再订阅,反过来就行不通。缓存

相关文章
相关标签/搜索