1
0

signal.js 963 B

1234567891011121314151617181920212223242526
  1. (function(root, mod) {
  2. if (typeof exports == "object" && typeof module == "object") // CommonJS
  3. return mod(exports);
  4. if (typeof define == "function" && define.amd) // AMD
  5. return define(["exports"], mod);
  6. mod((root.tern || (root.tern = {})).signal = {}); // Plain browser env
  7. })(this, function(exports) {
  8. function on(type, f) {
  9. var handlers = this._handlers || (this._handlers = Object.create(null));
  10. (handlers[type] || (handlers[type] = [])).push(f);
  11. }
  12. function off(type, f) {
  13. var arr = this._handlers && this._handlers[type];
  14. if (arr) for (var i = 0; i < arr.length; ++i)
  15. if (arr[i] == f) { arr.splice(i, 1); break; }
  16. }
  17. function signal(type, a1, a2, a3, a4) {
  18. var arr = this._handlers && this._handlers[type];
  19. if (arr) for (var i = 0; i < arr.length; ++i) arr[i].call(this, a1, a2, a3, a4);
  20. }
  21. exports.mixin = function(obj) {
  22. obj.on = on; obj.off = off; obj.signal = signal;
  23. return obj;
  24. };
  25. });