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
; },
7 Mixin
= require('mixto');
11 WeakMap
= (_ref
= global
.WeakMap
) != null ? _ref : require('es6-weak-map');
13 Subscription
= require('./subscription');
15 module
.exports
= Subscriber
= (function(_super
) {
16 __extends(Subscriber
, _super
);
18 function Subscriber() {
19 _ref1
= Subscriber
.__super__
.constructor.apply(this, arguments
);
23 Subscriber
.prototype.subscribeWith = function(eventEmitter
, methodName
, args
) {
24 var callback
, eventNames
;
25 if (eventEmitter
[methodName
] == null) {
26 throw new Error("Object does not have method '" + methodName
+ "' with which to subscribe");
28 eventEmitter
[methodName
].apply(eventEmitter
, args
);
30 callback
= args
[args
.length
- 1];
31 return this.addSubscription(new Subscription(eventEmitter
, eventNames
, callback
));
34 Subscriber
.prototype.addSubscription = function(subscription
) {
36 if (this._subscriptions
== null) {
37 this._subscriptions
= [];
39 this._subscriptions
.push(subscription
);
40 emitter
= subscription
.emitter
;
41 if (emitter
!= null) {
42 if (this._subscriptionsByObject
== null) {
43 this._subscriptionsByObject
= new WeakMap
;
45 if (this._subscriptionsByObject
.has(emitter
)) {
46 this._subscriptionsByObject
.get(emitter
).push(subscription
);
48 this._subscriptionsByObject
.set(emitter
, [subscription
]);
54 Subscriber
.prototype.subscribe = function() {
55 var args
, eventEmitterOrSubscription
;
56 eventEmitterOrSubscription
= arguments
[0], args
= 2 <= arguments
.length
? __slice
.call(arguments
, 1) : [];
57 if (args
.length
=== 0) {
58 return this.addSubscription(eventEmitterOrSubscription
);
60 if (args
.length
=== 1 && eventEmitterOrSubscription
.isSignal
) {
61 args
.unshift('value');
63 return this.subscribeWith(eventEmitterOrSubscription
, 'on', args
);
67 Subscriber
.prototype.subscribeToCommand = function() {
68 var args
, eventEmitter
;
69 eventEmitter
= arguments
[0], args
= 2 <= arguments
.length
? __slice
.call(arguments
, 1) : [];
70 return this.subscribeWith(eventEmitter
, 'command', args
);
73 Subscriber
.prototype.unsubscribe = function(object
) {
74 var index
, subscription
, _i
, _j
, _len
, _len1
, _ref2
, _ref3
, _ref4
, _ref5
, _ref6
, _ref7
;
76 _ref4
= (_ref2
= (_ref3
= this._subscriptionsByObject
) != null ? _ref3
.get(object
) : void 0) != null ? _ref2 : [];
77 for (_i
= 0, _len
= _ref4
.length
; _i
< _len
; _i
++) {
78 subscription
= _ref4
[_i
];
79 if (typeof subscription
.dispose
=== 'function') {
80 subscription
.dispose();
84 index
= this._subscriptions
.indexOf(subscription
);
86 this._subscriptions
.splice(index
, 1);
89 return (_ref5
= this._subscriptionsByObject
) != null ? _ref5
["delete"](object
) : void 0;
91 _ref7
= (_ref6
= this._subscriptions
) != null ? _ref6 : [];
92 for (_j
= 0, _len1
= _ref7
.length
; _j
< _len1
; _j
++) {
93 subscription
= _ref7
[_j
];
94 if (typeof subscription
.dispose
=== 'function') {
95 subscription
.dispose();
100 this._subscriptions
= null;
101 return this._subscriptionsByObject
= null;