iOS:add monitor

This commit is contained in:
pengfei.zhou 2020-01-11 11:01:38 +08:00 committed by osborn
parent 0aaf105f53
commit caea154377
9 changed files with 96 additions and 19 deletions

View File

@ -76,8 +76,8 @@ public class DoricNativeDriver implements IDoricDriver {
try {
return doricJSEngine.invokeDoricMethod(method, args);
} catch (Exception e) {
doricJSEngine.onException(e);
doricJSEngine.onLog(Log.ERROR, String.format("invokeDoricMethod(%s,...),error is %s", method, e.getLocalizedMessage()));
doricJSEngine.getRegistry().onException(e);
doricJSEngine.getRegistry().onLog(Log.ERROR, String.format("invokeDoricMethod(%s,...),error is %s", method, e.getLocalizedMessage()));
return new JSDecoder(null);
}
}
@ -106,8 +106,8 @@ public class DoricNativeDriver implements IDoricDriver {
doricJSEngine.prepareContext(contextId, script, source);
return true;
} catch (Exception e) {
doricJSEngine.onException(e);
doricJSEngine.onLog(Log.ERROR, String.format("createContext %s error is %s", source, e.getLocalizedMessage()));
doricJSEngine.getRegistry().onException(e);
doricJSEngine.getRegistry().onLog(Log.ERROR, String.format("createContext %s error is %s", source, e.getLocalizedMessage()));
return false;
}
}
@ -123,8 +123,8 @@ public class DoricNativeDriver implements IDoricDriver {
doricJSEngine.destroyContext(contextId);
return true;
} catch (Exception e) {
doricJSEngine.onException(e);
doricJSEngine.onLog(Log.ERROR, String.format("destroyContext %s error is %s", contextId, e.getLocalizedMessage()));
doricJSEngine.getRegistry().onException(e);
doricJSEngine.getRegistry().onLog(Log.ERROR, String.format("destroyContext %s error is %s", contextId, e.getLocalizedMessage()));
return false;
}
}

View File

@ -28,3 +28,4 @@
#import "DoricPromise.h"
#import "DoricLibrary.h"
#import "DoricNativePlugin.h"
#import "DoricMonitorProtocol.h"

View File

@ -78,6 +78,7 @@ + (instancetype)instance {
[ret setupResult:jsValue];
} @catch (NSException *exception) {
[ret setupError:exception];
[self.jsExecutor.registry onException:exception];
}
});
return ret;
@ -110,6 +111,7 @@ - (DoricAsyncResult *)invokeContextEntity:(NSString *)contextId method:(NSString
[ret setupResult:jsValue];
} @catch (NSException *exception) {
[ret setupError:exception];
[self.jsExecutor.registry onException:exception];
}
});
return ret;
@ -132,6 +134,7 @@ - (DoricAsyncResult *)invokeContextEntity:(NSString *)contextId method:(NSString
[ret setupResult:jsValue];
} @catch (NSException *exception) {
[ret setupError:exception];
[self.jsExecutor.registry onException:exception];
}
});
return ret;
@ -145,9 +148,10 @@ - (DoricAsyncResult *)createContext:(NSString *)contextId script:(NSString *)scr
if (!self) return;
@try {
[self.jsExecutor prepareContext:contextId script:script source:source];
[ret setupResult:[NSNumber numberWithBool:YES]];
[ret setupResult:@YES];
} @catch (NSException *exception) {
[ret setupError:exception];
[self.jsExecutor.registry onException:exception];
}
});
return ret;
@ -161,9 +165,10 @@ - (DoricAsyncResult *)destroyContext:(NSString *)contextId {
if (!self) return;
@try {
[self.jsExecutor destroyContext:contextId];
[ret setupResult:[NSNumber numberWithBool:YES]];
[ret setupResult:@YES];
} @catch (NSException *exception) {
[ret setupError:exception];
[self.jsExecutor.registry onException:exception];
}
});
return ret;

View File

@ -0,0 +1,17 @@
//
// Created by pengfei.zhou on 2020/1/10.
//
#import <Foundation/Foundation.h>
typedef NS_ENUM(NSInteger, DoricLogType) {
DoricLogTypeDebug = 0,
DoricLogTypeWarning = 1,
DoricLogTypeError = 2,
};
@protocol DoricMonitorProtocol <NSObject>
- (void)onException:(NSException *)exception;
- (void)onLog:(DoricLogType)type message:(NSString *)message;
@end

View File

@ -21,11 +21,12 @@
//
#import <Foundation/Foundation.h>
#import "DoricMonitorProtocol.h"
NS_ASSUME_NONNULL_BEGIN
@class DoricLibrary;
@interface DoricRegistry : NSObject
@interface DoricRegistry : NSObject <DoricMonitorProtocol>
- (NSString *)acquireJSBundle:(NSString *)name;
@ -44,6 +45,8 @@ NS_ASSUME_NONNULL_BEGIN
- (NSDictionary *)environmentVariables;
- (void)registerMonitor:(id <DoricMonitorProtocol>)monitor;
+ (void)register:(DoricLibrary *)library;
@end

View File

@ -47,6 +47,7 @@
#import "DoricDraggableNode.h"
#import "DoricLibrary.h"
#import "DoricNotificationPlugin.h"
#import "DoricUtil.h"
@interface DoricLibraries : NSObject
@property(nonatomic, strong) NSMutableSet <DoricLibrary *> *libraries;
@ -73,12 +74,26 @@ + (instancetype)instance {
@end
@interface DoricDefaultMonitor : NSObject <DoricMonitorProtocol>
@end
@implementation DoricDefaultMonitor
- (void)onException:(NSException *)exception {
DoricLog(@"DefaultMonitor - onException - %@", exception.reason);
}
- (void)onLog:(DoricLogType)type message:(NSString *)message {
DoricLog(message);
}
@end
@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;
@end
@implementation DoricRegistry
@ -97,6 +112,8 @@ - (instancetype)init {
[DoricLibraries.instance.libraries enumerateObjectsUsingBlock:^(DoricLibrary *obj, BOOL *stop) {
[obj load:self];
}];
_monitors = [NSMutableSet new];
[self registerMonitor:[DoricDefaultMonitor new]];
}
return self;
}
@ -161,4 +178,20 @@ - (void)setEnvironment:(NSString *)key variable:(id)value {
- (NSDictionary *)environmentVariables {
return self.envVariables;
}
- (void)registerMonitor:(id <DoricMonitorProtocol>)monitor {
[self.monitors addObject:monitor];
}
- (void)onException:(NSException *)exception {
for (id <DoricMonitorProtocol> monitor in self.monitors) {
[monitor onException:exception];
}
}
- (void)onLog:(DoricLogType)type message:(NSString *)message {
for (id <DoricMonitorProtocol> monitor in self.monitors) {
[monitor onLog:type message:message];
}
}
@end

View File

@ -70,7 +70,13 @@ - (void)initJSExecutor {
[self.jsExecutor injectGlobalJSObject:INJECT_ENVIRONMENT obj:[envDic copy]];
[self.jsExecutor injectGlobalJSObject:INJECT_LOG obj:^(NSString *type, NSString *message) {
DoricLog(@"JS:%@", message);
if ([type isEqualToString:@"e"]) {
[self.registry onLog:DoricLogTypeError message:message];
} else if ([type isEqualToString:@"w"]) {
[self.registry onLog:DoricLogTypeWarning message:message];
} else {
[self.registry onLog:DoricLogTypeDebug message:message];
}
}];
[self.jsExecutor injectGlobalJSObject:INJECT_EMPTY obj:^() {
@ -80,14 +86,15 @@ - (void)initJSExecutor {
if (!self) return NO;
NSString *content = [self.registry acquireJSBundle:name];
if (!content) {
DoricLog(@"require js bundle:%@ is empty", name);
[self.registry onLog:DoricLogTypeError message:[NSString stringWithFormat:@"require js bundle:%@ is empty", name]];
return NO;
}
@try {
[self.jsExecutor loadJSScript:[self packageModuleScript:name content:content]
source:[@"Module://" stringByAppendingString:name]];
} @catch (NSException *e) {
DoricLog(@"require js bundle:%@ error,for %@", name, e.reason);
[self.registry onLog:DoricLogTypeError
message:[NSString stringWithFormat:@"require js bundle:%@ error,for %@", name, e.reason]];
}
return YES;
}];
@ -119,11 +126,16 @@ - (void)initJSExecutor {
}
- (void)initDoricEnvironment {
[self loadBuiltinJS:DORIC_BUNDLE_SANDBOX];
NSString *path = [DoricBundle() pathForResource:DORIC_BUNDLE_LIB ofType:@"js"];
NSString *jsContent = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
[self.jsExecutor loadJSScript:[self packageModuleScript:DORIC_MODULE_LIB content:jsContent]
source:[@"Module://" stringByAppendingString:DORIC_MODULE_LIB]];
@try {
[self loadBuiltinJS:DORIC_BUNDLE_SANDBOX];
NSString *path = [DoricBundle() pathForResource:DORIC_BUNDLE_LIB ofType:@"js"];
NSString *jsContent = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
[self.jsExecutor loadJSScript:[self packageModuleScript:DORIC_MODULE_LIB content:jsContent]
source:[@"Module://" stringByAppendingString:DORIC_MODULE_LIB]];
} @catch (NSException *exception) {
[self.registry onException:exception];
}
}
- (void)loadBuiltinJS:(NSString *)fileName {
@ -184,7 +196,9 @@ - (void)callbackTimer:(NSTimer *)timer {
@try {
[self invokeDoricMethod:DORIC_TIMER_CALLBACK, timerId, nil];
} @catch (NSException *exception) {
DoricLog(@"Timer Callback error:%@", exception.reason);
[self.registry onException:exception];
[self.registry onLog:DoricLogTypeError
message:[NSString stringWithFormat:@"Timer Callback error:%@", exception.reason]];
}
if (![repeat boolValue]) {
[self.timers removeObjectForKey:[timerId stringValue]];

View File

@ -87,6 +87,7 @@ - (id)findClass:(Class)clz target:(id)target context:(DoricContext *)context met
[invocation invoke];
} @catch (NSException *exception) {
DoricLog(@"CallNative Error:%@", exception.reason);
[context.driver.registry onException:exception];
}
};
@ -102,6 +103,8 @@ - (id)findClass:(Class)clz target:(id)target context:(DoricContext *)context met
ret = [JSValue valueWithObject:[returnValue copy] inContext:[JSContext currentContext]];
} else {
DoricLog(@"CallNative Error:%@", @"Must return object type");
[context.driver.registry onLog:DoricLogTypeError
message:[NSString stringWithFormat:@"CallNative Error:%@", @"Must return object type"]];
ret = nil;
}
}

View File

@ -108,6 +108,7 @@ - (id)findClass:(Class)clz target:(id)target method:(NSString *)name promise:(Do
[invocation invoke];
} @catch (NSException *exception) {
DoricLog(@"CallNative Error:%@", exception.reason);
[self.doricContext.driver.registry onException:exception];
}
};
dispatch_async(dispatch_get_main_queue(), ^{