|
| 1 | +/** |
| 2 | + * Copyright (c) 2015-present, Facebook, Inc. |
| 3 | + * All rights reserved. |
| 4 | + * |
| 5 | + * This source code is licensed under the BSD-style license found in the |
| 6 | + * LICENSE file in the root directory of this source tree. An additional grant |
| 7 | + * of patent rights can be found in the PATENTS file in the same directory. |
| 8 | + * |
| 9 | + * @providesModule AppState |
| 10 | + * @flow |
| 11 | + */ |
| 12 | +'use strict'; |
| 13 | + |
| 14 | +var Map = require('Map'); |
| 15 | +var NativeModules = require('NativeModules'); |
| 16 | +var RCTDeviceEventEmitter = require('RCTDeviceEventEmitter'); |
| 17 | +var RCTAppState = NativeModules.AppState; |
| 18 | + |
| 19 | +var logError = require('logError'); |
| 20 | +var invariant = require('invariant'); |
| 21 | + |
| 22 | +var _eventHandlers = { |
| 23 | + change: new Map(), |
| 24 | + memoryWarning: new Map(), |
| 25 | +}; |
| 26 | + |
| 27 | +/** |
| 28 | + * `AppState` can tell you if the app is in the foreground or background, |
| 29 | + * and notify you when the state changes. |
| 30 | + * |
| 31 | + * AppState is frequently used to determine the intent and proper behavior when |
| 32 | + * handling push notifications. |
| 33 | + * |
| 34 | + * ### App States |
| 35 | + * |
| 36 | + * - `active` - The app is running in the foreground |
| 37 | + * - `background` - The app is running in the background. The user is either |
| 38 | + * in another app or on the home screen |
| 39 | + * - `inactive` - This is a transition state that currently never happens for |
| 40 | + * typical React Native apps. |
| 41 | + * |
| 42 | + * For more information, see |
| 43 | + * [Apple's documentation](https://developer.apple.com/library/ios/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/TheAppLifeCycle/TheAppLifeCycle.html) |
| 44 | + * |
| 45 | + * ### Basic Usage |
| 46 | + * |
| 47 | + * To see the current state, you can check `AppState.currentState`, which |
| 48 | + * will be kept up-to-date. However, `currentState` will be null at launch |
| 49 | + * while `AppState` retrieves it over the bridge. |
| 50 | + * |
| 51 | + * ``` |
| 52 | + * getInitialState: function() { |
| 53 | + * return { |
| 54 | + * currentAppState: AppState.currentState, |
| 55 | + * }; |
| 56 | + * }, |
| 57 | + * componentDidMount: function() { |
| 58 | + * AppState.addEventListener('change', this._handleAppStateChange); |
| 59 | + * }, |
| 60 | + * componentWillUnmount: function() { |
| 61 | + * AppState.removeEventListener('change', this._handleAppStateChange); |
| 62 | + * }, |
| 63 | + * _handleAppStateChange: function(currentAppState) { |
| 64 | + * this.setState({ currentAppState, }); |
| 65 | + * }, |
| 66 | + * render: function() { |
| 67 | + * return ( |
| 68 | + * <Text>Current state is: {this.state.currentAppState}</Text> |
| 69 | + * ); |
| 70 | + * }, |
| 71 | + * ``` |
| 72 | + * |
| 73 | + * This example will only ever appear to say "Current state is: active" because |
| 74 | + * the app is only visible to the user when in the `active` state, and the null |
| 75 | + * state will happen only momentarily. |
| 76 | + */ |
| 77 | + |
| 78 | +var AppState = { |
| 79 | + |
| 80 | + /** |
| 81 | + * Add a handler to AppState changes by listening to the `change` event type |
| 82 | + * and providing the handler |
| 83 | + */ |
| 84 | + addEventListener: function( |
| 85 | + type: string, |
| 86 | + handler: Function |
| 87 | + ) { |
| 88 | + invariant( |
| 89 | + ['change', 'memoryWarning'].indexOf(type) !== -1, |
| 90 | + 'Trying to subscribe to unknown event: "%s"', type |
| 91 | + ); |
| 92 | + if (type === 'change') { |
| 93 | + _eventHandlers[type].set(handler, RCTDeviceEventEmitter.addListener( |
| 94 | + 'appStateDidChange', |
| 95 | + (appStateData) => { |
| 96 | + handler(appStateData.app_state); |
| 97 | + } |
| 98 | + )); |
| 99 | + } else if (type === 'memoryWarning') { |
| 100 | + _eventHandlers[type].set(handler, RCTDeviceEventEmitter.addListener( |
| 101 | + 'memoryWarning', |
| 102 | + handler |
| 103 | + )); |
| 104 | + } |
| 105 | + }, |
| 106 | + |
| 107 | + /** |
| 108 | + * Remove a handler by passing the `change` event type and the handler |
| 109 | + */ |
| 110 | + removeEventListener: function( |
| 111 | + type: string, |
| 112 | + handler: Function |
| 113 | + ) { |
| 114 | + invariant( |
| 115 | + ['change', 'memoryWarning'].indexOf(type) !== -1, |
| 116 | + 'Trying to remove listener for unknown event: "%s"', type |
| 117 | + ); |
| 118 | + if (!_eventHandlers[type].has(handler)) { |
| 119 | + return; |
| 120 | + } |
| 121 | + _eventHandlers[type].get(handler).remove(); |
| 122 | + _eventHandlers[type].delete(handler); |
| 123 | + }, |
| 124 | + |
| 125 | + // TODO: getCurrentAppState callback seems to be called at a really late stage |
| 126 | + // after app launch. Trying to get currentState when mounting App component |
| 127 | + // will likely to have the initial value here. |
| 128 | + // Initialize to 'active' instead of null. |
| 129 | + currentState: ('active' : ?string), |
| 130 | + |
| 131 | +}; |
| 132 | + |
| 133 | +RCTDeviceEventEmitter.addListener( |
| 134 | + 'appStateDidChange', |
| 135 | + (appStateData) => { |
| 136 | + AppState.currentState = appStateData.app_state; |
| 137 | + } |
| 138 | +); |
| 139 | + |
| 140 | +RCTAppState.getCurrentAppState( |
| 141 | + (appStateData) => { |
| 142 | + AppState.currentState = appStateData.app_state; |
| 143 | + }, |
| 144 | + logError |
| 145 | +); |
| 146 | + |
| 147 | +module.exports = AppState; |
0 commit comments