├── .gitignore ├── LICENSE ├── README.md ├── component.json └── events.js /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | .idea -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # events 2 | 事件管理中心,用于全局监听,派发事件 3 | -------------------------------------------------------------------------------- /component.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "events", 3 | "version": "0.0.5", 4 | "main": "events.js", 5 | "description": "事件管理中心,用于全局监听,派发事件" 6 | } -------------------------------------------------------------------------------- /events.js: -------------------------------------------------------------------------------- 1 | /** 2 | * 事件管理中心,用于监听,派发事件 3 | */ 4 | var _listenerMap = {}, 5 | slice = [].slice; 6 | 7 | function call(callback, args) { 8 | var fn = callback[0], 9 | context = callback[1], 10 | args = callback[2].concat(args); 11 | try { 12 | return fn.apply(context, args); 13 | } catch (e) { 14 | setTimeout(function () { 15 | throw e; 16 | }, 0); 17 | } 18 | } 19 | 20 | function arrayClone(arr, len) { 21 | var copy = new Array(len); 22 | while (len--) 23 | copy[len] = arr[len]; 24 | return copy; 25 | } 26 | 27 | function emit(type, args) { 28 | var listenerList = _listenerMap[type]; 29 | if (!listenerList) 30 | return true; 31 | args = slice.call(arguments, 1); 32 | var len = listenerList.cbs.length; 33 | var cbs = arrayClone(listenerList.cbs, len); 34 | var ret = true; 35 | for (var index = 0; index < len; index++) { 36 | if (!cbs[index]) 37 | continue; 38 | ret = call(cbs[index], args) !== false && ret; 39 | } 40 | return !!ret; 41 | } 42 | 43 | var events = { 44 | on: function (type, fn, context) { 45 | var listenerList, 46 | callback, args; 47 | if (!(listenerList = _listenerMap[type])) { 48 | _listenerMap[type] = listenerList = { 49 | args: null, 50 | cbs: [] 51 | }; 52 | } 53 | 54 | callback = [fn, context, slice.call(arguments, 3)]; 55 | if (args = listenerList.args) { 56 | call(callback, args); 57 | } else { 58 | listenerList.cbs.push(callback); 59 | } 60 | }, 61 | un: function (type, fn) { 62 | var listenerList = _listenerMap[type], 63 | cbs, count; 64 | if (!listenerList) 65 | return true; 66 | if (arguments.length == 1) { 67 | listenerList.cbs = []; 68 | } else { 69 | cbs = listenerList.cbs; 70 | count = cbs.length; 71 | while (count--) { 72 | if (cbs[count] && cbs[count][0] === fn) { 73 | cbs.splice(count, 1); 74 | } 75 | } 76 | } 77 | }, 78 | emit: function (type, args) { 79 | return emit.apply(this, arguments); 80 | }, 81 | done: function (type, args) { 82 | var listenerList, ret, cbs, count; 83 | if (!(listenerList = _listenerMap[type])) { 84 | _listenerMap[type] = listenerList = { 85 | args: args, 86 | cbs: [] 87 | }; 88 | } 89 | cbs = listenerList.cbs; 90 | count = cbs.length; 91 | 92 | ret = emit.apply(this, arguments); 93 | 94 | args = slice.call(arguments, 1); 95 | listenerList.args = args; 96 | listenerList.cbs = cbs.slice(count); 97 | }, 98 | undo: function (type) { 99 | var listenerList = _listenerMap[type]; 100 | if (!listenerList) 101 | return false; 102 | listenerList.args = null; 103 | } 104 | }; 105 | 106 | module.exports = events; 107 | --------------------------------------------------------------------------------