event.js 1013 B

12345678910111213141516171819202122232425262728293031323334
  1. var initEvent = function (cos) {
  2. var listeners = {};
  3. var getList = function (action) {
  4. !listeners[action] && (listeners[action] = []);
  5. return listeners[action];
  6. };
  7. cos.on = function (action, callback) {
  8. if (action === 'task-list-update') {
  9. console.warn('warning: Event "' + action + '" has been deprecated. Please use "list-update" instead.');
  10. }
  11. getList(action).push(callback);
  12. };
  13. cos.off = function (action, callback) {
  14. var list = getList(action);
  15. for (var i = list.length - 1; i >= 0; i--) {
  16. callback === list[i] && list.splice(i, 1);
  17. }
  18. };
  19. cos.emit = function (action, data) {
  20. var list = getList(action).map(function (cb) {
  21. return cb;
  22. });
  23. for (var i = 0; i < list.length; i++) {
  24. list[i](data);
  25. }
  26. };
  27. };
  28. var EventProxy = function () {
  29. initEvent(this);
  30. };
  31. module.exports.init = initEvent;
  32. module.exports.EventProxy = EventProxy;