-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathreact-native-event-bridge-enhance.js
117 lines (94 loc) · 3.01 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
/**
* 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, { Component } from 'react';
import { EmitterSubscription } from 'react-native';
import EventBridge from './index.js';
const enhanceForEventsSupport = (ComposedComponent: Component<any>) => {
let _key = 'EventsBridgeEnhance_Listeners';
return class extends (ComposedComponent:any) {
static contextTypes = {
rootTag: React.PropTypes.number,
};
componentWillUnmount () {
super.componentWillUnmount && super.componentWillUnmount();
let {
[ _key ]: listeners,
} = this;
listeners && listeners.forEach((listener) => {
listener.remove();
});
this[ _key ] = null;
}
registerEventListener(...listeners:Array<EmitterSubscription>) {
let { [ _key ]: listenerList } = this;
if (!listenerList) {
this[ _key ] = listeners;
} else {
listenerList.push(...listeners);
}
return this;
}
}
};
export default enhanceForEventsSupport;
/********************************/
/* Experimental
/********************************/
const enhanceForEventsSupportEnhanced = (ComposedComponent: any) => {
return class extends (ComposedComponent:any) {
_subscribableSubscriptions: ?Array<EmitterSubscription>;
static contextTypes = {
rootTag: React.PropTypes.number,
};
componentWillMount() {
this._subscribableSubscriptions = [];
}
componentWillUnmount() {
this._subscribableSubscriptions && this._subscribableSubscriptions.forEach(
(subscription) => subscription.remove()
);
this._subscribableSubscriptions = null;
}
registerEventListener(callback: any) {
this._subscribableSubscriptions && this._subscribableSubscriptions.push(
EventBridge.addEventListener(this, callback)
);
}
}
};
function enhanceForEventsSupportDecorator() {
return function (DecoratedComponent: Component<any>) {
let _key = 'ViewControllerEventsListenerEnhance_listeners';
return class extends (DecoratedComponent:any) {
static contextTypes = {
rootTag: React.PropTypes.number,
};
componentWillUnmount () {
super.componentWillUnmount && super.componentWillUnmount();
let {
[ _key ]: listeners,
} = this;
listeners && listeners.forEach((listener) => {
listener.remove();
});
this[ _key ] = null;
}
registerEventListener(...listeners:Array<EmitterSubscription>) {
let { [ _key ]: listenerList } = this;
if (!listenerList) {
this[ _key ] = listeners;
} else {
listenerList.push(...listeners);
}
return this;
}
}
}
}
export { enhanceForEventsSupportDecorator, enhanceForEventsSupportEnhanced };