]> git.r.bdr.sh - rbdr/dotfiles/blob - atom/packages/ex-mode/node_modules/space-pen/node_modules/grim/node_modules/emissary/lib/subscriber.js
25016be7cc1bdb1e3332e5918e5c3eead235b500
[rbdr/dotfiles] / atom / packages / ex-mode / node_modules / space-pen / node_modules / grim / node_modules / emissary / lib / subscriber.js
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
7 Mixin = require('mixto');
8
9 Signal = null;
10
11 WeakMap = (_ref = global.WeakMap) != null ? _ref : require('es6-weak-map');
12
13 Subscription = require('./subscription');
14
15 module.exports = Subscriber = (function(_super) {
16 __extends(Subscriber, _super);
17
18 function Subscriber() {
19 _ref1 = Subscriber.__super__.constructor.apply(this, arguments);
20 return _ref1;
21 }
22
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");
27 }
28 eventEmitter[methodName].apply(eventEmitter, args);
29 eventNames = args[0];
30 callback = args[args.length - 1];
31 return this.addSubscription(new Subscription(eventEmitter, eventNames, callback));
32 };
33
34 Subscriber.prototype.addSubscription = function(subscription) {
35 var emitter;
36 if (this._subscriptions == null) {
37 this._subscriptions = [];
38 }
39 this._subscriptions.push(subscription);
40 emitter = subscription.emitter;
41 if (emitter != null) {
42 if (this._subscriptionsByObject == null) {
43 this._subscriptionsByObject = new WeakMap;
44 }
45 if (this._subscriptionsByObject.has(emitter)) {
46 this._subscriptionsByObject.get(emitter).push(subscription);
47 } else {
48 this._subscriptionsByObject.set(emitter, [subscription]);
49 }
50 }
51 return subscription;
52 };
53
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);
59 } else {
60 if (args.length === 1 && eventEmitterOrSubscription.isSignal) {
61 args.unshift('value');
62 }
63 return this.subscribeWith(eventEmitterOrSubscription, 'on', args);
64 }
65 };
66
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);
71 };
72
73 Subscriber.prototype.unsubscribe = function(object) {
74 var index, subscription, _i, _j, _len, _len1, _ref2, _ref3, _ref4, _ref5, _ref6, _ref7;
75 if (object != null) {
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();
81 } else {
82 subscription.off();
83 }
84 index = this._subscriptions.indexOf(subscription);
85 if (index >= 0) {
86 this._subscriptions.splice(index, 1);
87 }
88 }
89 return (_ref5 = this._subscriptionsByObject) != null ? _ref5["delete"](object) : void 0;
90 } else {
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();
96 } else {
97 subscription.off();
98 }
99 }
100 this._subscriptions = null;
101 return this._subscriptionsByObject = null;
102 }
103 };
104
105 return Subscriber;
106
107 })(Mixin);
108
109 }).call(this);