4
1

subscription.js 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. (function() {
  2. var Emitter, Subscription,
  3. __hasProp = {}.hasOwnProperty,
  4. __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; };
  5. Emitter = require('./emitter');
  6. module.exports = Subscription = (function(_super) {
  7. __extends(Subscription, _super);
  8. Subscription.prototype.cancelled = false;
  9. function Subscription(emitter, eventNames, handler) {
  10. this.emitter = emitter;
  11. this.eventNames = eventNames;
  12. this.handler = handler;
  13. }
  14. Subscription.prototype.off = function() {
  15. return this.dispose();
  16. };
  17. Subscription.prototype.dispose = function() {
  18. var unsubscribe, _ref;
  19. if (this.cancelled) {
  20. return;
  21. }
  22. unsubscribe = (_ref = this.emitter.off) != null ? _ref : this.emitter.removeListener;
  23. unsubscribe.call(this.emitter, this.eventNames, this.handler);
  24. this.emitter = null;
  25. this.handler = null;
  26. this.cancelled = true;
  27. return this.emit('cancelled');
  28. };
  29. return Subscription;
  30. })(Emitter);
  31. }).call(this);