Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[TM] Add spec for AppState #24880

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Btw, since you use getEnforcing(), this is always true-ish, and we get invariant() instead when it's not available.

This may be a breaking change, so if that's the case we could downgrade it to get() instead.

We can try landing this PR then adjust things if it breaks something.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh wow, i didn't realize that. okay. this is more to satisfy flow i guess- so i should check null instead?

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,
ericlewis marked this conversation as resolved.
Show resolved Hide resolved
) => void;

// Events
+addListener: (eventName: string) => void;
+removeListeners: (count: number) => void;
}

export default TurboModuleRegistry.getEnforcing<Spec>('AppState');