javascript发布订阅模式(代码片段)

author author     2022-12-05     565

关键词:

class Event 
    on (event, fn, ctx) 
        if (typeof fn != "function") 
            console.error('fn must be a function')
            return
        

        this._stores = this._stores || 

        ;(this._stores[event] = this._stores[event] || []).push(cb: fn, ctx: ctx)
    
    emit (event) 
        this._stores = this._stores || 
        var store = this._stores[event], args
        if (store) 
            store = store.slice(0)
            args = [].slice.call(arguments, 1)
            for (var i = 0, len = store.length; i < len; i++) 
                store[i].cb.apply(store[i].ctx, args)
            
        
    
    off (event, fn) 
        this._stores = this._stores || 
        // all
        if (!arguments.length) 
            this._stores = 
            return
        
        // specific event
        var store = this._stores[event]
        if (!store) return
        // remove all handlers
        if (arguments.length === 1) 
            delete this._stores[event]
            return
        
        // remove specific handler
        var cb
        for (var i = 0, len = store.length; i < len; i++) 
            cb = store[i].cb
            if (cb === fn) 
                store.splice(i, 1)
                break
            
        
        return
    
/**
 * 使用示例
 * /

// app.js

const Event = require('./utils/event')
App(
    event: new Event(),
    ...
)

// subsPage.js

let app = getApp()
Page(
    onLoad: function()
        app.event.on('eventName',this.triggerEvent, this)
    ,
    onUnload: function()
        // remove all
        // app.event.off()
        // remove all callbacks
        app.event.off('eventName')
        // remove specific callbacks
        // app.event.off('eventName', this.triggerEvent)
    ,
    triggerEvent: function(args) 
        ...
    ,
    ...
)

// emitPage.js

let app = getApp()
Page(
    emitFunc: function() 
        ... 
        app.event.emit('eventName', arg)
    ,
    ...
)

发布-订阅者模式(观察者模式)(代码片段)

发布订阅者模式还有一些好的列子应用可以看看javascript设计模式与开发实践这本书!!!!!一、发布订阅模式是什么发布订阅者模式又叫观察者模式,它定义对象间的一种一对多的依赖关系,当一个对象的状态发生改变时,... 查看详情

javascript发布订阅者模式和观察者模式及区别(代码片段)

一、发布订阅模式发布订阅模式其实是一种对象间一对多的依赖关系,当一个对象的状态发生改变时,所有依赖于它的对象都将得到状态改变的通知。多方订阅,一方发布,订阅放会收到通知举例:教学楼中... 查看详情

javascript发布订阅者模式和观察者模式及区别(代码片段)

一、发布订阅模式发布订阅模式其实是一种对象间一对多的依赖关系,当一个对象的状态发生改变时,所有依赖于它的对象都将得到状态改变的通知。多方订阅,一方发布,订阅放会收到通知举例:教学楼中... 查看详情

eventbus发布-订阅模式(使用代码实现发布-订阅模式)(代码片段)

文章目录一、发布-订阅模式二、代码实现发布-订阅模式1、订阅者接口2、订阅者实现类3、发布者4、调度中心5、客户端一、发布-订阅模式发布订阅模式:发布者Publisher:状态改变时,向消息中心发送事件;订阅者Subscriber:到消息中心... 查看详情

javascript手撕前端面试题:寄生组合式继承|发布订阅模式|观察者模式(代码片段)

🧑‍💼个人简介:大三学生,一个不甘平庸的平凡人🍬🖥️NodeJS专栏:Node.js从入门到精通🖥️博主的前端之路(源创征文一等奖作品):前端之行,任重道远(来自大三学长... 查看详情

eventbus发布-订阅模式(android中使用发布-订阅模式进行通信)(代码片段)

文章目录一、拷贝发布-订阅模式相关类二、完整代码示例一、拷贝发布-订阅模式相关类将上一篇博客【EventBus】发布-订阅模式(使用代码实现发布-订阅模式)写的发布-订阅模式相关代码拷贝到AndroidStudio工程中,在Android中,将Activity... 查看详情

设计模式:发布订阅模式(代码片段)

发布订阅模式调度中心相关方法订阅取消订阅触发示例调度中心classCenterconstructor()this.message=//订阅on(type,fn)if(this.message[type]===undefined)this.message[type]=[]this.message[type].push(fn)//取消订阅off 查看详情

设计模式:发布订阅模式(代码片段)

发布订阅模式调度中心相关方法订阅取消订阅触发示例调度中心classCenterconstructor()this.message=//订阅on(type,fn)if(this.message[type]===undefined)this.message[type]=[]this.message[type].push(fn)//取消订阅off(type,fn) 查看详情

javascript设计模式--发布/订阅模式

直接上代码:index.html:<!DOCTYPEhtml><htmllang="en"><head> <metacharset="UTF-8"> <title>设计模式</title></head><body> <divid="box"> <div>{{message}}& 查看详情

发布订阅模式(代码片段)

letfs=require(‘fs‘);//发布订阅发布和订阅没关系中间通过数组进行关联functionEvents()this.callbacks=[];this.results=[];//订阅Events.prototype.on=function(callback)this.callbacks.push(callback);//发布Events.prototype.emit=func 查看详情

javascriptjs发布订阅模式(代码片段)

查看详情

设计模式-发布-订阅者模式(代码片段)

1、发布-订阅者设计模式定义定义对象间的一种一对多的依赖关系,当一个对象的状态发生改变时,所有依赖于它的对象都将得到通知观察者模式和发布订阅模式区别观察者模式是由具体目标(发布者/被观察者)调度的,而发... 查看详情

activemq入门系列三:发布/订阅模式(代码片段)

...)》中提到了ActiveMQ中的两种模式:点对点模式(PTP)和发布/订阅模式(Pub&Sub),详细介绍了点对点模式并用代码实例进行说明,今天就介绍下发布/订阅模式。一、理论基础发布/订阅模式的工作示意图:消息生产者将消息... 查看详情

javaredis的订阅发布模式使用(代码片段)

查看详情

javaspringboot发布订阅模式的应用(代码片段)

查看详情

发布订阅模式源码实现(代码片段)

varshoeObj=;//定义发布者shoeObj.list=[];//缓存列表存放订阅者回调函数//增加订阅者shoeObj.listen=function(key,fn)if(!this.list[key])//如果还没有订阅过此类消息,给该类消息创建一个缓存列表this.list[key]=[];this.list[key].push(fn);//订阅消息添加到... 查看详情

eventbus发布-订阅模式(eventbus组成模块|观察者模式)(代码片段)

文章目录一、发布-订阅模式二、EventBus组成模块三、观察者模式一、发布-订阅模式发布订阅模式:发布者Publisher:状态改变时,向消息中心发送事件;订阅者Subscriber:到消息中心订阅自己关心的事件;消息中心:负责维护一个消息队列,... 查看详情

javascript使用jquery的简单发布/订阅(代码片段)

查看详情