123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129 |
- 'use strict';
- var has = Object.prototype.hasOwnProperty;
- var id = 0;
- function Ultron(ee) {
- if (!(this instanceof Ultron)) return new Ultron(ee);
- this.id = id++;
- this.ee = ee;
- }
- Ultron.prototype.on = function on(event, fn, context) {
- fn.__ultron = this.id;
- this.ee.on(event, fn, context);
- return this;
- };
- Ultron.prototype.once = function once(event, fn, context) {
- fn.__ultron = this.id;
- this.ee.once(event, fn, context);
- return this;
- };
- Ultron.prototype.remove = function remove() {
- var args = arguments
- , event;
-
-
-
-
- if (args.length === 1 && 'string' === typeof args[0]) {
- args = args[0].split(/[, ]+/);
- } else if (!args.length) {
- args = [];
- for (event in this.ee._events) {
- if (has.call(this.ee._events, event)) args.push(event);
- }
- }
- for (var i = 0; i < args.length; i++) {
- var listeners = this.ee.listeners(args[i]);
- for (var j = 0; j < listeners.length; j++) {
- event = listeners[j];
-
-
-
-
- if (event.listener) {
- if (event.listener.__ultron !== this.id) continue;
- delete event.listener.__ultron;
- } else {
- if (event.__ultron !== this.id) continue;
- delete event.__ultron;
- }
- this.ee.removeListener(args[i], event);
- }
- }
- return this;
- };
- Ultron.prototype.destroy = function destroy() {
- if (!this.ee) return false;
- this.remove();
- this.ee = null;
- return true;
- };
- module.exports = Ultron;
|