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/DoricRegistry.m

190 lines
7.2 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.
*/
//
// DoricRegistry.m
// Doric
//
// Created by pengfei.zhou on 2019/7/27.
//
#import "DoricRegistry.h"
#import "DoricModalPlugin.h"
#import "DoricNetworkPlugin.h"
#import "DoricShaderPlugin.h"
#import "DoricStackNode.h"
#import "DoricVLayoutNode.h"
#import "DoricHLayoutNode.h"
#import "DoricTextNode.h"
#import "DoricImageNode.h"
#import "DoricListNode.h"
#import "DoricListItemNode.h"
#import "DoricScrollerNode.h"
#import "DoricSliderNode.h"
#import "DoricSlideItemNode.h"
#import "DoricStoragePlugin.h"
#import "DoricNavigatorPlugin.h"
#import "DoricNavBarPlugin.h"
#import "DoricRefreshableNode.h"
#import "DoricFlowLayoutItemNode.h"
#import "DoricFlowLayoutNode.h"
#import "DoricPopoverPlugin.h"
#import "DoricAnimatePlugin.h"
2019-12-07 12:47:22 +08:00
#import "DoricNestedSliderNode.h"
2019-12-11 14:14:20 +08:00
#import "DoricInputNode.h"
2020-01-03 18:06:57 +08:00
#import "DoricDraggableNode.h"
2019-12-11 11:08:45 +08:00
#import "DoricLibrary.h"
2020-01-09 09:54:08 +08:00
#import "DoricNotificationPlugin.h"
2020-01-14 11:00:22 +08:00
#import "DoricStatusBarPlugin.h"
2020-02-13 22:18:13 +08:00
#import "DoricCoordinatorPlugin.h"
2020-03-13 13:01:21 +08:00
#import "DoricSwitchNode.h"
2020-03-19 15:07:34 +08:00
#import "DoricNotchPlugin.h"
2020-04-09 17:07:24 +08:00
#import "DoricFlexNode.h"
2021-03-18 18:48:05 +08:00
#import "DoricKeyboardPlugin.h"
2021-07-07 12:44:40 +08:00
#import "DoricJSEngine.h"
#import "DoricSingleton.h"
2021-09-22 10:28:13 +08:00
#import "DoricGestureContainerNode.h"
2021-10-25 16:01:44 +08:00
#import "DoricBundleResourceLoader.h"
#import "DoricBase64ResourceLoader.h"
#import "DoricLocalResourceLoader.h"
#import "DoricRemoteResourceLoader.h"
2021-10-25 16:51:45 +08:00
#import "DoricCommonBundleResourceLoader.h"
2019-12-04 13:29:26 +08:00
@interface DoricRegistry ()
@property(nonatomic, strong) NSMutableDictionary *plugins;
@property(nonatomic, strong) NSMutableDictionary *nodes;
2020-01-11 11:01:38 +08:00
@property(nonatomic, strong) NSMutableSet <id <DoricMonitorProtocol>> *monitors;
2021-07-07 12:44:40 +08:00
@property(nonatomic, weak) DoricJSEngine *jsEngine;
2019-12-04 13:29:26 +08:00
@end
@implementation DoricRegistry
2019-12-11 11:08:45 +08:00
+ (void)register:(DoricLibrary *)library {
[DoricSingleton.instance.libraries addObject:library];
for (DoricRegistry *registry in DoricSingleton.instance.registries) {
if (registry) {
[library load:registry];
}
}
2019-12-11 11:08:45 +08:00
}
2021-07-07 17:30:08 +08:00
- (instancetype)initWithJSEngine:(DoricJSEngine *)jsEngine {
2019-12-04 13:29:26 +08:00
if (self = [super init]) {
2021-07-07 17:30:08 +08:00
_jsEngine = jsEngine;
2020-01-10 15:11:14 +08:00
_plugins = [NSMutableDictionary new];
_nodes = [NSMutableDictionary new];
2020-02-19 15:09:13 +08:00
_monitors = [NSMutableSet new];
2021-10-25 16:01:44 +08:00
_loaderManager = [DoricResourceLoaderManager new];
2021-07-07 17:30:08 +08:00
[self innerRegister];
[DoricSingleton.instance.libraries enumerateObjectsUsingBlock:^(DoricLibrary *obj, BOOL *stop) {
2019-12-11 11:08:45 +08:00
[obj load:self];
}];
[jsEngine setEnvironmentValue:DoricSingleton.instance.envDic];
[DoricSingleton.instance.registries addObject:self];
2019-12-04 13:29:26 +08:00
}
return self;
}
- (void)innerRegister {
[self registerNativePlugin:DoricShaderPlugin.class withName:@"shader"];
[self registerNativePlugin:DoricModalPlugin.class withName:@"modal"];
[self registerNativePlugin:DoricNetworkPlugin.class withName:@"network"];
[self registerNativePlugin:DoricStoragePlugin.class withName:@"storage"];
[self registerNativePlugin:DoricNavigatorPlugin.class withName:@"navigator"];
[self registerNativePlugin:DoricNavBarPlugin.class withName:@"navbar"];
[self registerNativePlugin:DoricPopoverPlugin.class withName:@"popover"];
[self registerNativePlugin:DoricAnimatePlugin.class withName:@"animate"];
2020-01-09 09:54:08 +08:00
[self registerNativePlugin:DoricNotificationPlugin.class withName:@"notification"];
2021-03-18 18:48:05 +08:00
[self registerNativePlugin:DoricKeyboardPlugin.class withName:@"keyboard"];
2020-01-14 11:00:22 +08:00
[self registerNativePlugin:DoricStatusBarPlugin.class withName:@"statusbar"];
2020-02-13 22:18:13 +08:00
[self registerNativePlugin:DoricCoordinatorPlugin.class withName:@"coordinator"];
2020-03-19 15:07:34 +08:00
[self registerNativePlugin:DoricNotchPlugin.class withName:@"notch"];
2019-12-04 13:29:26 +08:00
[self registerViewNode:DoricStackNode.class withName:@"Stack"];
[self registerViewNode:DoricVLayoutNode.class withName:@"VLayout"];
[self registerViewNode:DoricHLayoutNode.class withName:@"HLayout"];
[self registerViewNode:DoricTextNode.class withName:@"Text"];
[self registerViewNode:DoricImageNode.class withName:@"Image"];
[self registerViewNode:DoricListNode.class withName:@"List"];
[self registerViewNode:DoricListItemNode.class withName:@"ListItem"];
[self registerViewNode:DoricScrollerNode.class withName:@"Scroller"];
[self registerViewNode:DoricSliderNode.class withName:@"Slider"];
[self registerViewNode:DoricSlideItemNode.class withName:@"SlideItem"];
[self registerViewNode:DoricRefreshableNode.class withName:@"Refreshable"];
[self registerViewNode:DoricFlowLayoutItemNode.class withName:@"FlowLayoutItem"];
[self registerViewNode:DoricFlowLayoutNode.class withName:@"FlowLayout"];
2019-12-07 12:47:22 +08:00
[self registerViewNode:DoricNestedSliderNode.class withName:@"NestedSlider"];
2019-12-11 14:14:20 +08:00
[self registerViewNode:DoricInputNode.class withName:@"Input"];
2020-01-03 18:06:57 +08:00
[self registerViewNode:DoricDraggableNode.class withName:@"Draggable"];
2020-03-13 13:01:21 +08:00
[self registerViewNode:DoricSwitchNode.class withName:@"Switch"];
2020-04-09 17:07:24 +08:00
[self registerViewNode:DoricFlexNode.class withName:@"FlexLayout"];
2021-09-22 10:28:13 +08:00
[self registerViewNode:DoricGestureContainerNode.class withName:@"GestureContainer"];
2021-10-25 16:01:44 +08:00
[self.loaderManager registerLoader:[[DoricBundleResourceLoader alloc]
initWithResourceType:@"mainBundle"
bundle:[NSBundle mainBundle]]];
[self.loaderManager registerLoader:[DoricLocalResourceLoader new]];
[self.loaderManager registerLoader:[DoricRemoteResourceLoader new]];
[self.loaderManager registerLoader:[DoricBase64ResourceLoader new]];
2021-10-25 16:51:45 +08:00
[self.loaderManager registerLoader:[DoricCommonBundleResourceLoader new]];
2019-12-04 13:29:26 +08:00
}
- (void)registerJSBundle:(NSString *)bundle withName:(NSString *)name {
2021-08-24 11:01:11 +08:00
DoricSingleton.instance.bundles[name] = bundle;
2019-12-04 13:29:26 +08:00
}
- (NSString *)acquireJSBundle:(NSString *)name {
2021-08-24 11:01:11 +08:00
return DoricSingleton.instance.bundles[name];
2019-12-04 13:29:26 +08:00
}
- (void)registerNativePlugin:(Class)pluginClass withName:(NSString *)name {
self.plugins[name] = pluginClass;
}
- (Class)acquireNativePlugin:(NSString *)name {
return self.plugins[name];
}
- (void)registerViewNode:(Class)nodeClass withName:(NSString *)name {
self.nodes[name] = nodeClass;
}
- (Class)acquireViewNode:(NSString *)name {
return self.nodes[name];
}
2021-07-07 17:30:08 +08:00
- (void)innerSetEnvironmentValue:(NSDictionary *)value {
[self.jsEngine setEnvironmentValue:value];
2020-01-10 15:11:14 +08:00
}
2020-01-11 11:01:38 +08:00
- (void)registerMonitor:(id <DoricMonitorProtocol>)monitor {
[self.monitors addObject:monitor];
}
- (void)onException:(NSException *)exception inContext:(DoricContext *)context {
2020-01-11 11:01:38 +08:00
for (id <DoricMonitorProtocol> monitor in self.monitors) {
[monitor onException:exception inContext:context];
2020-01-11 11:01:38 +08:00
}
}
- (void)onLog:(DoricLogType)type message:(NSString *)message {
for (id <DoricMonitorProtocol> monitor in self.monitors) {
[monitor onLog:type message:message];
}
}
2019-12-04 13:29:26 +08:00
@end