feat: add onEnvChanged

This commit is contained in:
pengfei.zhou
2021-07-07 12:44:40 +08:00
committed by osborn
parent 70bde4fba9
commit 0c10b513b9
13 changed files with 109 additions and 46 deletions

View File

@@ -50,6 +50,8 @@ NS_ASSUME_NONNULL_BEGIN
- (void)initJSEngine;
- (void)teardown;
- (void)setEnvironment:(NSString *)key variable:(id)value;
@end
NS_ASSUME_NONNULL_END

View File

@@ -27,6 +27,7 @@
#import "DoricBridgeExtension.h"
#import <sys/utsname.h>
#import "DoricContext.h"
#import "DoricContextManager.h"
@interface DoricDefaultMonitor : NSObject <DoricMonitorProtocol>
@end
@@ -47,12 +48,14 @@ @interface DoricJSEngine ()
@property(nonatomic, strong) NSMutableDictionary *environmentDictionary;
@property(nonatomic, strong) NSThread *jsThread;
@property(nonatomic, assign) BOOL destroyed;
@property(nonatomic, assign) BOOL initialized;
@end
@implementation DoricJSEngine
- (instancetype)init {
if (self = [super init]) {
_initialized = NO;
_jsThread = [[NSThread alloc] initWithTarget:self selector:@selector(threadRun) object:nil];
[_jsThread start];
NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];
@@ -88,7 +91,7 @@ - (instancetype)init {
@"localeLanguage": [[NSLocale currentLocale] objectForKey:NSLocaleLanguageCode],
@"localeCountry": [[NSLocale currentLocale] objectForKey:NSLocaleCountryCode],
}.mutableCopy;
self.registry = [[DoricRegistry alloc] init];
self.registry = [[DoricRegistry alloc] initWithJSEngine:self];
[self ensureRunOnJSThread:^() {
self.timers = [[NSMutableDictionary alloc] init];
self.bridgeExtension = [DoricBridgeExtension new];
@@ -96,12 +99,25 @@ - (instancetype)init {
[self initJSEngine];
[self initJSExecutor];
[self initDoricEnvironment];
self.initialized = YES;
}];
[self.registry registerMonitor:[DoricDefaultMonitor new]];
}
return self;
}
- (void)setEnvironment:(NSString *)key variable:(id)value {
[self ensureRunOnJSThread:^{
self.environmentDictionary[key] = value;
if (self.initialized) {
[self.jsExecutor injectGlobalJSObject:INJECT_ENVIRONMENT obj:[self.environmentDictionary copy]];
for (DoricContext *doricContext in DoricContextManager.instance.aliveContexts) {
[doricContext onEnvChanged];
}
}
}];
}
- (void)teardown {
_destroyed = YES;
//To ensure runloop continue.
@@ -136,9 +152,6 @@ - (void)initJSEngine {
- (void)initJSExecutor {
__weak typeof(self) _self = self;
[self.registry.environmentVariables enumerateKeysAndObjectsUsingBlock:^(NSString *key, id obj, BOOL *stop) {
self.environmentDictionary[key] = obj;
}];
[self.jsExecutor injectGlobalJSObject:INJECT_ENVIRONMENT obj:[self.environmentDictionary copy]];
[self.jsExecutor injectGlobalJSObject:INJECT_LOG obj:^(NSString *type, NSString *message) {
if ([type isEqualToString:@"e"]) {