Skip to content

Commit 8dc2a62

Browse files
authored
Merge pull request #4 from Root-App/rmc-subscribable-fix
Updates Subscribable.Mixin to support unmounting
2 parents 959f7c0 + 0c2ee9e commit 8dc2a62

File tree

2 files changed

+31
-21
lines changed

2 files changed

+31
-21
lines changed

src/mixins/Subscribable.js

Lines changed: 6 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,19 @@
11

22
const SubscribableMixin = {
3-
43
componentWillMount() {
5-
this._subscribableSubscriptions = [];
4+
this._subscriptions = [];
65
},
76

87
componentWillUnmount() {
9-
this._subscribableSubscriptions.forEach(
10-
(subscription) => subscription.remove()
8+
this._subscriptions.forEach(
9+
(subscription) => subscription.eventEmitter.removeListener(subscription.eventType, subscription.listener)
1110
);
12-
this._subscribableSubscriptions = null;
11+
this._subscriptions = null;
1312
},
1413

15-
/**
16-
* Special form of calling `addListener` that *guarantees* that a
17-
* subscription *must* be tied to a component instance, and therefore will
18-
* be cleaned up when the component is unmounted. It is impossible to create
19-
* the subscription and pass it in - this method must be the one to create
20-
* the subscription and therefore can guarantee it is retained in a way that
21-
* will be cleaned up.
22-
*
23-
* @param {EventEmitter} eventEmitter emitter to subscribe to.
24-
* @param {string} eventType Type of event to listen to.
25-
* @param {function} listener Function to invoke when event occurs.
26-
* @param {object} context Object to use as listener context.
27-
*/
2814
addListenerOn(eventEmitter, eventType, listener, context) {
29-
this._subscribableSubscriptions.push(
30-
eventEmitter.addListener(eventType, listener, context)
31-
);
15+
eventEmitter.addListener(eventType, listener, context);
16+
this._subscriptions.push({ eventEmitter, eventType, listener });
3217
}
3318
};
3419

test/mixins/Subscribable.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import React from 'react';
2+
import { expect } from 'chai';
3+
import Subscribable from '../../src/mixins/Subscribable.js';
4+
import DeviceEventEmitter from '../../src/plugins/DeviceEventEmitter.js';
5+
6+
describe('Subscribable.Mixin', () => {
7+
it('Can mount and unmount with a DeviceEventEmitter subscribed. Does not interupt existing events.', () => {
8+
const SubscribableClass = React.createClass({
9+
mixins: [Subscribable.Mixin],
10+
render() {
11+
return null;
12+
},
13+
});
14+
const subscribableComponent = new SubscribableClass();
15+
const existingEventType = 'Existing Event';
16+
DeviceEventEmitter.addListener(existingEventType, () => {}, context);
17+
expect(DeviceEventEmitter.listeners(existingEventType)).to.have.length(1);
18+
19+
subscribableComponent.componentWillMount();
20+
subscribableComponent.addListenerOn(DeviceEventEmitter, 'Test Event Type', () => {});
21+
subscribableComponent.componentWillUnmount();
22+
23+
expect(DeviceEventEmitter.listeners(existingEventType)).to.have.length(1);
24+
});
25+
});

0 commit comments

Comments
 (0)