2016-01-21 22:43:51 +03:00
|
|
|
/**
|
2018-09-12 01:27:47 +03:00
|
|
|
* Copyright (c) Facebook, Inc. and its affiliates.
|
2016-01-21 22:43:51 +03:00
|
|
|
*
|
2018-02-17 05:24:55 +03:00
|
|
|
* This source code is licensed under the MIT license found in the
|
|
|
|
* LICENSE file in the root directory of this source tree.
|
2016-01-21 22:43:51 +03:00
|
|
|
*
|
2018-05-11 05:06:46 +03:00
|
|
|
* @format
|
2016-01-21 22:43:51 +03:00
|
|
|
* @flow
|
|
|
|
*/
|
2018-05-11 05:06:46 +03:00
|
|
|
|
2016-01-21 22:43:51 +03:00
|
|
|
'use strict';
|
|
|
|
|
2020-06-27 12:15:58 +03:00
|
|
|
import NativeEventEmitter from '../EventEmitter/NativeEventEmitter';
|
|
|
|
import logError from '../Utilities/logError';
|
2020-06-23 23:43:04 +03:00
|
|
|
import EventEmitter from '../vendor/emitter/EventEmitter';
|
2019-10-22 05:09:51 +03:00
|
|
|
import NativeAppState from './NativeAppState';
|
2020-06-27 12:15:58 +03:00
|
|
|
import invariant from 'invariant';
|
2016-01-21 22:43:51 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* `AppState` can tell you if the app is in the foreground or background,
|
|
|
|
* and notify you when the state changes.
|
|
|
|
*
|
2020-02-25 00:06:47 +03:00
|
|
|
* See https://reactnative.dev/docs/appstate.html
|
2016-01-21 22:43:51 +03:00
|
|
|
*/
|
2016-05-23 19:08:51 +03:00
|
|
|
class AppState extends NativeEventEmitter {
|
|
|
|
_eventHandlers: Object;
|
2019-06-06 01:59:46 +03:00
|
|
|
_supportedEvents = ['change', 'memoryWarning', 'blur', 'focus'];
|
2016-05-23 19:08:51 +03:00
|
|
|
currentState: ?string;
|
2019-04-15 20:32:46 +03:00
|
|
|
isAvailable: boolean;
|
2016-05-23 19:08:51 +03:00
|
|
|
|
|
|
|
constructor() {
|
2019-05-17 02:49:52 +03:00
|
|
|
super(NativeAppState);
|
2016-05-23 19:08:51 +03:00
|
|
|
|
2017-02-21 19:55:45 +03:00
|
|
|
this.isAvailable = true;
|
2019-06-06 01:59:46 +03:00
|
|
|
this._eventHandlers = this._supportedEvents.reduce((handlers, key) => {
|
|
|
|
handlers[key] = new Map();
|
|
|
|
return handlers;
|
|
|
|
}, {});
|
2016-05-23 19:08:51 +03:00
|
|
|
|
2019-05-17 02:49:52 +03:00
|
|
|
this.currentState = NativeAppState.getConstants().initialAppState;
|
2016-07-13 09:24:27 +03:00
|
|
|
|
2017-08-18 23:08:56 +03:00
|
|
|
let eventUpdated = false;
|
|
|
|
|
2017-11-21 00:07:10 +03:00
|
|
|
// TODO: this is a terrible solution - in order to ensure `currentState`
|
2018-02-22 18:04:35 +03:00
|
|
|
// 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
|
2017-11-21 00:07:10 +03:00
|
|
|
// deprecate the `currentState` property and get rid of this.
|
2020-03-25 07:35:58 +03:00
|
|
|
this.addListener('appStateDidChange', appStateData => {
|
2018-05-11 05:06:46 +03:00
|
|
|
eventUpdated = true;
|
|
|
|
this.currentState = appStateData.app_state;
|
|
|
|
});
|
2016-07-13 09:24:27 +03:00
|
|
|
|
2016-05-23 19:08:51 +03:00
|
|
|
// TODO: see above - this request just populates the value of `currentState`
|
2017-11-21 00:07:10 +03:00
|
|
|
// when the module is first initialized. Would be better to get rid of the
|
|
|
|
// prop and expose `getCurrentAppState` method directly.
|
2020-03-25 07:35:58 +03:00
|
|
|
NativeAppState.getCurrentAppState(appStateData => {
|
2018-05-11 05:06:46 +03:00
|
|
|
// 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);
|
2016-05-23 19:08:51 +03:00
|
|
|
}
|
|
|
|
|
2017-11-21 00:07:10 +03:00
|
|
|
// TODO: now that AppState is a subclass of NativeEventEmitter, we could
|
2018-02-22 18:04:35 +03:00
|
|
|
// deprecate `addEventListener` and `removeEventListener` and just use
|
|
|
|
// addListener` and `listener.remove()` directly. That will be a breaking
|
2017-11-21 00:07:10 +03:00
|
|
|
// change though, as both the method and event names are different
|
|
|
|
// (addListener events are currently required to be globally unique).
|
2018-05-11 05:06:46 +03:00
|
|
|
/**
|
2016-01-21 22:43:51 +03:00
|
|
|
* Add a handler to AppState changes by listening to the `change` event type
|
2017-11-21 00:07:10 +03:00
|
|
|
* and providing the handler.
|
2018-02-22 18:04:35 +03:00
|
|
|
*
|
2020-02-25 00:06:47 +03:00
|
|
|
* See https://reactnative.dev/docs/appstate.html#addeventlistener
|
2016-01-21 22:43:51 +03:00
|
|
|
*/
|
2018-05-11 05:06:46 +03:00
|
|
|
addEventListener(type: string, handler: Function) {
|
2016-01-21 22:43:51 +03:00
|
|
|
invariant(
|
2019-06-06 01:59:46 +03:00
|
|
|
this._supportedEvents.indexOf(type) !== -1,
|
2018-05-11 05:06:46 +03:00
|
|
|
'Trying to subscribe to unknown event: "%s"',
|
|
|
|
type,
|
2016-01-21 22:43:51 +03:00
|
|
|
);
|
2019-06-06 01:59:46 +03:00
|
|
|
|
|
|
|
switch (type) {
|
|
|
|
case 'change': {
|
|
|
|
this._eventHandlers[type].set(
|
|
|
|
handler,
|
2020-03-25 07:35:58 +03:00
|
|
|
this.addListener('appStateDidChange', appStateData => {
|
2019-06-06 01:59:46 +03:00
|
|
|
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,
|
2020-03-25 07:35:58 +03:00
|
|
|
this.addListener('appStateFocusChange', hasFocus => {
|
2019-06-06 01:59:46 +03:00
|
|
|
if (type === 'blur' && !hasFocus) {
|
|
|
|
handler();
|
|
|
|
}
|
|
|
|
if (type === 'focus' && hasFocus) {
|
|
|
|
handler();
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
}
|
2016-01-21 22:43:51 +03:00
|
|
|
}
|
2016-05-23 19:08:51 +03:00
|
|
|
}
|
2016-01-21 22:43:51 +03:00
|
|
|
|
|
|
|
/**
|
2017-11-21 00:07:10 +03:00
|
|
|
* Remove a handler by passing the `change` event type and the handler.
|
2018-02-22 18:04:35 +03:00
|
|
|
*
|
2020-02-25 00:06:47 +03:00
|
|
|
* See https://reactnative.dev/docs/appstate.html#removeeventlistener
|
2016-01-21 22:43:51 +03:00
|
|
|
*/
|
2018-05-11 05:06:46 +03:00
|
|
|
removeEventListener(type: string, handler: Function) {
|
2016-01-21 22:43:51 +03:00
|
|
|
invariant(
|
2019-06-06 01:59:46 +03:00
|
|
|
this._supportedEvents.indexOf(type) !== -1,
|
2018-05-11 05:06:46 +03:00
|
|
|
'Trying to remove listener for unknown event: "%s"',
|
|
|
|
type,
|
2016-01-21 22:43:51 +03:00
|
|
|
);
|
2016-05-23 19:08:51 +03:00
|
|
|
if (!this._eventHandlers[type].has(handler)) {
|
2016-01-21 22:43:51 +03:00
|
|
|
return;
|
|
|
|
}
|
2016-05-23 19:08:51 +03:00
|
|
|
this._eventHandlers[type].get(handler).remove();
|
|
|
|
this._eventHandlers[type].delete(handler);
|
2016-01-21 22:43:51 +03:00
|
|
|
}
|
2016-08-09 16:32:41 +03:00
|
|
|
}
|
2016-01-21 22:43:51 +03:00
|
|
|
|
2019-04-15 20:32:46 +03:00
|
|
|
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.',
|
|
|
|
);
|
|
|
|
}
|
2017-02-17 20:49:35 +03:00
|
|
|
|
2019-04-15 20:32:46 +03:00
|
|
|
class MissingNativeAppStateShim extends EventEmitter {
|
|
|
|
// AppState
|
|
|
|
isAvailable: boolean = false;
|
|
|
|
currentState: ?string = null;
|
2017-02-17 20:49:35 +03:00
|
|
|
|
2019-10-22 05:09:51 +03:00
|
|
|
addEventListener(type: string, handler: Function) {
|
2019-04-15 20:32:46 +03:00
|
|
|
throwMissingNativeModule();
|
|
|
|
}
|
2017-02-17 20:49:35 +03:00
|
|
|
|
2019-10-22 05:09:51 +03:00
|
|
|
removeEventListener(type: string, handler: Function) {
|
2019-04-15 20:32:46 +03:00
|
|
|
throwMissingNativeModule();
|
2017-02-17 20:49:35 +03:00
|
|
|
}
|
|
|
|
|
2019-04-15 20:32:46 +03:00
|
|
|
// 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.
|
2019-10-22 05:09:51 +03:00
|
|
|
const AppStateInstance: AppState | MissingNativeAppStateShim = NativeAppState
|
|
|
|
? new AppState()
|
|
|
|
: new MissingNativeAppStateShim();
|
2016-01-21 22:43:51 +03:00
|
|
|
|
2019-10-22 05:09:51 +03:00
|
|
|
module.exports = AppStateInstance;
|