This repository has been archived on 2024-07-22. You can view files and clone it, but cannot push or open issues or pull requests.
Doric/doric-iOS/Devkit/Classes/DoricDev.m

181 lines
5.5 KiB
Mathematica
Raw Normal View History

2020-02-25 14:20:27 +08:00
/*
* Copyright [2019] [Doric.Pub]
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
//
// DoricDev.m
// Doric
//
// Created by jingpeng.wang on 2020/2/25.
//
2020-02-27 16:13:14 +08:00
#import <DoricCore/Doric.h>
#import <DoricCore/DoricNativeDriver.h>
2021-02-22 19:03:34 +08:00
#import <DoricCore/DoricContextManager.h>
2020-02-25 14:20:27 +08:00
#import "DoricDev.h"
2020-02-27 16:13:14 +08:00
#import "DoricDebugDriver.h"
2020-02-28 17:31:59 +08:00
#import "DoricDevViewController.h"
2021-02-05 19:36:25 +08:00
#import "DoricDevMonitor.h"
2021-02-22 19:03:34 +08:00
@interface DoricContextDebuggable : NSObject
@property(nonatomic, weak) DoricContext *doricContext;
@property(nonatomic, weak) id <DoricDriverProtocol> nativeDriver;
@property(nonatomic, weak) DoricWSClient *wsClient;
@end
@implementation DoricContextDebuggable
- (instancetype)initWithWSClient:(DoricWSClient *)client context:(DoricContext *)context {
if (self = [super init]) {
_wsClient = client;
_doricContext = context;
_nativeDriver = context.driver;
}
return self;
}
- (void)startDebug {
[self.doricContext setDriver:[[DoricDebugDriver alloc] initWithWSClient:self.wsClient]];
[self.doricContext reload:self.doricContext.script];
}
- (void)stopDebug:(BOOL)resume {
id <DoricDriverProtocol> driver = self.doricContext.driver;
if ([driver isKindOfClass:DoricDebugDriver.class]) {
}
if (resume) {
self.doricContext.driver = self.nativeDriver;
[self.doricContext reload:self.doricContext.script];
}
}
@end
2020-02-25 14:20:27 +08:00
@interface DoricDev ()
2021-02-22 19:03:34 +08:00
@property(nonatomic, strong) DoricContextDebuggable *debuggable;
2020-02-25 14:20:27 +08:00
@end
@implementation DoricDev
- (instancetype)init {
if (self = [super init]) {
2020-02-27 16:13:14 +08:00
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onOpenEvent) name:@"OpenEvent" object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onEOFEvent) name:@"EOFEvent" object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onConnectExceptionEvent) name:@"ConnectExceptionEvent" object:nil];
2021-02-07 15:58:11 +08:00
[DoricNativeDriver.instance.registry registerMonitor:[DoricDevMonitor new]];
2020-02-25 14:20:27 +08:00
}
return self;
}
+ (instancetype)instance {
static DoricDev *_instance;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
_instance = [[DoricDev alloc] init];
});
return _instance;
}
2020-02-28 17:31:59 +08:00
- (void)openDevMode {
DoricDevViewController *devViewController = [DoricDevViewController new];
UIViewController *viewController = [UIApplication sharedApplication].delegate.window.rootViewController;
UINavigationController *navigationController;
if ([viewController isKindOfClass:[UINavigationController class]]) {
navigationController = (UINavigationController *) viewController;
} else {
navigationController = viewController.navigationController;
}
[navigationController pushViewController:devViewController animated:NO];
}
- (void)closeDevMode {
2021-02-22 19:03:34 +08:00
if (self.wsClient) {
[self.wsClient close];
self.wsClient = nil;
2020-02-25 14:20:27 +08:00
}
}
2020-02-28 17:31:59 +08:00
- (BOOL)isInDevMode {
2021-02-22 19:03:34 +08:00
return self.wsClient != nil;
2020-02-26 14:51:02 +08:00
}
2020-02-28 17:31:59 +08:00
- (void)connectDevKit:(NSString *)url {
2021-02-22 19:03:34 +08:00
if (self.wsClient) {
[self.wsClient close];
2020-02-25 14:20:27 +08:00
}
2021-02-22 19:03:34 +08:00
self.wsClient = [[DoricWSClient alloc] initWithUrl:url];
2020-02-25 14:20:27 +08:00
}
2020-02-27 16:13:14 +08:00
- (void)onOpenEvent {
2020-04-03 10:19:13 +08:00
ShowToast(@"dev kit connected", DoricGravityBottom);
2020-02-27 16:13:14 +08:00
}
- (void)onEOFEvent {
2020-04-03 10:19:13 +08:00
ShowToast(@"dev kit eof exception", DoricGravityBottom);
2020-02-27 16:13:14 +08:00
}
- (void)onConnectExceptionEvent {
2020-04-03 10:19:13 +08:00
ShowToast(@"dev kit connection exception", DoricGravityBottom);
2020-02-27 16:13:14 +08:00
}
2021-02-22 19:03:34 +08:00
- (DoricContext *)matchContext:(NSString *)source {
for (DoricContext *context in [DoricContextManager.instance aliveContexts]) {
if ([source containsString:context.source] || [context.source isEqualToString:@"__dev__"]) {
return context;
2020-02-27 16:13:14 +08:00
}
}
2021-02-22 19:03:34 +08:00
return nil;
2020-02-27 16:13:14 +08:00
}
2021-02-22 19:03:34 +08:00
- (void)reload:(NSString *)source script:(NSString *)script {
DoricContext *context = [self matchContext:source];
if (context) {
if ([context.driver isKindOfClass:DoricDebugDriver.class]) {
DoricLog(@"Context source %@ in debugging,skip reload", source);
} else {
DoricLog(@"Context reload :id %@,source %@", context.contextId, source);
[context reload:script];
}
} else {
DoricLog(@"Cannot find context source %@ for reload", source);
}
2020-02-27 16:13:14 +08:00
}
2021-02-22 19:03:34 +08:00
- (void)startDebugging:(NSString *)source {
[self.debuggable stopDebug:YES];
DoricContext *context = [self matchContext:source];
if (context) {
[self.wsClient sendToDebugger:@"DEBUG_RES" payload:@{
@"contextId": context.contextId
}];
self.debuggable = [[DoricContextDebuggable alloc] initWithWSClient:self.wsClient context:context];
[self.debuggable startDebug];
} else {
DoricLog(@"Cannot find context source %@ for debugging", source);
[self.wsClient sendToDebugger:@"DEBUG_STOP" payload:@{
@"msg": @"Cannot find suitable alive context for debugging"
}];
}
};
- (void)stopDebugging:(BOOL)resume {
[self.wsClient sendToDebugger:@"DEBUG_STOP" payload:@{
@"msg": @"Stop debugging"
}];
[self.debuggable stopDebug:resume];
self.debuggable = nil;
2020-02-27 16:13:14 +08:00
}
2020-02-25 14:20:27 +08:00
@end