forked from steipete/react-native-event-bridge
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreact-native-event-bridge-enhance.js
120 lines (101 loc) · 3.1 KB
/
react-native-event-bridge-enhance.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/**
* react-native-event-bridge-enhance
* @flow
*
*/
// Mixin to provide the boilerplate that is needed for a event listener component
// as well as also supports automatic removing of subscription for event listeners
// if the listener is registered via the registerEventListener method.
import React from 'react';
import type EmitterSubscription from 'EmitterSubscription';
import EventBridge from './index';
const enhanceForEventsSupport = (ComposedComponent: React.Component<any>) => {
const _key = 'EventsBridgeEnhance_Listeners';
return class extends (ComposedComponent: any) {
static contextTypes = {
rootTag: React.PropTypes.number,
};
componentWillUnmount() {
if (super.componentWillUnmount) {
super.componentWillUnmount();
}
const { [_key]: listeners } = this;
if (listeners) {
listeners.forEach(listener => {
listener.remove();
});
}
this[_key] = null;
}
registerEventListener(...listeners: Array<EmitterSubscription>) {
const { [_key]: listenerList } = this;
if (!listenerList) {
this[_key] = listeners;
} else {
listenerList.push(...listeners);
}
return this;
}
};
};
export default enhanceForEventsSupport;
/* Experimental
/********************************/
const enhanceForEventsSupportEnhanced = (ComposedComponent: any) =>
class extends (ComposedComponent: any) {
_subscribableSubscriptions: ?Array<EmitterSubscription>;
static contextTypes = {
rootTag: React.PropTypes.number,
};
componentWillMount() {
this._subscribableSubscriptions = [];
}
componentWillUnmount() {
if (this._subscribableSubscriptions) {
this._subscribableSubscriptions.forEach(subscription =>
subscription.remove()
);
}
this._subscribableSubscriptions = null;
}
registerEventListener(callback: any) {
if (this._subscribableSubscriptions) {
this._subscribableSubscriptions.push(
EventBridge.addEventListener(this, callback)
);
}
}
};
function enhanceForEventsSupportDecorator() {
// eslint-disable-next-line func-names
return function(DecoratedComponent: React.Component<any>) {
const _key = 'ViewControllerEventsListenerEnhance_listeners';
return class extends (DecoratedComponent: any) {
static contextTypes = {
rootTag: React.PropTypes.number,
};
componentWillUnmount() {
if (super.componentWillUnmount) {
super.componentWillUnmount();
}
const { [_key]: listeners } = this;
if (listeners) {
listeners.forEach(listener => {
listener.remove();
});
}
this[_key] = null;
}
registerEventListener(...listeners: Array<EmitterSubscription>) {
const { [_key]: listenerList } = this;
if (!listenerList) {
this[_key] = listeners;
} else {
listenerList.push(...listeners);
}
return this;
}
};
};
}
export { enhanceForEventsSupportDecorator, enhanceForEventsSupportEnhanced };