Skip to content

Commit

Permalink
Add spec for AppState (facebook#24880)
Browse files Browse the repository at this point in the history
Summary:
part of facebook#24875.

## Changelog

[General] [Added] - Add TM spec for AppState
Pull Request resolved: facebook#24880

Differential Revision: D15379482

Pulled By: fkgozali

fbshipit-source-id: a76a145a77388cd2ba5cb97d17570162b38f1089
  • Loading branch information
ericlewis authored and M-i-k-e-l committed Mar 10, 2020
1 parent 4a2fba0 commit 6fb2a2a
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 6 deletions.
11 changes: 5 additions & 6 deletions Libraries/AppState/AppState.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -30,15 +29,15 @@ class AppState extends NativeEventEmitter {
isAvailable: boolean;

constructor() {
super(RCTAppState);
super(NativeAppState);

this.isAvailable = true;
this._eventHandlers = {
change: new Map(),
memoryWarning: new Map(),
};

this.currentState = RCTAppState.initialAppState;
this.currentState = NativeAppState.getConstants().initialAppState;

let eventUpdated = false;

Expand All @@ -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;
Expand Down Expand Up @@ -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();
Expand Down
30 changes: 30 additions & 0 deletions Libraries/AppState/NativeAppState.js
Original file line number Diff line number Diff line change
@@ -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<Spec>('AppState');

0 comments on commit 6fb2a2a

Please sign in to comment.