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

@@ -63,6 +63,8 @@ NS_ASSUME_NONNULL_BEGIN
- (void)onHidden;
-(void)onEnvChanged;
- (DoricViewNode *)targetViewNode:(NSString *)viewId;
- (void)dispatchToMainQueue:(_Nonnull dispatch_block_t)block;

View File

@@ -121,6 +121,10 @@ - (void)onHidden {
[self callEntity:DORIC_ENTITY_HIDDEN withArgumentsArray:@[]];
}
- (void)onEnvChanged {
[self callEntity:DORIC_ENTITY_ENV_CHANGE withArgumentsArray:@[]];
}
- (UIViewController *)vc {
if (!_vc) {
return [UIApplication sharedApplication].keyWindow.rootViewController;

View File

@@ -26,11 +26,14 @@
NS_ASSUME_NONNULL_BEGIN
@class DoricLibrary;
@class DoricJSEngine;
@interface DoricRegistry : NSObject <DoricMonitorProtocol>
@property(nonatomic, strong) UIImage *defaultPlaceHolderImage;
@property(nonatomic, strong) UIImage *defaultErrorImage;
- (instancetype)initWithJSEngine:(DoricJSEngine *)jsEngine;
- (NSString *)acquireJSBundle:(NSString *)name;
- (void)registerJSBundle:(NSString *)bundle withName:(NSString *)name;
@@ -46,8 +49,6 @@ NS_ASSUME_NONNULL_BEGIN
- (void)setEnvironment:(NSString *)key variable:(id)value;
- (NSDictionary *)environmentVariables;
- (void)registerMonitor:(id <DoricMonitorProtocol>)monitor;
+ (void)register:(DoricLibrary *)library;

View File

@@ -48,12 +48,12 @@
#import "DoricLibrary.h"
#import "DoricNotificationPlugin.h"
#import "DoricStatusBarPlugin.h"
#import "DoricUtil.h"
#import "DoricCoordinatorPlugin.h"
#import "DoricSwitchNode.h"
#import "DoricNotchPlugin.h"
#import "DoricFlexNode.h"
#import "DoricKeyboardPlugin.h"
#import "DoricJSEngine.h"
@interface DoricLibraries : NSObject
@property(nonatomic, strong) NSMutableSet <DoricLibrary *> *libraries;
@@ -87,12 +87,19 @@ @interface DoricRegistry ()
@property(nonatomic, strong) NSMutableDictionary *bundles;
@property(nonatomic, strong) NSMutableDictionary *plugins;
@property(nonatomic, strong) NSMutableDictionary *nodes;
@property(nonatomic, strong) NSMutableDictionary <NSString *, id> *envVariables;
@property(nonatomic, strong) NSMutableSet <id <DoricMonitorProtocol>> *monitors;
@property(nonatomic, weak) DoricJSEngine *jsEngine;
@end
@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) {
@@ -108,7 +115,6 @@ - (instancetype)init {
_bundles = [NSMutableDictionary new];
_plugins = [NSMutableDictionary new];
_nodes = [NSMutableDictionary new];
_envVariables = [NSMutableDictionary new];
[self innerRegister];
_monitors = [NSMutableSet new];
[DoricLibraries.instance.libraries enumerateObjectsUsingBlock:^(DoricLibrary *obj, BOOL *stop) {
@@ -180,11 +186,7 @@ - (Class)acquireViewNode:(NSString *)name {
}
- (void)setEnvironment:(NSString *)key variable:(id)value {
self.envVariables[key] = value;
}
- (NSDictionary *)environmentVariables {
return self.envVariables;
[self.jsEngine setEnvironment:key variable:value];
}
- (void)registerMonitor:(id <DoricMonitorProtocol>)monitor {

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"]) {

View File

@@ -67,3 +67,5 @@ extern NSString *const DORIC_ENTITY_SHOW;
extern NSString *const DORIC_ENTITY_HIDDEN;
extern NSString *const DORIC_ENTITY_BUILD;
extern NSString *const DORIC_ENTITY_ENV_CHANGE;

View File

@@ -85,3 +85,5 @@
NSString *const DORIC_ENTITY_HIDDEN = @"__onHidden__";
NSString *const DORIC_ENTITY_BUILD = @"__build__";
NSString *const DORIC_ENTITY_ENV_CHANGE = @"__onEnvChanged__";