refact: use DoricSingleton to hold all static or singleton objects

This commit is contained in:
pengfei.zhou
2021-07-21 17:56:03 +08:00
committed by osborn
parent 7d4d6713c6
commit 61c262bcc6
30 changed files with 320 additions and 221 deletions

View File

@@ -49,4 +49,14 @@
* */
+ (void)addJSLoader:(id <DoricLoaderProtocol>)loader;
+ (void)enablePerformance:(BOOL)enable;
+ (BOOL)isEnablePerformance;
+ (void)enableRenderSnapshot:(BOOL)enable;
+ (BOOL)isEnableRenderSnapshot;
+ (void)setEnvironmentValue:(NSDictionary *)value;
@end

View File

@@ -20,6 +20,7 @@
// Created by pengfei.zhou on 2020/2/28.
//
#import "Doric.h"
#import "DoricSingleton.h"
@implementation Doric
@@ -28,6 +29,26 @@ + (void)registerLibrary:(DoricLibrary *)library {
}
+ (void)addJSLoader:(id <DoricLoaderProtocol>)loader {
[[DoricJSLoaderManager instance] addJSLoader:loader];
[DoricSingleton.instance.jsLoaderManager addJSLoader:loader];
}
+ (void)enablePerformance:(BOOL)enable {
DoricSingleton.instance.enablePerformance = enable;
}
+ (BOOL)isEnablePerformance {
return DoricSingleton.instance.enablePerformance;
}
+ (void)enableRenderSnapshot:(BOOL)enable {
DoricSingleton.instance.enableRecordSnapshot = enable;
}
+ (BOOL)isEnableRenderSnapshot {
return DoricSingleton.instance.enableRecordSnapshot;
}
+ (void)setEnvironmentValue:(NSDictionary *)value {
[DoricSingleton.instance setEnvironmentValue:value];
}
@end

View File

@@ -27,12 +27,13 @@
#import "DoricExtensions.h"
#import "DoricNativeDriver.h"
#import "DoricUtil.h"
#import "DoricSingleton.h"
@implementation DoricContext
- (instancetype)initWithScript:(NSString *)script source:(NSString *)source extra:(NSString *)extra {
if (self = [super init]) {
_driver = [DoricNativeDriver instance];
_driver = DoricSingleton.instance.nativeDriver;
_pluginInstanceMap = [NSMutableDictionary new];
_script = script;
_source = source;
@@ -84,7 +85,7 @@ - (DoricAsyncResult *)callEntity:(NSString *)method withArgumentsArray:(NSArray
}
- (void)init:(NSString *)initData {
if ([DoricRegistry isEnableRenderSnapshot]) {
if (DoricSingleton.instance.enableRecordSnapshot) {
[self callEntity:@"__enableSnapshot__" withArgumentsArray:@[]];
}
self.extra = initData;
@@ -135,7 +136,6 @@ - (UIViewController *)vc {
return _vc;
}
- (void)dispatchToMainQueue:(_Nonnull dispatch_block_t)block {
dispatch_async(dispatch_get_main_queue(), ^{
@try {

View File

@@ -21,6 +21,7 @@
//
#import "DoricContextManager.h"
#import "DoricSingleton.h"
@interface DoricContextManager ()
@@ -43,12 +44,7 @@ - (instancetype)init {
}
+ (instancetype)instance {
static DoricContextManager *_instance;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
_instance = [[DoricContextManager alloc] init];
});
return _instance;
return DoricSingleton.instance.contextManager;
}
- (void)createContext:(DoricContext *)context script:(NSString *)script source:(NSString *)source {

View File

@@ -32,7 +32,6 @@ typedef NS_ENUM(NSInteger, DoricQueueMode) {
NS_ASSUME_NONNULL_BEGIN
@interface DoricNativeDriver : NSObject <DoricDriverProtocol>
+ (instancetype)instance;
@end
NS_ASSUME_NONNULL_END

View File

@@ -45,15 +45,6 @@ - (DoricRegistry *)registry {
return self.jsExecutor.registry;
}
+ (instancetype)instance {
static DoricNativeDriver *_instance;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
_instance = [[DoricNativeDriver alloc] init];
});
return _instance;
}
- (DoricAsyncResult *)invokeDoricMethod:(NSString *)method argumentsArray:(NSArray *)args {
id contextId = args.count > 0 ? args[0] : nil;
DoricPerformanceProfile *profile = nil;

View File

@@ -40,7 +40,6 @@ NS_ASSUME_NONNULL_BEGIN
- (void)registerJSBundle:(NSString *)bundle withName:(NSString *)name;
- (void)registerNativePlugin:(Class)pluginClass withName:(NSString *)name;
- (Class)acquireNativePlugin:(NSString *)name;
@@ -49,19 +48,12 @@ NS_ASSUME_NONNULL_BEGIN
- (Class)acquireViewNode:(NSString *)name;
+ (void)setEnvironmentValue:(NSDictionary *)value;
- (void)registerMonitor:(id <DoricMonitorProtocol>)monitor;
- (void)innerSetEnvironmentValue:(NSDictionary *)value;
+ (void)register:(DoricLibrary *)library;
+ (void)enablePerformance:(BOOL)enable;
+ (BOOL)isEnablePerformance;
+ (void)enableRenderSnapshot:(BOOL)enable;
+ (BOOL)isEnableRenderSnapshot;
@end
NS_ASSUME_NONNULL_END

View File

@@ -54,39 +54,7 @@
#import "DoricFlexNode.h"
#import "DoricKeyboardPlugin.h"
#import "DoricJSEngine.h"
@interface DoricLibraries : NSObject
@property(nonatomic, strong) NSMutableSet <DoricLibrary *> *libraries;
@property(nonatomic, strong) NSHashTable<DoricRegistry *> *registries;
@property(nonatomic, strong) NSMutableDictionary *envDic;
@property(nonatomic, assign) BOOL enablePerformance;
@property(nonatomic, assign) BOOL enableRecordSnapshot;
+ (instancetype)instance;
@end
@implementation DoricLibraries
- (instancetype)init {
if (self = [super init]) {
_libraries = [NSMutableSet new];
_registries = [NSHashTable new];
_envDic = [NSMutableDictionary new];
_enablePerformance = NO;
_enableRecordSnapshot = NO;
}
return self;
}
+ (instancetype)instance {
static DoricLibraries *_instance;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
_instance = [[DoricLibraries alloc] init];
});
return _instance;
}
@end
#import "DoricSingleton.h"
@interface DoricRegistry ()
@@ -100,22 +68,14 @@ @interface DoricRegistry ()
@implementation DoricRegistry
+ (void)register:(DoricLibrary *)library {
[DoricLibraries.instance.libraries addObject:library];
for (DoricRegistry *registry in DoricLibraries.instance.registries) {
[DoricSingleton.instance.libraries addObject:library];
for (DoricRegistry *registry in DoricSingleton.instance.registries) {
if (registry) {
[library load:registry];
}
}
}
+ (void)setEnvironmentValue:(NSDictionary *)value {
[DoricLibraries.instance.envDic addEntriesFromDictionary:value];
for (DoricRegistry *registry in DoricLibraries.instance.registries) {
if (registry) {
[registry innerSetEnvironmentValue:value];
}
}
}
- (instancetype)initWithJSEngine:(DoricJSEngine *)jsEngine {
if (self = [super init]) {
@@ -125,31 +85,15 @@ - (instancetype)initWithJSEngine:(DoricJSEngine *)jsEngine {
_nodes = [NSMutableDictionary new];
_monitors = [NSMutableSet new];
[self innerRegister];
[DoricLibraries.instance.libraries enumerateObjectsUsingBlock:^(DoricLibrary *obj, BOOL *stop) {
[DoricSingleton.instance.libraries enumerateObjectsUsingBlock:^(DoricLibrary *obj, BOOL *stop) {
[obj load:self];
}];
[jsEngine setEnvironmentValue:DoricLibraries.instance.envDic];
[DoricLibraries.instance.registries addObject:self];
[jsEngine setEnvironmentValue:DoricSingleton.instance.envDic];
[DoricSingleton.instance.registries addObject:self];
}
return self;
}
+ (void)enablePerformance:(BOOL)enable {
DoricLibraries.instance.enablePerformance = enable;
}
+ (BOOL)isEnablePerformance {
return DoricLibraries.instance.enablePerformance;
}
+ (void)enableRenderSnapshot:(BOOL)enable {
DoricLibraries.instance.enableRecordSnapshot = enable;
}
+ (BOOL)isEnableRenderSnapshot {
return DoricLibraries.instance.enableRecordSnapshot;
}
- (void)innerRegister {
[self registerNativePlugin:DoricShaderPlugin.class withName:@"shader"];
[self registerNativePlugin:DoricModalPlugin.class withName:@"modal"];

View File

@@ -0,0 +1,41 @@
/*
* Copyright [2021] [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.
*/
//
// Created by pengfei.zhou on 2021/7/21.
//
#import <Foundation/Foundation.h>
@class DoricLibrary;
@class DoricRegistry;
@class DoricJSLoaderManager;
@class DoricNativeDriver;
@class DoricContextManager;
@interface DoricSingleton : NSObject
@property(nonatomic, strong) NSMutableSet <DoricLibrary *> *libraries;
@property(nonatomic, strong) NSHashTable<DoricRegistry *> *registries;
@property(nonatomic, strong) NSMutableDictionary *envDic;
@property(nonatomic, assign) BOOL enablePerformance;
@property(nonatomic, assign) BOOL enableRecordSnapshot;
@property(nonatomic, strong) DoricJSLoaderManager *jsLoaderManager;
@property(nonatomic, strong) DoricNativeDriver *nativeDriver;
@property(nonatomic, strong) DoricContextManager *contextManager;
+ (instancetype)instance;
- (void)setEnvironmentValue:(NSDictionary *)value;
@end

View File

@@ -0,0 +1,65 @@
/*
* Copyright [2021] [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.
*/
//
// Created by pengfei.zhou on 2021/7/21.
//
#import "DoricSingleton.h"
#import "DoricRegistry.h"
#import "DoricJSLoaderManager.h"
#import "DoricNativeDriver.h"
#import "DoricContextManager.h"
@implementation DoricSingleton
- (instancetype)init {
if (self = [super init]) {
_libraries = [NSMutableSet new];
_registries = [NSHashTable new];
_envDic = [NSMutableDictionary new];
_enablePerformance = NO;
_enableRecordSnapshot = NO;
_jsLoaderManager = [DoricJSLoaderManager new];
_contextManager = [DoricContextManager new];
}
return self;
}
+ (instancetype)instance {
static DoricSingleton *_instance;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
_instance = [DoricSingleton new];
});
return _instance;
}
- (DoricNativeDriver *)nativeDriver {
if (!_nativeDriver) {
_nativeDriver = [DoricNativeDriver new];
}
return _nativeDriver;
}
- (void)setEnvironmentValue:(NSDictionary *)value {
[self.envDic addEntriesFromDictionary:value];
for (DoricRegistry *registry in self.registries) {
if (registry) {
[registry innerSetEnvironmentValue:value];
}
}
}
@end

View File

@@ -22,6 +22,7 @@
#import "UIView+Doric.h"
#import "DoricExtensions.h"
#import "DoricUtil.h"
#import "DoricSingleton.h"
NSString *const DORIC_MASK_RETRY = @"doric_mask_retry";
@@ -202,7 +203,7 @@ - (void)hideMask {
- (void)loadJSBundle {
[self showLoading];
DoricAsyncResult <NSString *> *result = [DoricJSLoaderManager.instance request:self.source];
DoricAsyncResult <NSString *> *result = [DoricSingleton.instance.jsLoaderManager request:self.source];
result.resultCallback = ^(NSString *result) {
dispatch_async(dispatch_get_main_queue(), ^{
[self hideMask];

View File

@@ -25,8 +25,6 @@
#import "DoricAsyncResult.h"
@interface DoricJSLoaderManager : NSObject
+ (instancetype)instance;
- (void)addJSLoader:(id <DoricLoaderProtocol>)loader;
- (DoricAsyncResult <NSString *> *)request:(NSString *)source;

View File

@@ -30,15 +30,6 @@ @interface DoricJSLoaderManager ()
@end
@implementation DoricJSLoaderManager
+ (instancetype)instance {
static DoricJSLoaderManager *_instance;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
_instance = [DoricJSLoaderManager new];
});
return _instance;
}
- (instancetype)init {
if (self = [super init]) {
_loaders = [[NSSet alloc] initWithArray:@[

View File

@@ -22,6 +22,7 @@
#import "DoricPerformanceProfile.h"
#import "DoricRegistry.h"
#import "DoricSingleton.h"
@interface DoricPerformanceProfile ()
@property(nonatomic, strong) dispatch_queue_t anchorQueue;
@@ -36,7 +37,7 @@ - (instancetype)initWithName:(NSString *)name {
_name = name;
_anchorQueue = dispatch_queue_create("doric.performance.profile", DISPATCH_QUEUE_SERIAL);
_anchorMap = [NSMutableDictionary new];
_enable = [DoricRegistry isEnablePerformance];
_enable = DoricSingleton.instance.enablePerformance;
_hooks = [NSHashTable new];
}
return self;