forked from facebook/react-native
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAppState.js
More file actions
181 lines (157 loc) · 5.37 KB
/
AppState.js
File metadata and controls
181 lines (157 loc) · 5.37 KB
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @format
* @flow
*/
'use strict';
const EventEmitter = require('../vendor/emitter/EventEmitter');
const NativeEventEmitter = require('../EventEmitter/NativeEventEmitter');
const invariant = require('invariant');
const logError = require('../Utilities/logError');
import NativeAppState from './NativeAppState';
/**
* `AppState` can tell you if the app is in the foreground or background,
* and notify you when the state changes.
*
* See https://reactnative.dev/docs/appstate.html
*/
class AppState extends NativeEventEmitter {
_eventHandlers: Object;
_supportedEvents = ['change', 'memoryWarning', 'blur', 'focus'];
currentState: ?string;
isAvailable: boolean;
constructor() {
super(NativeAppState);
this.isAvailable = true;
this._eventHandlers = this._supportedEvents.reduce((handlers, key) => {
handlers[key] = new Map();
return handlers;
}, {});
this.currentState = NativeAppState.getConstants().initialAppState;
let eventUpdated = false;
// TODO: this is a terrible solution - in order to ensure `currentState`
// prop is up to date, we have to register an observer that updates it
// whenever the state changes, even if nobody cares. We should just
// deprecate the `currentState` property and get rid of this.
this.addListener('appStateDidChange', appStateData => {
eventUpdated = true;
this.currentState = appStateData.app_state;
});
// TODO: see above - this request just populates the value of `currentState`
// when the module is first initialized. Would be better to get rid of the
// prop and expose `getCurrentAppState` method directly.
NativeAppState.getCurrentAppState(appStateData => {
// It's possible that the state will have changed here & listeners need to be notified
if (!eventUpdated && this.currentState !== appStateData.app_state) {
this.currentState = appStateData.app_state;
this.emit('appStateDidChange', appStateData);
}
}, logError);
}
// TODO: now that AppState is a subclass of NativeEventEmitter, we could
// deprecate `addEventListener` and `removeEventListener` and just use
// addListener` and `listener.remove()` directly. That will be a breaking
// change though, as both the method and event names are different
// (addListener events are currently required to be globally unique).
/**
* Add a handler to AppState changes by listening to the `change` event type
* and providing the handler.
*
* See https://reactnative.dev/docs/appstate.html#addeventlistener
*/
addEventListener(type: string, handler: Function) {
invariant(
this._supportedEvents.indexOf(type) !== -1,
'Trying to subscribe to unknown event: "%s"',
type,
);
switch (type) {
case 'change': {
this._eventHandlers[type].set(
handler,
this.addListener('appStateDidChange', appStateData => {
handler(appStateData.app_state);
}),
);
break;
}
case 'memoryWarning': {
this._eventHandlers[type].set(
handler,
this.addListener('memoryWarning', handler),
);
break;
}
case 'blur':
case 'focus': {
this._eventHandlers[type].set(
handler,
this.addListener('appStateFocusChange', hasFocus => {
if (type === 'blur' && !hasFocus) {
handler();
}
if (type === 'focus' && hasFocus) {
handler();
}
}),
);
}
}
}
/**
* Remove a handler by passing the `change` event type and the handler.
*
* See https://reactnative.dev/docs/appstate.html#removeeventlistener
*/
removeEventListener(type: string, handler: Function) {
invariant(
this._supportedEvents.indexOf(type) !== -1,
'Trying to remove listener for unknown event: "%s"',
type,
);
if (!this._eventHandlers[type].has(handler)) {
return;
}
this._eventHandlers[type].get(handler).remove();
this._eventHandlers[type].delete(handler);
}
}
function throwMissingNativeModule() {
invariant(
false,
'Cannot use AppState module when native RCTAppState is not included in the build.\n' +
'Either include it, or check AppState.isAvailable before calling any methods.',
);
}
class MissingNativeAppStateShim extends EventEmitter {
// AppState
isAvailable: boolean = false;
currentState: ?string = null;
addEventListener(type: string, handler: Function) {
throwMissingNativeModule();
}
removeEventListener(type: string, handler: Function) {
throwMissingNativeModule();
}
// EventEmitter
addListener() {
throwMissingNativeModule();
}
removeAllListeners() {
throwMissingNativeModule();
}
removeSubscription() {
throwMissingNativeModule();
}
}
// This module depends on the native `RCTAppState` module. If you don't include it,
// `AppState.isAvailable` will return `false`, and any method calls will throw.
// We reassign the class variable to keep the autodoc generator happy.
const AppStateInstance: AppState | MissingNativeAppStateShim = NativeAppState
? new AppState()
: new MissingNativeAppStateShim();
module.exports = AppStateInstance;