|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +/* global global, window, self */ |
| 4 | + |
| 5 | +var _asyncNextTick = (function (_global, _raf_prefixes) { |
| 6 | + |
| 7 | + if( 'nextTick' in _global ) return _global.nextTick |
| 8 | + |
| 9 | + for( var _prefix in _raf_prefixes ) { |
| 10 | + if( _global[_prefix + 'equestAnimationFrame'] instanceof Function ) |
| 11 | + return _global[_prefix + 'equestAnimationFrame'] |
| 12 | + } |
| 13 | + |
| 14 | + return setTimeout |
| 15 | + |
| 16 | +})( |
| 17 | + typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : typeof window !== 'undefined' ? window : {}, |
| 18 | + 'r, webkitR, mozR, oR, msR'.split(/ *, */) |
| 19 | +); |
| 20 | + |
| 21 | +var _syncNextTick = function (fn) { fn.apply(this, arguments); }; |
| 22 | + |
| 23 | +function _removeFromList ( list, item ) { |
| 24 | + for( var i = list.length - 1 ; i >= 0 ; i-- ) { |
| 25 | + if( item === list[i] ) list.splice(i, 1); |
| 26 | + } |
| 27 | +} |
| 28 | + |
| 29 | +function _normalizeEventName (fn, host, test_listener) { |
| 30 | + return function _fn (event_name, listener, use_capture) { |
| 31 | + if( test_listener !== false && !(listener instanceof Function ) ) throw new Error('listener should be a Function') |
| 32 | + |
| 33 | + if( event_name instanceof Array ) { |
| 34 | + event_name.forEach(function (_event_name) { |
| 35 | + _fn(_event_name, listener, use_capture); |
| 36 | + }); |
| 37 | + return host |
| 38 | + } |
| 39 | + // runAsArray(fn, arguments, this, host ); |
| 40 | + if( typeof event_name !== 'string' ) throw new Error('event_name should be a string') |
| 41 | + if( / /.test(event_name) ) return _fn(event_name.split(/ +/), listener, use_capture) |
| 42 | + |
| 43 | + fn.apply(this, arguments); |
| 44 | + |
| 45 | + return host |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +function Azazel (host, options) { |
| 50 | + options = options || {}; |
| 51 | + if( typeof options === 'string' ) options = { |
| 52 | + prefix: options |
| 53 | + }; |
| 54 | + options.prefix = options.prefix || ''; |
| 55 | + |
| 56 | + host = host || (this instanceof Azazel ? this : {}); |
| 57 | + |
| 58 | + var listeners = {}, |
| 59 | + listeners_once = {}, |
| 60 | + events_emitted = {}, |
| 61 | + _nextTick = options.async !== false ? _asyncNextTick : _syncNextTick; |
| 62 | + |
| 63 | + function _on (event_name, listener, use_capture) { |
| 64 | + listeners[event_name] = listeners[event_name] || []; |
| 65 | + if( use_capture ) listeners[event_name].unshift(listener); |
| 66 | + else listeners[event_name].push(listener); |
| 67 | + } |
| 68 | + |
| 69 | + var on = _normalizeEventName(_on, host); |
| 70 | + |
| 71 | + function _once (event_name, listener, use_capture) { |
| 72 | + listeners_once[event_name] = listeners_once[event_name] || []; |
| 73 | + if( use_capture ) listeners_once[event_name].unshift(listener); |
| 74 | + else listeners_once[event_name].push(listener); |
| 75 | + } |
| 76 | + |
| 77 | + var once = _normalizeEventName(_once, host); |
| 78 | + |
| 79 | + function watch (event_name, listener, use_capture) { |
| 80 | + if( !(listener instanceof Function ) ) throw new Error('listener should be a Function') |
| 81 | + var last_emitted = events_emitted[event_name]; |
| 82 | + if( last_emitted ) listener.apply(last_emitted.this_arg, last_emitted.args); |
| 83 | + if( !last_emitted || use_capture !== 'once' ) _on(event_name, listener, use_capture); |
| 84 | + return host |
| 85 | + } |
| 86 | + |
| 87 | + function _emit (event_name, args, this_arg) { |
| 88 | + _nextTick(function () { |
| 89 | + events_emitted[event_name] = { args: args, this_arg: this_arg }; |
| 90 | + |
| 91 | + if( listeners[event_name] ) listeners[event_name].forEach(function (listener) { |
| 92 | + listener.apply(this_arg, args || []); |
| 93 | + }); |
| 94 | + |
| 95 | + if( listeners_once[event_name] ) { |
| 96 | + _emitOnce(listeners_once[event_name], args, this_arg); |
| 97 | + delete listeners_once[event_name]; |
| 98 | + } |
| 99 | + }); |
| 100 | + } |
| 101 | + |
| 102 | + function _emitOnce (listeners_list, args, this_arg) { |
| 103 | + listeners_list.forEach(function (listener) { |
| 104 | + listener.apply(this_arg, args || []); |
| 105 | + }); |
| 106 | + |
| 107 | + for( var event_name in listeners_once ) { |
| 108 | + if( listeners_once[event_name] !== listeners_list ) listeners_list.forEach(function (_listener) { |
| 109 | + if( listeners_once[event_name] ) _removeFromList(listeners_once[event_name], _listener ); |
| 110 | + }); |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + var emit = _normalizeEventName(_emit, host, false); |
| 115 | + |
| 116 | + function off (event_name, listener) { |
| 117 | + if( listeners[event_name] ) _removeFromList(listeners[event_name], listener ); |
| 118 | + if( listeners_once[event_name] ) _removeFromList(listeners_once[event_name], listener ); |
| 119 | + } |
| 120 | + |
| 121 | + host[options.prefix + 'on'] = on; |
| 122 | + host[options.prefix + 'once'] = once; |
| 123 | + host[options.prefix + 'watch'] = watch; // like watch but triggering |
| 124 | + |
| 125 | + host[options.prefix + 'emit'] = emit; |
| 126 | + host[options.prefix + 'off'] = _normalizeEventName(off); |
| 127 | + |
| 128 | + return host |
| 129 | +} |
| 130 | + |
| 131 | +Azazel.nextTick = _asyncNextTick; |
| 132 | + |
| 133 | +module.exports = Azazel; |
0 commit comments