|
1 | 1 |
|
2 | 2 | const SubscribableMixin = { |
3 | | - |
4 | 3 | componentWillMount() { |
5 | | - this._subscribableSubscriptions = []; |
| 4 | + this._subscriptions = []; |
6 | 5 | }, |
7 | 6 |
|
8 | 7 | componentWillUnmount() { |
9 | | - this._subscribableSubscriptions.forEach( |
10 | | - (subscription) => subscription.remove() |
| 8 | + this._subscriptions.forEach( |
| 9 | + (subscription) => subscription.eventEmitter.removeListener(subscription.eventType, subscription.listener) |
11 | 10 | ); |
12 | | - this._subscribableSubscriptions = null; |
| 11 | + this._subscriptions = null; |
13 | 12 | }, |
14 | 13 |
|
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 | | - */ |
28 | 14 | 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 }); |
32 | 17 | } |
33 | 18 | }; |
34 | 19 |
|
|
0 commit comments