游戏开发笔记-消息监听及分发

一个监听器的结构通常是

  • 监听标志 消息类型
  • 回调函数 消息动作
  • target 消息来源位置
  • once 是否是一次性监听

listener={msg:msg,listener:listener,target:target,once:false}

将监听的结构放在一个字典中

msg2listDict={
    msg1:listener1,
    msg2:listener2,
    msg3:listener3,
    msg4:listener4,
    ...
}

触发监听

  1. 触发结构 msg,data
  2. 通过 msg 找到监听,并以 data 为参数执行监听函数
  3. 如果是一次性监听,执行完之后将该监听移除

code

class NotifyCenter{
    constructor(){
        this.msg2listDict = {}
    }
    static instance(){
        if (NotifyCenter.g === undefined) {
            NotifyCenter.g = new NotifyCenter()
        }
        return NotifyCenter.g
    }
    addListener(msg,listener,target){
        cc.log('NotifyCenter 监听消息',msg,listener)
        let info = {msg:msg,listener:listener,target:target}
        if (this.msg2listDict.hasOwnProperty(msg)) {
            this.msg2listDict[msg].push(info)
        } else {
            this.msg2listDict[msg] = [info]
        }
    }
    addOnceListener(msg,listener,target){
        let info = {msg:msg,listener:listener,target:target,once:true}
        this.msg2listDict[msg] = [info]

    }
    removeMsgListener(msg,listener){
        if (!this.msg2listDict.hasOwnProperty(msg)) return;
        let list = this.msg2listDict[msg]
        for (let i=0;i<list.length;i++) {
            let info = list[i]
            if (info.listener === listener) {
                list.splice(i,1)
                break
            }
        }
    }
    removeAllListener(){
        this.msg2listDict = {}
    }
    removeMsgAllListener(msg){
        if (this.msg2listDict.hasOwnProperty(msg)) {
            this.msg2listDict[msg] = []
        }
    }
    removeTargetMsgListener(msg,target){
        if (!this.msg2listDict.hasOwnProperty(msg)) return;
        let list = this.msg2listDict[msg]
        for (let i=list.length; i>=0; i--) {
            let info = list[i]
            if (info.target === target) {
                list.splice(i,1)
                break
            }
        }
    }
    removeTargetAllListener(target){
        for (let msg of this.msg2listDict) {
            let list = this.msg2listDict[msg]
            for (let i=list.length; i>=0; i--) {
                if (list[i].target === target) {
                    list.splice(i,1)
                    break
                }
            }
        }
    }
    notify(msg,data){
        if (!this.msg2listDict.hasOwnProperty(msg)) return;
        let list = this.msg2listDict[msg]
        for (let i=list.length-1; i>=0; i--){
            let info = list[i]
            if (info.hasOwnProperty('once') && !!info.once){
                list.splice(i,1)
            }
            this.dispatch(info, msg, data)
        }
    }
    dispatch(info,msg,data) {
        info.listener(msg,data)
    }
}

NotifyCenter.g = undefined

export default NotifyCenter.instance()

测试用例

class Notify {

}

转载于:https://www.cnblogs.com/heitufei/p/11509199.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值