From 6fb2a2a7d5fab191f999b8f31e524608c30ad694 Mon Sep 17 00:00:00 2001 From: Eric Lewis Date: Thu, 16 May 2019 16:49:52 -0700 Subject: [PATCH] Add spec for AppState (#24880) Summary: part of #24875. ## Changelog [General] [Added] - Add TM spec for AppState Pull Request resolved: https://github.com/facebook/react-native/pull/24880 Differential Revision: D15379482 Pulled By: fkgozali fbshipit-source-id: a76a145a77388cd2ba5cb97d17570162b38f1089 --- Libraries/AppState/AppState.js | 11 +++++----- Libraries/AppState/NativeAppState.js | 30 ++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 6 deletions(-) create mode 100644 Libraries/AppState/NativeAppState.js 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');