From 5354ee586ef357aeb370de25702803fe4fcde705 Mon Sep 17 00:00:00 2001 From: Mark Vayngrib Date: Tue, 5 Jan 2016 19:18:24 -0500 Subject: [PATCH 1/3] synchronous access to env --- .../RNEnvironmentManagerIOS.m | 25 ++-------------- index.js | 29 +------------------ 2 files changed, 4 insertions(+), 50 deletions(-) diff --git a/RNEnvironmentManagerIOS/RNEnvironmentManagerIOS.m b/RNEnvironmentManagerIOS/RNEnvironmentManagerIOS.m index bc724c0..938124e 100644 --- a/RNEnvironmentManagerIOS/RNEnvironmentManagerIOS.m +++ b/RNEnvironmentManagerIOS/RNEnvironmentManagerIOS.m @@ -5,28 +5,9 @@ @implementation RNEnvironmentManagerIOS RCT_EXPORT_MODULE() -RCT_EXPORT_METHOD(get:(NSString *)name callback:(RCTResponseSenderBlock)callback) { - @try { - NSDictionary *env = [NSDictionary dictionaryWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"environment" ofType:@"plist"]]; - if ([env objectForKey:name]) { - callback(@[[NSNull null], env[name]]); - } else { - callback(@[[NSNull null], [NSNull null]]); - } - } - @catch (NSException *exception) { - callback(@[exception.reason, [NSNull null]]); - } -} - -RCT_EXPORT_METHOD(getAll:(RCTResponseSenderBlock)callback) { - @try { - NSDictionary *env = [NSDictionary dictionaryWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"environment" ofType:@"plist"]]; - callback(@[[NSNull null], env]); - } - @catch (NSException *exception) { - callback(@[exception.reason, [NSNull null]]); - } +- (NSDictionary *)constantsToExport +{ + return [NSDictionary dictionaryWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"environment" ofType:@"plist"]]; } @end diff --git a/index.js b/index.js index 499ccaf..33e3d73 100644 --- a/index.js +++ b/index.js @@ -1,28 +1 @@ -var NativeModules = require('react-native').NativeModules; -var RNEnvironmentManagerIOS = NativeModules.RNEnvironmentManagerIOS; - -module.exports = { - get(name) { - return new Promise((resolve, reject) => { - RNEnvironmentManagerIOS.get(name, (err, res) => { - if (err) { - reject(err); - } else { - resolve(res); - } - }); - }); - }, - - getAll() { - return new Promise((resolve, reject) => { - RNEnvironmentManagerIOS.getAll((err, res) => { - if (err) { - reject(err); - } else { - resolve(res); - } - }); - }); - } -} +module.exports = require('react-native').NativeModules.RNEnvironmentManagerIOS From 7236cee9d9df4d013e5a9fcefaa89eb678d6c841 Mon Sep 17 00:00:00 2001 From: Mark Vayngrib Date: Tue, 5 Jan 2016 20:07:42 -0500 Subject: [PATCH 2/3] support environment.json --- RNEnvironmentManagerIOS/RNEnvironmentManagerIOS.m | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/RNEnvironmentManagerIOS/RNEnvironmentManagerIOS.m b/RNEnvironmentManagerIOS/RNEnvironmentManagerIOS.m index 938124e..f785c5d 100644 --- a/RNEnvironmentManagerIOS/RNEnvironmentManagerIOS.m +++ b/RNEnvironmentManagerIOS/RNEnvironmentManagerIOS.m @@ -7,7 +7,17 @@ @implementation RNEnvironmentManagerIOS - (NSDictionary *)constantsToExport { - return [NSDictionary dictionaryWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"environment" ofType:@"plist"]]; + NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"environment" ofType:@"plist"]]; + if (dict) return dict; + + NSData *data = [NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"environment" ofType:@"json"]]; + NSError *error = nil; + id json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error]; + if (error) { + return @{ @"error": @"environment.json not found. Did you forget to Add Files to your project?" }; + } else { + return json; + } } @end From 19361827c84ad4a3a9d761fdadd8d65f0e814856 Mon Sep 17 00:00:00 2001 From: Mark Vayngrib Date: Thu, 7 Jan 2016 23:45:49 -0500 Subject: [PATCH 3/3] err handling --- RNEnvironmentManagerIOS/RNEnvironmentManagerIOS.m | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/RNEnvironmentManagerIOS/RNEnvironmentManagerIOS.m b/RNEnvironmentManagerIOS/RNEnvironmentManagerIOS.m index f785c5d..ae5e785 100644 --- a/RNEnvironmentManagerIOS/RNEnvironmentManagerIOS.m +++ b/RNEnvironmentManagerIOS/RNEnvironmentManagerIOS.m @@ -11,13 +11,22 @@ - (NSDictionary *)constantsToExport if (dict) return dict; NSData *data = [NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"environment" ofType:@"json"]]; + if (!data) { + return [RNEnvironmentManagerIOS getEnvNotFoundError]; + } + NSError *error = nil; id json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error]; if (error) { - return @{ @"error": @"environment.json not found. Did you forget to Add Files to your project?" }; + return [RNEnvironmentManagerIOS getEnvNotFoundError]; } else { return json; } } ++ (NSDictionary *) getEnvNotFoundError +{ + return @{ @"error": @"neither environment.plist nor environment.json were found. Did you forget to Add Files to your project?" }; +} + @end