Skip to content

synchronous access to env #7

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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
40 changes: 20 additions & 20 deletions RNEnvironmentManagerIOS/RNEnvironmentManagerIOS.m
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,28 @@ @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]]);
}
- (NSDictionary *)constantsToExport
{
NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"environment" ofType:@"plist"]];
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 [RNEnvironmentManagerIOS getEnvNotFoundError];
} else {
return json;
}
}

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 *) getEnvNotFoundError
{
return @{ @"error": @"neither environment.plist nor environment.json were found. Did you forget to Add Files to your project?" };
}

@end
29 changes: 1 addition & 28 deletions index.js
Original file line number Diff line number Diff line change
@@ -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