2
0

subscriber.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. (function() {
  2. var Mixin, Signal, Subscriber, Subscription, WeakMap, _ref, _ref1,
  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. __slice = [].slice;
  6. Mixin = require('mixto');
  7. Signal = null;
  8. WeakMap = (_ref = global.WeakMap) != null ? _ref : require('es6-weak-map');
  9. Subscription = require('./subscription');
  10. module.exports = Subscriber = (function(_super) {
  11. __extends(Subscriber, _super);
  12. function Subscriber() {
  13. _ref1 = Subscriber.__super__.constructor.apply(this, arguments);
  14. return _ref1;
  15. }
  16. Subscriber.prototype.subscribeWith = function(eventEmitter, methodName, args) {
  17. var callback, eventNames;
  18. if (eventEmitter[methodName] == null) {
  19. throw new Error("Object does not have method '" + methodName + "' with which to subscribe");
  20. }
  21. eventEmitter[methodName].apply(eventEmitter, args);
  22. eventNames = args[0];
  23. callback = args[args.length - 1];
  24. return this.addSubscription(new Subscription(eventEmitter, eventNames, callback));
  25. };
  26. Subscriber.prototype.addSubscription = function(subscription) {
  27. var emitter;
  28. if (this._subscriptions == null) {
  29. this._subscriptions = [];
  30. }
  31. this._subscriptions.push(subscription);
  32. emitter = subscription.emitter;
  33. if (emitter != null) {
  34. if (this._subscriptionsByObject == null) {
  35. this._subscriptionsByObject = new WeakMap;
  36. }
  37. if (this._subscriptionsByObject.has(emitter)) {
  38. this._subscriptionsByObject.get(emitter).push(subscription);
  39. } else {
  40. this._subscriptionsByObject.set(emitter, [subscription]);
  41. }
  42. }
  43. return subscription;
  44. };
  45. Subscriber.prototype.subscribe = function() {
  46. var args, eventEmitterOrSubscription;
  47. eventEmitterOrSubscription = arguments[0], args = 2 <= arguments.length ? __slice.call(arguments, 1) : [];
  48. if (args.length === 0) {
  49. return this.addSubscription(eventEmitterOrSubscription);
  50. } else {
  51. if (args.length === 1 && eventEmitterOrSubscription.isSignal) {
  52. args.unshift('value');
  53. }
  54. return this.subscribeWith(eventEmitterOrSubscription, 'on', args);
  55. }
  56. };
  57. Subscriber.prototype.subscribeToCommand = function() {
  58. var args, eventEmitter;
  59. eventEmitter = arguments[0], args = 2 <= arguments.length ? __slice.call(arguments, 1) : [];
  60. return this.subscribeWith(eventEmitter, 'command', args);
  61. };
  62. Subscriber.prototype.unsubscribe = function(object) {
  63. var index, subscription, _i, _j, _len, _len1, _ref2, _ref3, _ref4, _ref5, _ref6, _ref7;
  64. if (object != null) {
  65. _ref4 = (_ref2 = (_ref3 = this._subscriptionsByObject) != null ? _ref3.get(object) : void 0) != null ? _ref2 : [];
  66. for (_i = 0, _len = _ref4.length; _i < _len; _i++) {
  67. subscription = _ref4[_i];
  68. if (typeof subscription.dispose === 'function') {
  69. subscription.dispose();
  70. } else {
  71. subscription.off();
  72. }
  73. index = this._subscriptions.indexOf(subscription);
  74. if (index >= 0) {
  75. this._subscriptions.splice(index, 1);
  76. }
  77. }
  78. return (_ref5 = this._subscriptionsByObject) != null ? _ref5["delete"](object) : void 0;
  79. } else {
  80. _ref7 = (_ref6 = this._subscriptions) != null ? _ref6 : [];
  81. for (_j = 0, _len1 = _ref7.length; _j < _len1; _j++) {
  82. subscription = _ref7[_j];
  83. if (typeof subscription.dispose === 'function') {
  84. subscription.dispose();
  85. } else {
  86. subscription.off();
  87. }
  88. }
  89. this._subscriptions = null;
  90. return this._subscriptionsByObject = null;
  91. }
  92. };
  93. return Subscriber;
  94. })(Mixin);
  95. }).call(this);