2016-01-21 19:43:51 +00:00
|
|
|
/**
|
|
|
|
* Copyright (c) 2015-present, Facebook, Inc.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This source code is licensed under the BSD-style license found in the
|
|
|
|
* LICENSE file in the root directory of this source tree. An additional grant
|
|
|
|
* of patent rights can be found in the PATENTS file in the same directory.
|
|
|
|
*
|
|
|
|
* @providesModule AppState
|
|
|
|
* @flow
|
|
|
|
*/
|
|
|
|
'use strict';
|
|
|
|
|
2017-06-01 15:20:57 +00:00
|
|
|
const MissingNativeEventEmitterShim = require('MissingNativeEventEmitterShim');
|
2016-05-23 16:08:51 +00:00
|
|
|
const NativeEventEmitter = require('NativeEventEmitter');
|
|
|
|
const NativeModules = require('NativeModules');
|
|
|
|
const RCTAppState = NativeModules.AppState;
|
2016-01-21 19:43:51 +00:00
|
|
|
|
2016-05-23 16:08:51 +00:00
|
|
|
const logError = require('logError');
|
|
|
|
const invariant = require('fbjs/lib/invariant');
|
2016-01-21 19:43:51 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* `AppState` can tell you if the app is in the foreground or background,
|
|
|
|
* and notify you when the state changes.
|
|
|
|
*
|
|
|
|
* AppState is frequently used to determine the intent and proper behavior when
|
|
|
|
* handling push notifications.
|
|
|
|
*
|
|
|
|
* ### App States
|
|
|
|
*
|
|
|
|
* - `active` - The app is running in the foreground
|
|
|
|
* - `background` - The app is running in the background. The user is either
|
|
|
|
* in another app or on the home screen
|
2016-05-23 16:08:51 +00:00
|
|
|
* - `inactive` - This is a state that occurs when transitioning between
|
2017-06-01 15:20:57 +00:00
|
|
|
* foreground & background, and during periods of inactivity such as
|
|
|
|
* entering the Multitasking view or in the event of an incoming call
|
2016-01-21 19:43:51 +00:00
|
|
|
*
|
|
|
|
* For more information, see
|
|
|
|
* [Apple's documentation](https://developer.apple.com/library/ios/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/TheAppLifeCycle/TheAppLifeCycle.html)
|
|
|
|
*
|
|
|
|
* ### Basic Usage
|
|
|
|
*
|
|
|
|
* To see the current state, you can check `AppState.currentState`, which
|
|
|
|
* will be kept up-to-date. However, `currentState` will be null at launch
|
|
|
|
* while `AppState` retrieves it over the bridge.
|
|
|
|
*
|
|
|
|
* ```
|
2017-01-23 19:58:03 +00:00
|
|
|
* import React, {Component} from 'react'
|
|
|
|
* import {AppState, Text} from 'react-native'
|
|
|
|
*
|
|
|
|
* class AppStateExample extends Component {
|
|
|
|
*
|
|
|
|
* state = {
|
|
|
|
* appState: AppState.currentState
|
|
|
|
* }
|
|
|
|
*
|
|
|
|
* componentDidMount() {
|
|
|
|
* AppState.addEventListener('change', this._handleAppStateChange);
|
|
|
|
* }
|
|
|
|
*
|
|
|
|
* componentWillUnmount() {
|
|
|
|
* AppState.removeEventListener('change', this._handleAppStateChange);
|
|
|
|
* }
|
|
|
|
*
|
|
|
|
* _handleAppStateChange = (nextAppState) => {
|
|
|
|
* if (this.state.appState.match(/inactive|background/) && nextAppState === 'active') {
|
|
|
|
* console.log('App has come to the foreground!')
|
|
|
|
* }
|
|
|
|
* this.setState({appState: nextAppState});
|
|
|
|
* }
|
|
|
|
*
|
|
|
|
* render() {
|
|
|
|
* return (
|
|
|
|
* <Text>Current state is: {this.state.appState}</Text>
|
|
|
|
* );
|
|
|
|
* }
|
|
|
|
*
|
|
|
|
* }
|
2016-01-21 19:43:51 +00:00
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* This example will only ever appear to say "Current state is: active" because
|
|
|
|
* the app is only visible to the user when in the `active` state, and the null
|
|
|
|
* state will happen only momentarily.
|
|
|
|
*/
|
|
|
|
|
2016-05-23 16:08:51 +00:00
|
|
|
class AppState extends NativeEventEmitter {
|
2016-01-21 19:43:51 +00:00
|
|
|
|
2016-05-23 16:08:51 +00:00
|
|
|
_eventHandlers: Object;
|
|
|
|
currentState: ?string;
|
2017-06-01 15:20:57 +00:00
|
|
|
isAvailable: boolean = true;
|
2016-05-23 16:08:51 +00:00
|
|
|
|
|
|
|
constructor() {
|
|
|
|
super(RCTAppState);
|
|
|
|
|
2017-02-21 16:55:45 +00:00
|
|
|
this.isAvailable = true;
|
2016-05-23 16:08:51 +00:00
|
|
|
this._eventHandlers = {
|
|
|
|
change: new Map(),
|
|
|
|
memoryWarning: new Map(),
|
|
|
|
};
|
|
|
|
|
2016-07-13 06:24:27 +00:00
|
|
|
// TODO: Remove the 'active' fallback after `initialAppState` is exported by
|
|
|
|
// the Android implementation.
|
|
|
|
this.currentState = RCTAppState.initialAppState || 'active';
|
|
|
|
|
2017-08-18 20:08:56 +00:00
|
|
|
let eventUpdated = false;
|
|
|
|
|
2016-05-23 16:08:51 +00:00
|
|
|
// 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) => {
|
2017-08-18 20:08:56 +00:00
|
|
|
eventUpdated = true;
|
2016-05-23 16:08:51 +00:00
|
|
|
this.currentState = appStateData.app_state;
|
|
|
|
}
|
|
|
|
);
|
2016-07-13 06:24:27 +00:00
|
|
|
|
2016-05-23 16:08:51 +00:00
|
|
|
// 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.
|
|
|
|
RCTAppState.getCurrentAppState(
|
|
|
|
(appStateData) => {
|
2017-08-18 20:08:56 +00:00
|
|
|
if (!eventUpdated) {
|
|
|
|
this.currentState = appStateData.app_state;
|
|
|
|
}
|
2016-05-23 16:08:51 +00:00
|
|
|
},
|
|
|
|
logError
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2016-07-13 06:24:27 +00:00
|
|
|
/**
|
2016-01-21 19:43:51 +00:00
|
|
|
* Add a handler to AppState changes by listening to the `change` event type
|
|
|
|
* and providing the handler
|
2016-05-23 16:08:51 +00:00
|
|
|
*
|
|
|
|
* 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).
|
2016-01-21 19:43:51 +00:00
|
|
|
*/
|
2016-05-23 16:08:51 +00:00
|
|
|
addEventListener(
|
2016-01-21 19:43:51 +00:00
|
|
|
type: string,
|
|
|
|
handler: Function
|
|
|
|
) {
|
|
|
|
invariant(
|
|
|
|
['change', 'memoryWarning'].indexOf(type) !== -1,
|
|
|
|
'Trying to subscribe to unknown event: "%s"', type
|
|
|
|
);
|
|
|
|
if (type === 'change') {
|
2016-05-23 16:08:51 +00:00
|
|
|
this._eventHandlers[type].set(handler, this.addListener(
|
2016-01-21 19:43:51 +00:00
|
|
|
'appStateDidChange',
|
|
|
|
(appStateData) => {
|
|
|
|
handler(appStateData.app_state);
|
|
|
|
}
|
|
|
|
));
|
|
|
|
} else if (type === 'memoryWarning') {
|
2016-05-23 16:08:51 +00:00
|
|
|
this._eventHandlers[type].set(handler, this.addListener(
|
2016-01-21 19:43:51 +00:00
|
|
|
'memoryWarning',
|
|
|
|
handler
|
|
|
|
));
|
|
|
|
}
|
2016-05-23 16:08:51 +00:00
|
|
|
}
|
2016-01-21 19:43:51 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove a handler by passing the `change` event type and the handler
|
|
|
|
*/
|
2016-05-23 16:08:51 +00:00
|
|
|
removeEventListener(
|
2016-01-21 19:43:51 +00:00
|
|
|
type: string,
|
|
|
|
handler: Function
|
|
|
|
) {
|
|
|
|
invariant(
|
|
|
|
['change', 'memoryWarning'].indexOf(type) !== -1,
|
|
|
|
'Trying to remove listener for unknown event: "%s"', type
|
|
|
|
);
|
2016-05-23 16:08:51 +00:00
|
|
|
if (!this._eventHandlers[type].has(handler)) {
|
2016-01-21 19:43:51 +00:00
|
|
|
return;
|
|
|
|
}
|
2016-05-23 16:08:51 +00:00
|
|
|
this._eventHandlers[type].get(handler).remove();
|
|
|
|
this._eventHandlers[type].delete(handler);
|
2016-01-21 19:43:51 +00:00
|
|
|
}
|
2016-08-09 13:32:41 +00:00
|
|
|
}
|
2016-01-21 19:43:51 +00:00
|
|
|
|
2017-06-01 15:20:57 +00:00
|
|
|
if (__DEV__ && !RCTAppState) {
|
|
|
|
class MissingNativeAppStateShim extends MissingNativeEventEmitterShim {
|
|
|
|
constructor() {
|
|
|
|
super('RCTAppState', 'AppState');
|
|
|
|
}
|
2017-02-17 17:49:35 +00:00
|
|
|
|
2017-06-01 15:20:57 +00:00
|
|
|
get currentState(): ?string {
|
|
|
|
this.throwMissingNativeModule();
|
|
|
|
}
|
2017-02-17 17:49:35 +00:00
|
|
|
|
2017-06-01 15:20:57 +00:00
|
|
|
addEventListener(...args: Array<any>) {
|
|
|
|
this.throwMissingNativeModule();
|
|
|
|
}
|
2017-02-17 17:49:35 +00:00
|
|
|
|
2017-06-01 15:20:57 +00:00
|
|
|
removeEventListener(...args: Array<any>) {
|
|
|
|
this.throwMissingNativeModule();
|
|
|
|
}
|
2017-02-17 17:49:35 +00:00
|
|
|
}
|
|
|
|
|
2017-06-01 15:20:57 +00:00
|
|
|
// 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.
|
2017-02-21 16:55:45 +00:00
|
|
|
AppState = new MissingNativeAppStateShim();
|
2017-06-01 15:20:57 +00:00
|
|
|
} else {
|
|
|
|
AppState = new AppState();
|
2017-02-21 16:55:45 +00:00
|
|
|
}
|
2016-01-21 19:43:51 +00:00
|
|
|
|
2017-02-21 16:55:45 +00:00
|
|
|
module.exports = AppState;
|