mirror of
https://github.com/status-im/react-native.git
synced 2025-01-10 09:35:48 +00:00
88ac40666c
Summary: public This diff replaces the RegEx module method parser with a handwritten recursive descent parser that's faster and easier to maintain. The new parser is ~8 times faster when tested on the UIManager.managerChildren() method, and uses ~1/10 as much RAM. The new parser also supports lightweight generics, and is more tolerant of white space. (This means that you now can – and should – use types like `NSArray<NSString *> *` for your exported properties and method arguments, instead of `NSStringArray`). Reviewed By: jspahrsummers Differential Revision: D2736636 fb-gh-sync-id: f6a11431935fa8acc8ac36f3471032ec9a1c8490
105 lines
2.4 KiB
Objective-C
105 lines
2.4 KiB
Objective-C
/**
|
|
* Copyright (c) 2015-present, Facebook, Inc.
|
|
* All rights reserved.
|
|
*
|
|
* This source code is licensed under the BSD-style license found in the
|
|
* LICENSE file in the root directory of this source tree. An additional grant
|
|
* of patent rights can be found in the PATENTS file in the same directory.
|
|
*/
|
|
|
|
#import "RCTSettingsManager.h"
|
|
|
|
#import "RCTBridge.h"
|
|
#import "RCTConvert.h"
|
|
#import "RCTEventDispatcher.h"
|
|
#import "RCTUtils.h"
|
|
|
|
@implementation RCTSettingsManager
|
|
{
|
|
BOOL _ignoringUpdates;
|
|
NSUserDefaults *_defaults;
|
|
}
|
|
|
|
@synthesize bridge = _bridge;
|
|
|
|
RCT_EXPORT_MODULE()
|
|
|
|
- (instancetype)initWithUserDefaults:(NSUserDefaults *)defaults
|
|
{
|
|
if ((self = [self init])) {
|
|
_defaults = defaults;
|
|
}
|
|
return self;
|
|
}
|
|
|
|
- (void)setBridge:(RCTBridge *)bridge
|
|
{
|
|
_bridge = bridge;
|
|
|
|
if (!_defaults) {
|
|
_defaults = [NSUserDefaults standardUserDefaults];
|
|
}
|
|
|
|
[[NSNotificationCenter defaultCenter] addObserver:self
|
|
selector:@selector(userDefaultsDidChange:)
|
|
name:NSUserDefaultsDidChangeNotification
|
|
object:_defaults];
|
|
}
|
|
|
|
- (NSDictionary<NSString *, id> *)constantsToExport
|
|
{
|
|
return @{@"settings": RCTJSONClean([_defaults dictionaryRepresentation])};
|
|
}
|
|
|
|
- (void)dealloc
|
|
{
|
|
[[NSNotificationCenter defaultCenter] removeObserver:self];
|
|
}
|
|
|
|
- (void)userDefaultsDidChange:(NSNotification *)note
|
|
{
|
|
if (_ignoringUpdates) {
|
|
return;
|
|
}
|
|
|
|
[_bridge.eventDispatcher
|
|
sendDeviceEventWithName:@"settingsUpdated"
|
|
body:RCTJSONClean([_defaults dictionaryRepresentation])];
|
|
}
|
|
|
|
/**
|
|
* Set one or more values in the settings.
|
|
* TODO: would it be useful to have a callback for when this has completed?
|
|
*/
|
|
RCT_EXPORT_METHOD(setValues:(NSDictionary *)values)
|
|
{
|
|
_ignoringUpdates = YES;
|
|
[values enumerateKeysAndObjectsUsingBlock:^(NSString *key, id json, BOOL *stop) {
|
|
id plist = [RCTConvert NSPropertyList:json];
|
|
if (plist) {
|
|
[_defaults setObject:plist forKey:key];
|
|
} else {
|
|
[_defaults removeObjectForKey:key];
|
|
}
|
|
}];
|
|
|
|
[_defaults synchronize];
|
|
_ignoringUpdates = NO;
|
|
}
|
|
|
|
/**
|
|
* Remove some values from the settings.
|
|
*/
|
|
RCT_EXPORT_METHOD(deleteValues:(NSArray<NSString *> *)keys)
|
|
{
|
|
_ignoringUpdates = YES;
|
|
for (NSString *key in keys) {
|
|
[_defaults removeObjectForKey:key];
|
|
}
|
|
|
|
[_defaults synchronize];
|
|
_ignoringUpdates = NO;
|
|
}
|
|
|
|
@end
|