@@ -19,6 +19,16 @@ const RCTAppState = NativeModules.AppState;
19
19
const logError = require ( 'logError' ) ;
20
20
const invariant = require ( 'fbjs/lib/invariant' ) ;
21
21
22
+ import type { Layout } from 'CoreEventTypes' ;
23
+
24
+ type Screen = { |
25
+ + rootTag : number ,
26
+ + bounds : Layout ,
27
+ + scale : number ,
28
+ | } ;
29
+
30
+ const eventTypes = [ 'change' , 'memoryWarning' , 'rootViewWillAppear' , 'windowDidChangeScreen' ] ;
31
+
22
32
/**
23
33
* `AppState` can tell you if the app is in the foreground or background,
24
34
* and notify you when the state changes.
@@ -30,15 +40,16 @@ class AppState extends NativeEventEmitter {
30
40
_eventHandlers : Object ;
31
41
currentState : ?string ;
32
42
isAvailable : boolean = true ;
43
+ windows : { [ rootTag : number ] : Screen } ;
33
44
34
45
constructor ( ) {
35
46
super ( RCTAppState ) ;
36
47
37
48
this . isAvailable = true ;
38
- this . _eventHandlers = {
39
- change : new Map ( ) ,
40
- memoryWarning : new Map ( ) ,
41
- } ;
49
+ this . _eventHandlers = eventTypes . reduce ( ( out , type ) => {
50
+ out [ type ] = new Map ( ) ;
51
+ return out ;
52
+ } , { } ) ;
42
53
43
54
// TODO: Remove the 'active' fallback after `initialAppState` is exported by
44
55
// the Android implementation.
@@ -58,6 +69,18 @@ class AppState extends NativeEventEmitter {
58
69
}
59
70
) ;
60
71
72
+ this . windows = RCTAppState . windows . reduce ( ( acc , state ) => {
73
+ acc [ state . rootTag ] = state ;
74
+ return acc ;
75
+ } , { } ) ;
76
+
77
+ const onWindowChange = state => {
78
+ this . windows [ state . rootTag ] = state ;
79
+ }
80
+
81
+ this . addListener ( 'rootViewWillAppear' , onWindowChange ) ;
82
+ this . addListener ( 'windowDidChangeScreen' , onWindowChange ) ;
83
+
61
84
// TODO: see above - this request just populates the value of `currentState`
62
85
// when the module is first initialized. Would be better to get rid of the
63
86
// prop and expose `getCurrentAppState` method directly.
@@ -87,7 +110,7 @@ class AppState extends NativeEventEmitter {
87
110
handler : Function
88
111
) {
89
112
invariant (
90
- [ 'change' , 'memoryWarning' ] . indexOf ( type ) !== - 1 ,
113
+ eventTypes . indexOf ( type ) !== - 1 ,
91
114
'Trying to subscribe to unknown event: "%s"' , type
92
115
) ;
93
116
if ( type === 'change' ) {
@@ -97,11 +120,8 @@ class AppState extends NativeEventEmitter {
97
120
handler ( appStateData . app_state ) ;
98
121
}
99
122
) ) ;
100
- } else if ( type === 'memoryWarning' ) {
101
- this . _eventHandlers [ type ] . set ( handler , this . addListener (
102
- 'memoryWarning' ,
103
- handler
104
- ) ) ;
123
+ } else {
124
+ this . _eventHandlers [ type ] . set ( handler , this . addListener ( type , handler ) ) ;
105
125
}
106
126
}
107
127
@@ -115,7 +135,7 @@ class AppState extends NativeEventEmitter {
115
135
handler : Function
116
136
) {
117
137
invariant (
118
- [ 'change' , 'memoryWarning' ] . indexOf ( type ) !== - 1 ,
138
+ eventTypes . indexOf ( type ) !== - 1 ,
119
139
'Trying to remove listener for unknown event: "%s"' , type
120
140
) ;
121
141
if ( ! this . _eventHandlers [ type ] . has ( handler ) ) {
0 commit comments