mirror of
https://github.com/facebook/react-native.git
synced 2025-11-01 09:14:26 +00:00
e088a4fe9c
Summary: This new type will be valid in Flow strict mode and can be used by native modules and components to replace `Object`, with the same semantics. This unblocks the migration of the most modules in the React Native package to Flow strict. Changelog: [Internal] Add UnsafeObject type compatible with Flow strict mode to use in native modules and components Reviewed By: RSNara Differential Revision: D25540631 fbshipit-source-id: 60b80bbc84a53aecc747e3a1799cdf551e1859cd
146 lines
2.9 KiB
Plaintext
146 lines
2.9 KiB
Plaintext
/*
|
|
* 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 "RCTSampleTurboModule.h"
|
|
|
|
#import <React/RCTUtils.h>
|
|
#import <UIKit/UIKit.h>
|
|
|
|
using namespace facebook::react;
|
|
|
|
@implementation RCTSampleTurboModule
|
|
|
|
// Backward-compatible export
|
|
RCT_EXPORT_MODULE()
|
|
|
|
@synthesize turboModuleRegistry = _turboModuleRegistry;
|
|
|
|
// Backward-compatible queue configuration
|
|
+ (BOOL)requiresMainQueueSetup
|
|
{
|
|
return YES;
|
|
}
|
|
|
|
- (dispatch_queue_t)methodQueue
|
|
{
|
|
return dispatch_get_main_queue();
|
|
}
|
|
|
|
- (std::shared_ptr<facebook::react::TurboModule>)getTurboModule:
|
|
(const facebook::react::ObjCTurboModule::InitParams &)params
|
|
{
|
|
return std::make_shared<NativeSampleTurboModuleSpecJSI>(params);
|
|
}
|
|
|
|
// Backward compatible invalidation
|
|
- (void)invalidate
|
|
{
|
|
// Actually do nothing here.
|
|
NSLog(@"Invalidating RCTSampleTurboModule...");
|
|
}
|
|
|
|
- (NSDictionary *)getConstants
|
|
{
|
|
__block NSDictionary *constants;
|
|
RCTUnsafeExecuteOnMainQueueSync(^{
|
|
UIScreen *mainScreen = UIScreen.mainScreen;
|
|
CGSize screenSize = mainScreen.bounds.size;
|
|
|
|
constants = @{
|
|
@"const1" : @YES,
|
|
@"const2" : @(screenSize.width),
|
|
@"const3" : @"something",
|
|
};
|
|
});
|
|
|
|
return constants;
|
|
}
|
|
|
|
// TODO: Remove once fully migrated to TurboModule.
|
|
- (NSDictionary *)constantsToExport
|
|
{
|
|
return [self getConstants];
|
|
}
|
|
|
|
RCT_EXPORT_METHOD(voidFunc)
|
|
{
|
|
// Nothing to do
|
|
}
|
|
|
|
RCT_EXPORT_SYNCHRONOUS_TYPED_METHOD(NSNumber *, getBool : (BOOL)arg)
|
|
{
|
|
return @(arg);
|
|
}
|
|
|
|
RCT_EXPORT_SYNCHRONOUS_TYPED_METHOD(NSNumber *, getNumber : (double)arg)
|
|
{
|
|
return @(arg);
|
|
}
|
|
|
|
RCT_EXPORT_SYNCHRONOUS_TYPED_METHOD(NSString *, getString : (NSString *)arg)
|
|
{
|
|
return arg;
|
|
}
|
|
|
|
RCT_EXPORT_SYNCHRONOUS_TYPED_METHOD(NSArray<id<NSObject>> *, getArray : (NSArray *)arg)
|
|
{
|
|
return arg;
|
|
}
|
|
|
|
RCT_EXPORT_SYNCHRONOUS_TYPED_METHOD(NSDictionary *, getObject : (NSDictionary *)arg)
|
|
{
|
|
return arg;
|
|
}
|
|
|
|
RCT_EXPORT_SYNCHRONOUS_TYPED_METHOD(NSDictionary *, getUnsafeObject : (NSDictionary *)arg)
|
|
{
|
|
return arg;
|
|
}
|
|
|
|
RCT_EXPORT_SYNCHRONOUS_TYPED_METHOD(NSNumber *, getRootTag : (double)arg)
|
|
{
|
|
return @(arg);
|
|
}
|
|
|
|
RCT_EXPORT_SYNCHRONOUS_TYPED_METHOD(NSDictionary *, getValue : (double)x y : (NSString *)y z : (NSDictionary *)z)
|
|
{
|
|
return @{
|
|
@"x" : @(x),
|
|
@"y" : y ?: [NSNull null],
|
|
@"z" : z ?: [NSNull null],
|
|
};
|
|
}
|
|
|
|
RCT_EXPORT_METHOD(getValueWithCallback : (RCTResponseSenderBlock)callback)
|
|
{
|
|
if (!callback) {
|
|
return;
|
|
}
|
|
callback(@[ @"value from callback!" ]);
|
|
}
|
|
|
|
RCT_EXPORT_METHOD(getValueWithPromise
|
|
: (BOOL)error resolve
|
|
: (RCTPromiseResolveBlock)resolve reject
|
|
: (RCTPromiseRejectBlock)reject)
|
|
{
|
|
if (!resolve || !reject) {
|
|
return;
|
|
}
|
|
|
|
if (error) {
|
|
reject(
|
|
@"code_1",
|
|
@"intentional promise rejection",
|
|
[NSError errorWithDomain:@"RCTSampleTurboModule" code:1 userInfo:nil]);
|
|
} else {
|
|
resolve(@"result!");
|
|
}
|
|
}
|
|
|
|
@end
|