feat:add set environment value api

This commit is contained in:
pengfei.zhou
2021-07-07 17:30:08 +08:00
committed by osborn
parent 0c10b513b9
commit e6595d5c51
18 changed files with 157 additions and 30 deletions

View File

@@ -9,10 +9,15 @@
#import "AppDelegate.h"
#import "NavigationController.h"
#import "ViewController.h"
#import "DoricRegistry.h"
#if __has_include(<SDWebImage/SDWebImage.h>)
#import <SDWebImage/SDWebImage.h>
#import <SDWebImageWebPCoder/SDWebImageWebPCoder.h>
#endif
@interface AppDelegate ()
@property(nonatomic, strong) UIViewController *rootVC;
@property(nonatomic, strong) NavigationController *navigationController;
@@ -21,8 +26,20 @@ @interface AppDelegate ()
@implementation AppDelegate
- (void)localeChanged {
[DoricRegistry setEnvironmentValue:@{
@"localeLanguage": [[NSLocale autoupdatingCurrentLocale] objectForKey:NSLocaleLanguageCode],
@"localeCountry": [[NSLocale autoupdatingCurrentLocale] objectForKey:NSLocaleCountryCode],
}];
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(localeChanged)
name:UITextFieldTextDidChangeNotification
object:nil];
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.rootVC = [[ViewController alloc] init];

View File

@@ -2,8 +2,6 @@
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>CFBundleDevelopmentRegion</key>
<string>$(DEVELOPMENT_LANGUAGE)</string>
<key>CFBundleExecutable</key>
<string>$(EXECUTABLE_NAME)</string>
<key>CFBundleIdentifier</key>

View File

@@ -47,7 +47,7 @@ NS_ASSUME_NONNULL_BEGIN
- (Class)acquireViewNode:(NSString *)name;
- (void)setEnvironment:(NSString *)key variable:(id)value;
+ (void)setEnvironmentValue:(NSDictionary *)value;
- (void)registerMonitor:(id <DoricMonitorProtocol>)monitor;

View File

@@ -58,6 +58,7 @@
@interface DoricLibraries : NSObject
@property(nonatomic, strong) NSMutableSet <DoricLibrary *> *libraries;
@property(nonatomic, strong) NSMutableArray <NSValue *> *registries;
@property(nonatomic, strong) NSMutableDictionary *envDic;
+ (instancetype)instance;
@end
@@ -67,6 +68,7 @@ - (instancetype)init {
if (self = [super init]) {
_libraries = [NSMutableSet new];
_registries = [NSMutableArray new];
_envDic = [NSMutableDictionary new];
}
return self;
}
@@ -93,13 +95,6 @@ @interface DoricRegistry ()
@implementation DoricRegistry
- (instancetype)initWithJSEngine:(DoricJSEngine *)jsEngine {
if (self = [super init]) {
_jsEngine = jsEngine;
}
return self;
}
+ (void)register:(DoricLibrary *)library {
[DoricLibraries.instance.libraries addObject:library];
for (NSValue *value in DoricLibraries.instance.registries) {
@@ -110,17 +105,29 @@ + (void)register:(DoricLibrary *)library {
}
}
- (instancetype)init {
+ (void)setEnvironmentValue:(NSDictionary *)value {
[DoricLibraries.instance.envDic addEntriesFromDictionary:value];
for (NSValue *val in DoricLibraries.instance.registries) {
DoricRegistry *registry = val.nonretainedObjectValue;
if (registry) {
[registry innerSetEnvironmentValue:value];
}
}
}
- (instancetype)initWithJSEngine:(DoricJSEngine *)jsEngine {
if (self = [super init]) {
_jsEngine = jsEngine;
_bundles = [NSMutableDictionary new];
_plugins = [NSMutableDictionary new];
_nodes = [NSMutableDictionary new];
[self innerRegister];
_monitors = [NSMutableSet new];
NSValue *value = [NSValue valueWithNonretainedObject:self];
[self innerRegister];
[DoricLibraries.instance.libraries enumerateObjectsUsingBlock:^(DoricLibrary *obj, BOOL *stop) {
[obj load:self];
}];
NSValue *value = [NSValue valueWithNonretainedObject:self];
[jsEngine setEnvironmentValue:DoricLibraries.instance.envDic];
[DoricLibraries.instance.registries addObject:value];
}
return self;
@@ -185,8 +192,8 @@ - (Class)acquireViewNode:(NSString *)name {
return self.nodes[name];
}
- (void)setEnvironment:(NSString *)key variable:(id)value {
[self.jsEngine setEnvironment:key variable:value];
- (void)innerSetEnvironmentValue:(NSDictionary *)value {
[self.jsEngine setEnvironmentValue:value];
}
- (void)registerMonitor:(id <DoricMonitorProtocol>)monitor {

View File

@@ -51,7 +51,7 @@ NS_ASSUME_NONNULL_BEGIN
- (void)teardown;
- (void)setEnvironment:(NSString *)key variable:(id)value;
- (void)setEnvironmentValue:(NSDictionary *)value;
@end
NS_ASSUME_NONNULL_END

View File

@@ -106,9 +106,9 @@ - (instancetype)init {
return self;
}
- (void)setEnvironment:(NSString *)key variable:(id)value {
- (void)setEnvironmentValue:(NSDictionary *)value {
[self ensureRunOnJSThread:^{
self.environmentDictionary[key] = value;
[self.environmentDictionary addEntriesFromDictionary:value];
if (self.initialized) {
[self.jsExecutor injectGlobalJSObject:INJECT_ENVIRONMENT obj:[self.environmentDictionary copy]];
for (DoricContext *doricContext in DoricContextManager.instance.aliveContexts) {