it('removes all subscriptions of a certain type', function() { var subscriber = new EventSubscriptionVendor(); subscriber.addSubscription('type1', new EventSubscription(subscriber)); subscriber.addSubscription('type1', new EventSubscription(subscriber)); expect(subscriber.getSubscriptionsForType('type1').length).toBe(2); subscriber.removeAllSubscriptions('type1'); expect(subscriber.getSubscriptionsForType('type1')) .toBe(undefined); });
/** * Adds a listener to be invoked when events of the specified type are * emitted. An optional calling context may be provided. The data arguments * emitted will be passed to the listener function. * * TODO: Annotate the listener arg's type. This is tricky because listeners * can be invoked with varargs. * * @param {string} eventType - Name of the event to listen to * @param {function} listener - Function to invoke when the specified event is * emitted * @param {*} context - Optional context object to use when invoking the * listener */ addListener( eventType: string, listener: Function, context: ?Object): EmitterSubscription { return (this._subscriber.addSubscription( eventType, new EmitterSubscription(this, this._subscriber, listener, context) ) : any); }
it('removes a subscription', function() { var subscriber = new EventSubscriptionVendor(); var subscription1 = new EventSubscription(subscriber); subscription1.is1 = true; subscriber.addSubscription('type1', subscription1); var subscription2 = new EventSubscription(subscriber); subscription2.is1 = false; subscriber.addSubscription('type1', subscription2); expect(subscriber.getSubscriptionsForType('type1').length).toBe(2); subscriber.removeSubscription(subscription1); var subscriptions = subscriber.getSubscriptionsForType('type1'); var allempty = true; for (var key in subscriptions) { if (subscriptions[key]) { allempty = false; expect(subscriptions[key].is1).toBeFalsy(); } } expect(allempty).toBeFalsy(); });
it('adds subscriptions keyed on type', function() { var subscriber = new EventSubscriptionVendor(); subscriber.addSubscription('type1', new EventSubscription(subscriber)); subscriber.addSubscription('type2', new EventSubscription(subscriber)); expect(subscriber.getSubscriptionsForType('type1').length).toBe(1); });