mirror of
https://github.com/status-im/react-native.git
synced 2025-02-23 22:58:19 +00:00
Summary: TurboModules depend on a getConstants method. Existing ObjectiveC modules do not have this method. Therefore, I moved the contents of `constantsToExport` to `getConstants` and then had `constantsToExports` call `getConstants`. facebook Since all NativeModules will eventually need to be migrated to the TurboModule system, I didn't restrict this to just the NativeModules in Marketplace. ``` const fs = require('fs'); if (process.argv.length < 3) { throw new Error('Expected a file containing a list of native modules as the third param'); } function read(filename) { return fs.readFileSync(filename, 'utf8'); } const nativeModuleFilenames = read(process.argv[2]).split('\n').filter(Boolean); nativeModuleFilenames.forEach((fileName) => { if (fileName.endsWith('.h')) { return; } const absPath = `${process.env.HOME}/${fileName}`; const fileSource = read(absPath); if (/(\n|^)-\s*\((.+)\)getConstants/.test(fileSource)) { return; } const constantsToExportRegex = /(\n|^)-\s*\((.+)\)constantsToExport/; const result = constantsToExportRegex.exec(fileSource); if (result == null) { throw new Error(`Didn't find a constantsToExport function inside NativeModule ${fileName}`); } const returnType = result[2]; const newFileSource = fileSource.replace( constantsToExportRegex, '$1- ($2)constantsToExport\n' + '{\n' + ` return ${returnType.includes('ModuleConstants') ? '($2)' : ''}[self getConstants];\n` + '}\n' + '\n' + '- ($2)getConstants' ); fs.writeFileSync(absPath, newFileSource); }); ``` ``` > xbgs -l ')constantsToExport' ``` Reviewed By: fkgozali Differential Revision: D13951197 fbshipit-source-id: 394a319d42aff466c56a3d748e17c335307a8f47
130 lines
3.2 KiB
Objective-C
130 lines
3.2 KiB
Objective-C
/**
|
|
* 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.
|
|
*/
|
|
|
|
#import "RCTAppState.h"
|
|
|
|
#import "RCTAssert.h"
|
|
#import "RCTBridge.h"
|
|
#import "RCTEventDispatcher.h"
|
|
#import "RCTUtils.h"
|
|
|
|
static NSString *RCTCurrentAppBackgroundState()
|
|
{
|
|
static NSDictionary *states;
|
|
static dispatch_once_t onceToken;
|
|
dispatch_once(&onceToken, ^{
|
|
states = @{
|
|
@(UIApplicationStateActive): @"active",
|
|
@(UIApplicationStateBackground): @"background"
|
|
};
|
|
});
|
|
|
|
if (RCTRunningInAppExtension()) {
|
|
return @"extension";
|
|
}
|
|
|
|
return states[@(RCTSharedApplication().applicationState)] ?: @"unknown";
|
|
}
|
|
|
|
@implementation RCTAppState
|
|
{
|
|
NSString *_lastKnownState;
|
|
}
|
|
|
|
RCT_EXPORT_MODULE()
|
|
|
|
+ (BOOL)requiresMainQueueSetup
|
|
{
|
|
return YES;
|
|
}
|
|
|
|
- (dispatch_queue_t)methodQueue
|
|
{
|
|
return dispatch_get_main_queue();
|
|
}
|
|
|
|
- (NSDictionary *)constantsToExport
|
|
{
|
|
return [self getConstants];
|
|
}
|
|
|
|
- (NSDictionary *)getConstants
|
|
{
|
|
return @{@"initialAppState": RCTCurrentAppBackgroundState()};
|
|
}
|
|
|
|
#pragma mark - Lifecycle
|
|
|
|
- (NSArray<NSString *> *)supportedEvents
|
|
{
|
|
return @[@"appStateDidChange", @"memoryWarning"];
|
|
}
|
|
|
|
- (void)startObserving
|
|
{
|
|
for (NSString *name in @[UIApplicationDidBecomeActiveNotification,
|
|
UIApplicationDidEnterBackgroundNotification,
|
|
UIApplicationDidFinishLaunchingNotification,
|
|
UIApplicationWillResignActiveNotification,
|
|
UIApplicationWillEnterForegroundNotification]) {
|
|
|
|
[[NSNotificationCenter defaultCenter] addObserver:self
|
|
selector:@selector(handleAppStateDidChange:)
|
|
name:name
|
|
object:nil];
|
|
}
|
|
|
|
[[NSNotificationCenter defaultCenter] addObserver:self
|
|
selector:@selector(handleMemoryWarning)
|
|
name:UIApplicationDidReceiveMemoryWarningNotification
|
|
object:nil];
|
|
}
|
|
|
|
- (void)stopObserving
|
|
{
|
|
[[NSNotificationCenter defaultCenter] removeObserver:self];
|
|
}
|
|
|
|
#pragma mark - App Notification Methods
|
|
|
|
- (void)handleMemoryWarning
|
|
{
|
|
[self sendEventWithName:@"memoryWarning" body:nil];
|
|
}
|
|
|
|
- (void)handleAppStateDidChange:(NSNotification *)notification
|
|
{
|
|
NSString *newState;
|
|
|
|
if ([notification.name isEqualToString:UIApplicationWillResignActiveNotification]) {
|
|
newState = @"inactive";
|
|
} else if ([notification.name isEqualToString:UIApplicationWillEnterForegroundNotification]) {
|
|
newState = @"background";
|
|
} else {
|
|
newState = RCTCurrentAppBackgroundState();
|
|
}
|
|
|
|
if (![newState isEqualToString:_lastKnownState]) {
|
|
_lastKnownState = newState;
|
|
[self sendEventWithName:@"appStateDidChange"
|
|
body:@{@"app_state": _lastKnownState}];
|
|
}
|
|
}
|
|
|
|
#pragma mark - Public API
|
|
|
|
/**
|
|
* Get the current background/foreground state of the app
|
|
*/
|
|
RCT_EXPORT_METHOD(getCurrentAppState:(RCTResponseSenderBlock)callback
|
|
error:(__unused RCTResponseSenderBlock)error)
|
|
{
|
|
callback(@[@{@"app_state": RCTCurrentAppBackgroundState()}]);
|
|
}
|
|
|
|
@end
|