-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathAppEventListenerEnhance.js
41 lines (31 loc) · 1.13 KB
/
AppEventListenerEnhance.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
/*
* An AppEventListenerEnhance for React Native app
* which provides addEventListener functions that are safely cleaned up when the component unmounts
* https://github.com/react-native-component/react-native-smart-app-event-listener-enhance/
* Released under the MIT license
* Copyright (c) 2016 react-native-component <[email protected]>
*/
export default AppEventListenerEnhance = (ComposedComponent) => {
let _key = 'AppEventListenerEnhance_listeners'
return class extends ComposedComponent {
componentWillUnmount () {
super.componentWillUnmount && super.componentWillUnmount()
let {
[ _key ]: listeners,
} = this
listeners && listeners.forEach((listener) => {
listener.remove()
})
this[ _key ] = null
}
addAppEventListener = (...listeners) => {
let { [ _key ]: listenerList } = this
if (!listenerList) {
this[ _key ] = listeners
} else {
listenerList.push(...listeners)
}
return this
}
}
}