diff --git a/Libraries/AppState/AppState.js b/Libraries/AppState/AppState.js index 19a9334bd7aab8..24b7da77d01481 100644 --- a/Libraries/AppState/AppState.js +++ b/Libraries/AppState/AppState.js @@ -12,8 +12,7 @@ const EventEmitter = require('../vendor/emitter/EventEmitter'); const NativeEventEmitter = require('../EventEmitter/NativeEventEmitter'); -const NativeModules = require('../BatchedBridge/NativeModules'); -const RCTAppState = NativeModules.AppState; +import NativeAppState from './NativeAppState'; const logError = require('../Utilities/logError'); const invariant = require('invariant'); @@ -30,7 +29,7 @@ class AppState extends NativeEventEmitter { isAvailable: boolean; constructor() { - super(RCTAppState); + super(NativeAppState); this.isAvailable = true; this._eventHandlers = { @@ -38,7 +37,7 @@ class AppState extends NativeEventEmitter { memoryWarning: new Map(), }; - this.currentState = RCTAppState.initialAppState; + this.currentState = NativeAppState.getConstants().initialAppState; let eventUpdated = false; @@ -54,7 +53,7 @@ class AppState extends NativeEventEmitter { // 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 => { + 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; @@ -152,7 +151,7 @@ class MissingNativeAppStateShim extends EventEmitter { // 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. -if (RCTAppState) { +if (NativeAppState) { AppState = new AppState(); } else { AppState = new MissingNativeAppStateShim(); diff --git a/Libraries/AppState/NativeAppState.js b/Libraries/AppState/NativeAppState.js new file mode 100644 index 00000000000000..d5d13a392acb94 --- /dev/null +++ b/Libraries/AppState/NativeAppState.js @@ -0,0 +1,30 @@ +/** + * 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. + * + * @flow + * @format + */ + +'use strict'; + +import type {TurboModule} from 'RCTExport'; +import * as TurboModuleRegistry from 'TurboModuleRegistry'; + +export interface Spec extends TurboModule { + +getConstants: () => {| + initialAppState: string, + |}; + +getCurrentAppState: ( + success: (appState: {|app_state: string|}) => void, + failure: (error: Object) => void, + ) => void; + + // Events + +addListener: (eventName: string) => void; + +removeListeners: (count: number) => void; +} + +export default TurboModuleRegistry.getEnforcing('AppState');