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/Pod/Classes/DoricContext.m

192 lines
6.3 KiB
Mathematica
Raw Normal View History

2019-12-04 13:29:26 +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.
*/
//
// DoricContext.m
// Doric
//
// Created by pengfei.zhou on 2019/7/25.
//
#import "DoricContext.h"
#import "DoricContextManager.h"
#import "DoricRootNode.h"
#import "DoricConstant.h"
#import "DoricExtensions.h"
#import "DoricNativeDriver.h"
#import "DoricUtil.h"
#import "DoricSingleton.h"
#import "DoricShaderPlugin.h"
#import <JavaScriptCore/JavaScriptCore.h>
2019-12-04 13:29:26 +08:00
@implementation DoricContext
2019-12-09 20:32:33 +08:00
- (instancetype)initWithScript:(NSString *)script source:(NSString *)source extra:(NSString *)extra {
2019-12-04 13:29:26 +08:00
if (self = [super init]) {
_driver = DoricSingleton.instance.nativeDriver;
2019-12-04 13:29:26 +08:00
_pluginInstanceMap = [NSMutableDictionary new];
_script = script;
_source = source;
_initialParams = [@{@"width": @(0), @"height": @(0)} mutableCopy];
[[DoricContextManager instance] createContext:self script:script source:source];
_headNodes = [NSMutableDictionary new];
DoricRootNode *rootNode = [[DoricRootNode alloc] initWithContext:self];
_rootNode = rootNode;
_cachedResources = [[NSMapTable alloc] initWithKeyOptions:NSPointerFunctionsCopyIn
valueOptions:NSPointerFunctionsWeakMemory
capacity:0];
[self init:extra];
2020-04-20 11:03:48 +08:00
[self callEntity:DORIC_ENTITY_CREATE withArgumentsArray:@[]];
2019-12-04 13:29:26 +08:00
}
return self;
}
- (DoricViewNode *)targetViewNode:(NSString *)viewId {
if ([self.rootNode.viewId isEqualToString:viewId]) {
return self.rootNode;
} else {
2020-01-09 19:00:53 +08:00
for (NSMutableDictionary <NSString *, DoricViewNode *> *map in self.headNodes.allValues) {
if ([[map allKeys] containsObject:viewId]) {
return map[viewId];
}
}
2019-12-04 13:29:26 +08:00
}
2020-01-09 19:00:53 +08:00
return nil;
2019-12-04 13:29:26 +08:00
}
- (void)dealloc {
2020-05-06 17:34:48 +08:00
_destroyed = YES;
2019-12-04 13:29:26 +08:00
[[DoricContextManager instance] destroyContext:self];
2020-04-22 13:55:07 +08:00
[self callEntity:DORIC_ENTITY_DESTROY withArgumentsArray:@[]];
[self.driver destroyContext:self.contextId];
[self.cachedResources removeAllObjects];
2019-12-04 13:29:26 +08:00
}
- (DoricAsyncResult *)callEntity:(NSString *)method, ... {
va_list args;
va_start(args, method);
DoricAsyncResult *ret = [self.driver invokeContextEntity:self.contextId method:method arguments:args];
va_end(args);
return ret;
}
- (DoricAsyncResult *)callEntity:(NSString *)method withArguments:(va_list)args {
return [self.driver invokeContextEntity:self.contextId method:method arguments:args];
}
- (DoricAsyncResult *)callEntity:(NSString *)method withArgumentsArray:(NSArray *)args {
return [self.driver invokeContextEntity:self.contextId method:method argumentsArray:args];
}
- (void)init:(NSString *)initData {
if (DoricSingleton.instance.enableRecordSnapshot) {
2021-07-14 15:23:33 +08:00
[self callEntity:@"__enableSnapshot__" withArgumentsArray:@[]];
}
self.extra = initData;
if (initData) {
2020-04-20 11:03:48 +08:00
[self callEntity:DORIC_ENTITY_INIT withArgumentsArray:@[initData]];
}
}
- (void)build:(CGSize)size {
2019-12-04 13:29:26 +08:00
[self.initialParams also:^(NSMutableDictionary *it) {
it[@"width"] = @(size.width);
it[@"height"] = @(size.height);
2019-12-04 13:29:26 +08:00
}];
2020-04-20 11:03:48 +08:00
[self callEntity:DORIC_ENTITY_BUILD withArgumentsArray:@[self.initialParams]];
2019-12-04 13:29:26 +08:00
}
- (void)reload:(NSString *)script {
[[self.driver destroyContext:self.contextId] waitUntilResult];
2019-12-04 13:29:26 +08:00
self.rootNode.viewId = nil;
2021-03-04 10:02:29 +08:00
[self.rootNode clearSubModel];
[self.rootNode.view.subviews forEach:^(__kindof UIView *obj) {
[obj removeFromSuperview];
}];
2019-12-04 13:29:26 +08:00
self.script = script;
_destroyed = YES;
[self dispatchToMainQueue:^{
_destroyed = NO;
[self.driver createContext:self.contextId script:script source:self.source];
[self init:self.extra];
[self callEntity:DORIC_ENTITY_CREATE withArgumentsArray:@[]];
[self callEntity:DORIC_ENTITY_BUILD withArgumentsArray:@[self.initialParams]];
[self onShow];
}];
2019-12-04 13:29:26 +08:00
}
- (void)onShow {
2020-04-20 11:03:48 +08:00
[self callEntity:DORIC_ENTITY_SHOW withArgumentsArray:@[]];
2019-12-04 13:29:26 +08:00
}
- (void)onHidden {
2020-04-20 11:03:48 +08:00
[self callEntity:DORIC_ENTITY_HIDDEN withArgumentsArray:@[]];
2019-12-04 13:29:26 +08:00
}
2021-07-07 12:44:40 +08:00
- (void)onEnvChanged {
[self callEntity:DORIC_ENTITY_ENV_CHANGE withArgumentsArray:@[]];
}
2020-04-21 18:52:54 +08:00
- (UIViewController *)vc {
if (!_vc) {
2020-04-21 18:52:54 +08:00
return [UIApplication sharedApplication].keyWindow.rootViewController;
}
return _vc;
}
- (void)dispatchToMainQueue:(_Nonnull dispatch_block_t)block {
dispatch_async(dispatch_get_main_queue(), ^{
@try {
block();
} @catch (NSException *exception) {
DoricLog(@"dispatchToMainQueue Error:%@", exception.reason);
[self.driver.registry onException:exception inContext:self];
}
});
}
- (DoricAsyncResult *)pureCallEntity:(NSString *)method withArgumentsArray:(NSArray *)args {
NSMutableArray *array = [[NSMutableArray alloc] init];
[array addObject:self.contextId];
[array addObject:method];
[array addObjectsFromArray:args];
DoricAsyncResult *ret = [self.driver invokeDoricMethod:DORIC_CONTEXT_INVOKE_PURE argumentsArray:array];
__weak typeof(self) __self = self;
ret.exceptionCallback = ^(NSException *e) {
__strong typeof(__self) self = __self;
[self.driver.registry
onException:e
inContext:self];
};
return ret;
}
- (void)renderSynchronously {
DoricAsyncResult *ret = [self pureCallEntity:DORIC_ENTITY_FETCH_DIRTY_DATA withArgumentsArray:@[]];
NSArray *array = [ret waitUntilResult:^(JSValue *models) {
return [models toArray];
}];
DoricShaderPlugin *shaderPlugin = self.pluginInstanceMap[@"Shader"];
if (!shaderPlugin) {
shaderPlugin = [[DoricShaderPlugin alloc] initWithContext:self];
self.pluginInstanceMap[@"Shader"] = shaderPlugin;
}
for (NSDictionary *model in array) {
[shaderPlugin render:model withPromise:nil];
}
}
2019-12-04 13:29:26 +08:00
@end