Skip to content

Commit

Permalink
Merge pull request #4 from Root-App/rmc-subscribable-fix
Browse files Browse the repository at this point in the history
Updates Subscribable.Mixin to support unmounting
  • Loading branch information
bob-carson authored Jan 4, 2017
2 parents 959f7c0 + 0c2ee9e commit 8dc2a62
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 21 deletions.
27 changes: 6 additions & 21 deletions src/mixins/Subscribable.js
Original file line number Diff line number Diff line change
@@ -1,34 +1,19 @@

const SubscribableMixin = {

componentWillMount() {
this._subscribableSubscriptions = [];
this._subscriptions = [];
},

componentWillUnmount() {
this._subscribableSubscriptions.forEach(
(subscription) => subscription.remove()
this._subscriptions.forEach(
(subscription) => subscription.eventEmitter.removeListener(subscription.eventType, subscription.listener)
);
this._subscribableSubscriptions = null;
this._subscriptions = null;
},

/**
* Special form of calling `addListener` that *guarantees* that a
* subscription *must* be tied to a component instance, and therefore will
* be cleaned up when the component is unmounted. It is impossible to create
* the subscription and pass it in - this method must be the one to create
* the subscription and therefore can guarantee it is retained in a way that
* will be cleaned up.
*
* @param {EventEmitter} eventEmitter emitter to subscribe to.
* @param {string} eventType Type of event to listen to.
* @param {function} listener Function to invoke when event occurs.
* @param {object} context Object to use as listener context.
*/
addListenerOn(eventEmitter, eventType, listener, context) {
this._subscribableSubscriptions.push(
eventEmitter.addListener(eventType, listener, context)
);
eventEmitter.addListener(eventType, listener, context);
this._subscriptions.push({ eventEmitter, eventType, listener });
}
};

Expand Down
25 changes: 25 additions & 0 deletions test/mixins/Subscribable.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import React from 'react';
import { expect } from 'chai';
import Subscribable from '../../src/mixins/Subscribable.js';
import DeviceEventEmitter from '../../src/plugins/DeviceEventEmitter.js';

describe('Subscribable.Mixin', () => {
it('Can mount and unmount with a DeviceEventEmitter subscribed. Does not interupt existing events.', () => {
const SubscribableClass = React.createClass({
mixins: [Subscribable.Mixin],
render() {
return null;
},
});
const subscribableComponent = new SubscribableClass();
const existingEventType = 'Existing Event';
DeviceEventEmitter.addListener(existingEventType, () => {}, context);
expect(DeviceEventEmitter.listeners(existingEventType)).to.have.length(1);

subscribableComponent.componentWillMount();
subscribableComponent.addListenerOn(DeviceEventEmitter, 'Test Event Type', () => {});
subscribableComponent.componentWillUnmount();

expect(DeviceEventEmitter.listeners(existingEventType)).to.have.length(1);
});
});

0 comments on commit 8dc2a62

Please sign in to comment.