从node异步编程解决方案提及吧:node
事件监听器模式是一种普遍运用于异步编程的模式,是回调函数的事件话,又称发布/订阅模式。
主要实现的几个功能包括编程
废话少说,咱们来简单的实现一个事件监听函数吧数组
首先建立一个eventEmitter函数
function EventEmitter() { // 用Object.create(null)代替空对象{} // 好处是无杂质,不继承原型链 // _events来保存观察着队列的信息 this._events = Object.create(null); }
由于过多的侦听器占用大量内存,致使内存泄漏,因此侦听器的个数通常不会超过10个,不然会有warnning警告⚠️
接下来是一些默认的设置异步
// 默认最多的绑定次数 EventEmitter.defaultMaxListeners = 10; // 同on方法 EventEmitter.prototype.addListener = EventEmitter.prototype.on; // 返回监听的事件名 EventEmitter.prototype.eventNames = function () { return Object.keys(this._events); }; // 设置最大监听数 EventEmitter.prototype.setMaxListeners = function (n) { this._count = n; }; // 返回监听数 EventEmitter.prototype.getMaxListeners = function () { return this._count ? this._count : this.defaultMaxListeners; };
接下来是on函数的实现
EventEmitter.prototype.on = function (type, cb, flag) { // 不是newListener 就应该让newListener执行如下 if (type !== 'newListener') { this._events['newListener'] && this._events['newListener'].forEach(listener => { listener(type); }); } if (this._events[type]) { // 根据传入的flag来决定是向前仍是向后添加 if (flag) { this._events[type].unshift(cb); } else { this._events[type].push(cb); } } else { this._events[type] = [cb]; } // 监听的事件不能超过了设置的最大监听数 if (this._events[type].length === this.getMaxListeners()) { console.warn('警告-监听器Number过大'); } };
解析:
on函数是帮定的初始函数,首先判断是不是首次进行侦听,若是是的话,先进行一遍初始化函数
接下来在——events队列里找到指针为type的地方,根据flag判断是在队列尾仍是头加入callback函数异步编程
接下来是once监听一次的实现方法
// 监听一次 EventEmitter.prototype.once = function (type, cb, flag) { // 先绑定,调用后删除 function wrap() { cb(...arguments); this.removeListener(type, wrap); } // 自定义属性 wrap.listen = cb; this.on(type, wrap, flag); };
解析:
实现为在callback上包装一层remove操做,再当作一个新的callback传入on函数
这样的的话在首次执行回调的时候就会执行remove操做,达到执行一次就删除的操做函数
接下来是remove函数,删除一个type的侦听器
EventEmitter.prototype.removeListener = function (type, cb) { if (this._events[type]) { this._events[type] = this._events[type].filter(listener => { return cb !== listener && cb !== listener.listen; }); } };
解析:
传入type和要删除的callback,对type标记的数组进行 filter操做,假如cb cb === listener则过滤掉this
删除全部
EventEmitter.prototype.removeAllListener = function () { this._events = Object.create(null); };
接下来是发布函数 emit
EventEmitter.prototype.emit = function (type, ...args) { if (this._events[type]) { this._events[type].forEach(listener => { listener.call(this, ...args); }); } };
解析:
也比较直观,若是events里面存在type的监听器队列,则队列里的每一个回调都执行一遍,而且用call函数绑定this和argprototype
//EventEmitter.js function EventEmitter() { // 用Object.create(null)代替空对象{} // 好处是无杂质,不继承原型链的东东 this._events = Object.create(null); } // 默认最多的绑定次数 EventEmitter.defaultMaxListeners = 10; // 同on方法 EventEmitter.prototype.addListener = EventEmitter.prototype.on; // 返回监听的事件名 EventEmitter.prototype.eventNames = function () { return Object.keys(this._events); }; // 设置最大监听数 EventEmitter.prototype.setMaxListeners = function (n) { this._count = n; }; // 返回监听数 EventEmitter.prototype.getMaxListeners = function () { return this._count ? this._count : this.defaultMaxListeners; }; // 监听 EventEmitter.prototype.on = function (type, cb, flag) { // 默认值,若是没有_events的话,就给它建立一个 if (!this._events) { this._events = Object.create(null); } // 不是newListener 就应该让newListener执行如下 if (type !== 'newListener') { this._events['newListener'] && this._events['newListener'].forEach(listener => { listener(type); }); } if (this._events[type]) { // 根据传入的flag来决定是向前仍是向后添加 if (flag) { this._events[type].unshift(cb); } else { this._events[type].push(cb); } } else { this._events[type] = [cb]; } // 监听的事件不能超过了设置的最大监听数 if (this._events[type].length === this.getMaxListeners()) { console.warn('警告-警告-警告'); } }; // 向前添加 EventEmitter.prototype.prependListener = function (type, cb) { this.on(type, cb, true); }; EventEmitter.prototype.prependOnceListener = function (type, cb) { this.once(type, cb, true); }; // 监听一次 EventEmitter.prototype.once = function (type, cb, flag) { // 先绑定,调用后删除 function wrap() { cb(...arguments); this.removeListener(type, wrap); } // 自定义属性 wrap.listen = cb; this.on(type, wrap, flag); }; // 删除监听类型 EventEmitter.prototype.removeListener = function (type, cb) { if (this._events[type]) { this._events[type] = this._events[type].filter(listener => { return cb !== listener && cb !== listener.listen; }); } }; EventEmitter.prototype.removeAllListener = function () { this._events = Object.create(null); }; // 返回全部的监听类型 EventEmitter.prototype.listeners = function (type) { return this._events[type]; }; // 发布 EventEmitter.prototype.emit = function (type, ...args) { if (this._events[type]) { this._events[type].forEach(listener => { listener.call(this, ...args); }); } }; module.exports = EventEmitter;
个人博客即将同步至腾讯云+社区,邀请你们一同入驻:https://cloud.tencent.com/dev...指针