Skip to content

Commit

Permalink
Make RCTAppState TurboModule-compatible
Browse files Browse the repository at this point in the history
Summary:
See title.

Changelog:
[iOS][Added] - Make RCTAppState TurboModule-compatible

Reviewed By: PeteTheHeat

Differential Revision: D18142253

fbshipit-source-id: 5bd8afa6e3ee98f92aac3b2ebdfe63b9f7afc775
  • Loading branch information
RSNara authored and facebook-github-bot committed Nov 1, 2019
1 parent 6481a12 commit 7233ae4
Show file tree
Hide file tree
Showing 9 changed files with 34 additions and 12 deletions.
2 changes: 1 addition & 1 deletion Libraries/AppState/NativeAppState.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export interface Spec extends TurboModule {
|};
+getCurrentAppState: (
success: (appState: {|app_state: string|}) => void,
failure: (error: Object) => void,
error: (error: Object) => void,
) => void;

// Events
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -403,7 +403,7 @@ + (RCTManagedPointer *)JS_NativeAppState_SpecGetCurrentAppStateSuccessAppState:(


static facebook::jsi::Value __hostFunction_NativeAppStateSpecJSI_getCurrentAppState(facebook::jsi::Runtime& rt, TurboModule &turboModule, const facebook::jsi::Value* args, size_t count) {
return static_cast<ObjCTurboModule&>(turboModule).invokeObjCMethod(rt, VoidKind, "getCurrentAppState", @selector(getCurrentAppState:failure:), args, count);
return static_cast<ObjCTurboModule&>(turboModule).invokeObjCMethod(rt, VoidKind, "getCurrentAppState", @selector(getCurrentAppState:error:), args, count);
}

static facebook::jsi::Value __hostFunction_NativeAppStateSpecJSI_addListener(facebook::jsi::Runtime& rt, TurboModule &turboModule, const facebook::jsi::Value* args, size_t count) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,7 @@ namespace JS {
@protocol NativeAppStateSpec <RCTBridgeModule, RCTTurboModule>

- (void)getCurrentAppState:(RCTResponseSenderBlock)success
failure:(RCTResponseSenderBlock)failure;
error:(RCTResponseSenderBlock)error;
- (void)addListener:(NSString *)eventName;
- (void)removeListeners:(double)count;
- (facebook::react::ModuleConstants<JS::NativeAppState::Constants::Builder>)constantsToExport;
Expand Down
3 changes: 3 additions & 0 deletions React/CoreModules/BUCK
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,9 @@ rn_apple_library(
) + react_module_plugin_providers(
name = "KeyboardObserver",
native_class_func = "RCTKeyboardObserverCls",
) + react_module_plugin_providers(
name = "AppState",
native_class_func = "RCTAppStateCls",
),
plugins_header = "FBCoreModulesPlugins.h",
preprocessor_flags = OBJC_ARC_PREPROCESSOR_FLAGS + get_debug_preprocessor_flags() + rn_extra_build_flags() + [
Expand Down
1 change: 1 addition & 0 deletions React/CoreModules/CoreModulesPlugins.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ Class RCTAsyncLocalStorageCls(void) __attribute__((used));
Class RCTTimingCls(void) __attribute__((used));
Class RCTStatusBarManagerCls(void) __attribute__((used));
Class RCTKeyboardObserverCls(void) __attribute__((used));
Class RCTAppStateCls(void) __attribute__((used));

#ifdef __cplusplus
}
Expand Down
1 change: 1 addition & 0 deletions React/CoreModules/CoreModulesPlugins.mm
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ Class RCTCoreModulesClassProvider(const char *name) {
{"Timing", RCTTimingCls},
{"StatusBarManager", RCTStatusBarManagerCls},
{"KeyboardObserver", RCTKeyboardObserverCls},
{"AppState", RCTAppStateCls},
};

auto p = sCoreModuleClassMap.find(name);
Expand Down
File renamed without changes.
33 changes: 25 additions & 8 deletions React/Modules/RCTAppState.m → React/CoreModules/RCTAppState.mm
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,13 @@

#import "RCTAppState.h"

#import "RCTAssert.h"
#import "RCTBridge.h"
#import "RCTEventDispatcher.h"
#import "RCTUtils.h"
#import <FBReactNativeSpec/FBReactNativeSpec.h>
#import <React/RCTAssert.h>
#import <React/RCTBridge.h>
#import <React/RCTEventDispatcher.h>
#import <React/RCTUtils.h>

#import "CoreModulesPlugins.h"

static NSString *RCTCurrentAppState()
{
Expand All @@ -30,6 +33,9 @@
return states[@(RCTSharedApplication().applicationState)] ?: @"unknown";
}

@interface RCTAppState() <NativeAppStateSpec>
@end

@implementation RCTAppState
{
NSString *_lastKnownState;
Expand All @@ -47,14 +53,16 @@ - (dispatch_queue_t)methodQueue
return dispatch_get_main_queue();
}

- (NSDictionary *)constantsToExport
- (facebook::react::ModuleConstants<JS::NativeAppState::Constants>)constantsToExport
{
return [self getConstants];
return (facebook::react::ModuleConstants<JS::NativeAppState::Constants>)[self getConstants];
}

- (NSDictionary *)getConstants
- (facebook::react::ModuleConstants<JS::NativeAppState::Constants>)getConstants
{
return @{@"initialAppState": RCTCurrentAppState()};
return facebook::react::typedConstants<JS::NativeAppState::Constants>({
.initialAppState = RCTCurrentAppState(),
});
}

#pragma mark - Lifecycle
Expand Down Expand Up @@ -126,4 +134,13 @@ - (void)handleAppStateDidChange:(NSNotification *)notification
callback(@[@{@"app_state": RCTCurrentAppState()}]);
}

- (std::shared_ptr<facebook::react::TurboModule>)getTurboModuleWithJsInvoker:(std::shared_ptr<facebook::react::CallInvoker>)jsInvoker
{
return std::make_shared<facebook::react::NativeAppStateSpecJSI>(self, jsInvoker);
}

@end

Class RCTAppStateCls(void) {
return RCTAppState.class;
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public NativeAppStateSpec(ReactApplicationContext reactContext) {
public abstract void removeListeners(double count);

@ReactMethod
public abstract void getCurrentAppState(Callback success, Callback failure);
public abstract void getCurrentAppState(Callback success, Callback error);

@ReactMethod
public abstract void addListener(String eventName);
Expand Down

0 comments on commit 7233ae4

Please sign in to comment.