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

@ -26,6 +26,7 @@ import java.util.Map;
import pub.doric.Doric; import pub.doric.Doric;
import pub.doric.DoricRegistry; import pub.doric.DoricRegistry;
import pub.doric.DoricSingleton;
public class MyApplication extends Application { public class MyApplication extends Application {
@Override @Override
@ -41,10 +42,10 @@ public class MyApplication extends Application {
Map<String, Object> map = new HashMap<>(); Map<String, Object> map = new HashMap<>();
map.put("localeLanguage", context.getResources().getConfiguration().locale.getLanguage()); map.put("localeLanguage", context.getResources().getConfiguration().locale.getLanguage());
map.put("localeCountry", context.getResources().getConfiguration().locale.getCountry()); map.put("localeCountry", context.getResources().getConfiguration().locale.getCountry());
DoricRegistry.setEnvironmentValue(map); DoricSingleton.getInstance().setEnvironmentValue(map);
} }
}, intentFilter); }, intentFilter);
DoricRegistry.enablePerformance(true); Doric.enablePerformance(true);
DoricRegistry.enableRenderSnapshot(true); Doric.enableRenderSnapshot(true);
} }
} }

View File

@ -19,6 +19,7 @@ import pub.doric.Doric;
import pub.doric.DoricContext; import pub.doric.DoricContext;
import pub.doric.DoricContextManager; import pub.doric.DoricContextManager;
import pub.doric.DoricNativeDriver; import pub.doric.DoricNativeDriver;
import pub.doric.DoricSingleton;
import pub.doric.devkit.ui.DoricDevActivity; import pub.doric.devkit.ui.DoricDevActivity;
import pub.doric.devkit.util.SimulatorUtil; import pub.doric.devkit.util.SimulatorUtil;
import pub.doric.performance.DoricPerformanceProfile; import pub.doric.performance.DoricPerformanceProfile;
@ -51,8 +52,8 @@ public class DoricDev {
private DoricDev() { private DoricDev() {
this.isRunningInEmulator = SimulatorUtil.isSimulator(Doric.application()); this.isRunningInEmulator = SimulatorUtil.isSimulator(Doric.application());
DoricNativeDriver.getInstance().getRegistry().registerMonitor(new DoricDevMonitor()); DoricSingleton.getInstance().getNativeDriver().getRegistry().registerMonitor(new DoricDevMonitor());
DoricNativeDriver.getInstance().getRegistry().setGlobalPerformanceAnchorHook(new DoricDevPerformanceAnchorHook()); DoricSingleton.getInstance().getNativeDriver().getRegistry().setGlobalPerformanceAnchorHook(new DoricDevPerformanceAnchorHook());
} }
public static DoricDev getInstance() { public static DoricDev getInstance() {

View File

@ -54,9 +54,9 @@ import java.util.HashMap;
import java.util.Locale; import java.util.Locale;
import java.util.Map; import java.util.Map;
import pub.doric.Doric;
import pub.doric.DoricContext; import pub.doric.DoricContext;
import pub.doric.DoricContextManager; import pub.doric.DoricContextManager;
import pub.doric.DoricRegistry;
import pub.doric.devkit.DoricDebugDriver; import pub.doric.devkit.DoricDebugDriver;
import pub.doric.devkit.DoricDev; import pub.doric.devkit.DoricDev;
import pub.doric.devkit.R; import pub.doric.devkit.R;
@ -209,21 +209,21 @@ public class DoricDevActivity extends AppCompatActivity implements DoricDev.Stat
} }
}); });
SwitchCompat snapshotSwitch = findViewById(R.id.switch_snapshot); SwitchCompat snapshotSwitch = findViewById(R.id.switch_snapshot);
snapshotSwitch.setChecked(DoricRegistry.isEnableRenderSnapshot()); snapshotSwitch.setChecked(Doric.isEnableRenderSnapshot());
snapshotSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { snapshotSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override @Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
DoricRegistry.enableRenderSnapshot(isChecked); Doric.enableRenderSnapshot(isChecked);
} }
}); });
setSwitch(snapshotSwitch); setSwitch(snapshotSwitch);
SwitchCompat profileSwitch = findViewById(R.id.switch_profile); SwitchCompat profileSwitch = findViewById(R.id.switch_profile);
profileSwitch.setChecked(DoricRegistry.isEnablePerformance()); profileSwitch.setChecked(Doric.isEnablePerformance());
profileSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { profileSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override @Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
DoricRegistry.enablePerformance(isChecked); Doric.enablePerformance(isChecked);
} }
}); });
setSwitch(profileSwitch); setSwitch(profileSwitch);
@ -396,7 +396,7 @@ public class DoricDevActivity extends AppCompatActivity implements DoricDev.Stat
}); });
} }
} }
if (DoricRegistry.isEnableRenderSnapshot()) { if (Doric.isEnableRenderSnapshot()) {
actionMap.put("Snapshot", new DialogInterface.OnClickListener() { actionMap.put("Snapshot", new DialogInterface.OnClickListener() {
@Override @Override
public void onClick(DialogInterface dialog, int which) { public void onClick(DialogInterface dialog, int which) {
@ -410,7 +410,7 @@ public class DoricDevActivity extends AppCompatActivity implements DoricDev.Stat
} }
}); });
} }
if (DoricRegistry.isEnablePerformance()) { if (Doric.isEnablePerformance()) {
actionMap.put("Performance", new DialogInterface.OnClickListener() { actionMap.put("Performance", new DialogInterface.OnClickListener() {
@Override @Override
public void onClick(DialogInterface dialog, int which) { public void onClick(DialogInterface dialog, int which) {

View File

@ -20,8 +20,8 @@ import android.app.Application;
import com.facebook.soloader.SoLoader; import com.facebook.soloader.SoLoader;
import java.io.IOException; import java.io.IOException;
import java.util.Map;
import pub.doric.loader.DoricJSLoaderManager;
import pub.doric.loader.IDoricJSLoader; import pub.doric.loader.IDoricJSLoader;
/** /**
@ -56,7 +56,7 @@ public class Doric {
* @param doricLibrary Which registered in global * @param doricLibrary Which registered in global
*/ */
public static void registerLibrary(DoricLibrary doricLibrary) { public static void registerLibrary(DoricLibrary doricLibrary) {
DoricRegistry.register(doricLibrary); DoricSingleton.getInstance().registerLibrary(doricLibrary);
} }
/** /**
@ -65,6 +65,27 @@ public class Doric {
* @param jsLoader Which added in global * @param jsLoader Which added in global
*/ */
public static void addJSLoader(IDoricJSLoader jsLoader) { public static void addJSLoader(IDoricJSLoader jsLoader) {
DoricJSLoaderManager.getInstance().addJSLoader(jsLoader); DoricSingleton.getInstance().getJsLoaderManager().addJSLoader(jsLoader);
} }
public void setEnvironmentValue(Map<String, Object> value) {
DoricSingleton.getInstance().setEnvironmentValue(value);
}
public static void enablePerformance(boolean enable) {
DoricSingleton.getInstance().enablePerformance = enable;
}
public static boolean isEnablePerformance() {
return DoricSingleton.getInstance().enablePerformance;
}
public static void enableRenderSnapshot(boolean enable) {
DoricSingleton.getInstance().enableRenderSnapshot = enable;
}
public static boolean isEnableRenderSnapshot() {
return DoricSingleton.getInstance().enableRenderSnapshot;
}
} }

View File

@ -135,7 +135,7 @@ public class DoricContext {
} }
public void init(String initData) { public void init(String initData) {
if (DoricRegistry.isEnableRenderSnapshot()) { if (Doric.isEnableRenderSnapshot()) {
callEntity("__enableSnapshot__"); callEntity("__enableSnapshot__");
} }
this.extra = initData; this.extra = initData;
@ -158,7 +158,7 @@ public class DoricContext {
public IDoricDriver getDriver() { public IDoricDriver getDriver() {
if (doricDriver == null) { if (doricDriver == null) {
doricDriver = DoricNativeDriver.getInstance(); doricDriver = DoricSingleton.getInstance().getNativeDriver();
} }
return doricDriver; return doricDriver;
} }

View File

@ -35,17 +35,8 @@ public class DoricContextManager {
private final AtomicInteger counter = new AtomicInteger(); private final AtomicInteger counter = new AtomicInteger();
private final Map<String, DoricContext> doricContextMap = new ConcurrentHashMap<>(); private final Map<String, DoricContext> doricContextMap = new ConcurrentHashMap<>();
private static class Inner {
private static final DoricContextManager sInstance = new DoricContextManager();
}
private DoricContextManager() {
}
public static DoricContextManager getInstance() { public static DoricContextManager getInstance() {
return Inner.sInstance; return DoricSingleton.getInstance().getContextManager();
} }
DoricContext createContext(Context context, final String script, final String source, String extra) { DoricContext createContext(Context context, final String script, final String source, String extra) {

View File

@ -43,21 +43,13 @@ public class DoricNativeDriver implements IDoricDriver {
private final Handler mUIHandler; private final Handler mUIHandler;
private final Handler mJSHandler; private final Handler mJSHandler;
private static class Inner { public DoricNativeDriver() {
private static final DoricNativeDriver sInstance = new DoricNativeDriver();
}
private DoricNativeDriver() {
doricJSEngine = new DoricJSEngine(); doricJSEngine = new DoricJSEngine();
mBridgeExecutor = Executors.newCachedThreadPool(); mBridgeExecutor = Executors.newCachedThreadPool();
mUIHandler = new Handler(Looper.getMainLooper()); mUIHandler = new Handler(Looper.getMainLooper());
mJSHandler = doricJSEngine.getJSHandler(); mJSHandler = doricJSEngine.getJSHandler();
} }
public static DoricNativeDriver getInstance() {
return Inner.sInstance;
}
@Override @Override
public AsyncResult<JSDecoder> invokeContextEntityMethod(final String contextId, final String method, final Object... args) { public AsyncResult<JSDecoder> invokeContextEntityMethod(final String contextId, final String method, final Object... args) {
final AsyncResult<JSDecoder> asyncResult = new AsyncResult<>(); final AsyncResult<JSDecoder> asyncResult = new AsyncResult<>();

View File

@ -198,7 +198,7 @@ public class DoricPanelFragment extends Fragment implements IDoricNavigator {
final String alias = argument.getString("alias"); final String alias = argument.getString("alias");
String source = argument.getString("source"); String source = argument.getString("source");
final String extra = argument.getString("extra"); final String extra = argument.getString("extra");
DoricJSLoaderManager.getInstance().loadJSBundle(source).setCallback(new AsyncResult.Callback<String>() { DoricSingleton.getInstance().getJsLoaderManager().loadJSBundle(source).setCallback(new AsyncResult.Callback<String>() {
@Override @Override
public void onResult(String result) { public void onResult(String result) {
if (getActivity() == null) { if (getActivity() == null) {

View File

@ -19,13 +19,10 @@ import android.graphics.drawable.Drawable;
import android.text.TextUtils; import android.text.TextUtils;
import java.lang.ref.WeakReference; import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.HashSet; import java.util.HashSet;
import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import pub.doric.engine.DoricJSEngine; import pub.doric.engine.DoricJSEngine;
import pub.doric.performance.DoricPerformanceProfile; import pub.doric.performance.DoricPerformanceProfile;
@ -71,10 +68,6 @@ import pub.doric.utils.DoricMetaInfo;
* @CreateDate: 2019-07-20 * @CreateDate: 2019-07-20
*/ */
public class DoricRegistry { public class DoricRegistry {
private static final Map<String, String> bundles = new ConcurrentHashMap<>();
private static final Set<DoricLibrary> doricLibraries = new HashSet<>();
private static final List<WeakReference<DoricRegistry>> registries = new ArrayList<>();
private static final Map<String, Object> envMap = new ConcurrentHashMap<>();
private final Map<String, DoricMetaInfo<DoricJavaPlugin>> pluginInfoMap = new HashMap<>(); private final Map<String, DoricMetaInfo<DoricJavaPlugin>> pluginInfoMap = new HashMap<>();
private final Map<String, DoricMetaInfo<ViewNode>> nodeInfoMap = new HashMap<>(); private final Map<String, DoricMetaInfo<ViewNode>> nodeInfoMap = new HashMap<>();
@ -84,52 +77,13 @@ public class DoricRegistry {
private Drawable defaultErrorDrawable = null; private Drawable defaultErrorDrawable = null;
private static boolean enablePerformance = false;
private static boolean enableRenderSnapshot = false; private void initRegistry(DoricRegistry doricRegistry) {
for (DoricLibrary library : DoricSingleton.getInstance().doricLibraries) {
public static void enablePerformance(boolean enable) {
enablePerformance = enable;
}
public static boolean isEnablePerformance() {
return enablePerformance;
}
public static void enableRenderSnapshot(boolean enable) {
enableRenderSnapshot = enable;
}
public static boolean isEnableRenderSnapshot() {
return enableRenderSnapshot;
}
private static void initRegistry(DoricRegistry doricRegistry) {
for (DoricLibrary library : doricLibraries) {
library.load(doricRegistry); library.load(doricRegistry);
} }
} }
public static void register(DoricLibrary doricLibrary) {
doricLibraries.add(doricLibrary);
for (WeakReference<DoricRegistry> registryWeakReference : registries) {
DoricRegistry registry = registryWeakReference.get();
if (registry != null) {
doricLibrary.load(registry);
}
}
}
public static void setEnvironmentValue(Map<String, Object> value) {
envMap.putAll(value);
for (WeakReference<DoricRegistry> registryWeakReference : registries) {
DoricRegistry registry = registryWeakReference.get();
if (registry != null) {
registry.innerSetEnvironmentValue(value);
}
}
}
private final WeakReference<DoricJSEngine> doricJSEngineWeakReference; private final WeakReference<DoricJSEngine> doricJSEngineWeakReference;
public DoricRegistry(DoricJSEngine doricJSEngine) { public DoricRegistry(DoricJSEngine doricJSEngine) {
@ -168,12 +122,12 @@ public class DoricRegistry {
this.registerViewNode(SwitchNode.class); this.registerViewNode(SwitchNode.class);
this.registerViewNode(FlexNode.class); this.registerViewNode(FlexNode.class);
initRegistry(this); initRegistry(this);
doricJSEngine.setEnvironmentValue(envMap); doricJSEngine.setEnvironmentValue(DoricSingleton.getInstance().envMap);
registries.add(new WeakReference<>(this)); DoricSingleton.getInstance().registries.add(new WeakReference<>(this));
} }
public void registerJSBundle(String name, String bundle) { public void registerJSBundle(String name, String bundle) {
bundles.put(name, bundle); DoricSingleton.getInstance().bundles.put(name, bundle);
} }
public void registerNativePlugin(Class<? extends DoricJavaPlugin> pluginClass) { public void registerNativePlugin(Class<? extends DoricJavaPlugin> pluginClass) {
@ -199,10 +153,10 @@ public class DoricRegistry {
} }
public String acquireJSBundle(String name) { public String acquireJSBundle(String name) {
return bundles.get(name); return DoricSingleton.getInstance().bundles.get(name);
} }
private void innerSetEnvironmentValue(Map<String, Object> value) { void innerSetEnvironmentValue(Map<String, Object> value) {
DoricJSEngine doricJSEngine = doricJSEngineWeakReference.get(); DoricJSEngine doricJSEngine = doricJSEngineWeakReference.get();
if (doricJSEngine == null) { if (doricJSEngine == null) {
return; return;
@ -259,4 +213,7 @@ public class DoricRegistry {
return globalPerformanceAnchorHook; return globalPerformanceAnchorHook;
} }
public static void register(DoricLibrary doricLibrary) {
DoricSingleton.getInstance().registerLibrary(doricLibrary);
}
} }

View File

@ -0,0 +1,94 @@
/*
* 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.
*/
package pub.doric;
import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import pub.doric.loader.DoricJSLoaderManager;
/**
* @Description: pub.doric
* @Author: pengfei.zhou
* @CreateDate: 2021/7/21
*/
public class DoricSingleton {
final Map<String, String> bundles = new ConcurrentHashMap<>();
final Set<DoricLibrary> doricLibraries = new HashSet<>();
final List<WeakReference<DoricRegistry>> registries = new ArrayList<>();
final Map<String, Object> envMap = new ConcurrentHashMap<>();
private final DoricJSLoaderManager jsLoaderManager = new DoricJSLoaderManager();
boolean enablePerformance = false;
boolean enableRenderSnapshot = false;
private DoricNativeDriver nativeDriver;
private final DoricContextManager doricContextManager = new DoricContextManager();
private static class Inner {
private static final DoricSingleton sInstance = new DoricSingleton();
}
private DoricSingleton() {
}
public static DoricSingleton getInstance() {
return Inner.sInstance;
}
public void registerLibrary(DoricLibrary doricLibrary) {
doricLibraries.add(doricLibrary);
for (WeakReference<DoricRegistry> registryWeakReference : registries) {
DoricRegistry registry = registryWeakReference.get();
if (registry != null) {
doricLibrary.load(registry);
}
}
}
public void setEnvironmentValue(Map<String, Object> value) {
envMap.putAll(value);
for (WeakReference<DoricRegistry> registryWeakReference : registries) {
DoricRegistry registry = registryWeakReference.get();
if (registry != null) {
registry.innerSetEnvironmentValue(value);
}
}
}
public DoricJSLoaderManager getJsLoaderManager() {
return jsLoaderManager;
}
public DoricNativeDriver getNativeDriver() {
if (nativeDriver == null) {
nativeDriver = new DoricNativeDriver();
}
return nativeDriver;
}
public DoricContextManager getContextManager() {
return doricContextManager;
}
}

View File

@ -32,17 +32,13 @@ import pub.doric.async.AsyncResult;
*/ */
public class DoricJSLoaderManager { public class DoricJSLoaderManager {
private Set<IDoricJSLoader> jsLoaders = new HashSet<>(); private final Set<IDoricJSLoader> jsLoaders = new HashSet<>();
private DoricJSLoaderManager() { public DoricJSLoaderManager() {
addJSLoader(new DoricAssetJSLoader()); addJSLoader(new DoricAssetJSLoader());
addJSLoader(new DoricHttpJSLoader()); addJSLoader(new DoricHttpJSLoader());
} }
private static class Inner {
private static final DoricJSLoaderManager sInstance = new DoricJSLoaderManager();
}
public void addJSLoader(IDoricJSLoader jsLoader) { public void addJSLoader(IDoricJSLoader jsLoader) {
jsLoaders.add(jsLoader); jsLoaders.add(jsLoader);
} }
@ -51,10 +47,6 @@ public class DoricJSLoaderManager {
return jsLoaders; return jsLoaders;
} }
public static DoricJSLoaderManager getInstance() {
return Inner.sInstance;
}
public AsyncResult<String> loadJSBundle(String source) { public AsyncResult<String> loadJSBundle(String source) {
if (!TextUtils.isEmpty(source)) { if (!TextUtils.isEmpty(source)) {
if (source.startsWith("_internal_://")) { if (source.startsWith("_internal_://")) {

View File

@ -17,14 +17,13 @@ package pub.doric.performance;
import android.os.Handler; import android.os.Handler;
import android.os.HandlerThread; import android.os.HandlerThread;
import android.util.Log;
import java.util.HashMap; import java.util.HashMap;
import java.util.HashSet; import java.util.HashSet;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
import pub.doric.DoricRegistry; import pub.doric.Doric;
/** /**
* @Description: pub.doric.performance * @Description: pub.doric.performance
@ -43,7 +42,7 @@ public class DoricPerformanceProfile {
private static final String MARK_END = "end"; private static final String MARK_END = "end";
private final String name; private final String name;
private boolean enable = DoricRegistry.isEnablePerformance(); private boolean enable = Doric.isEnablePerformance();
private static final Handler performanceHandler; private static final Handler performanceHandler;
private final Set<AnchorHook> hooks = new HashSet<>(); private final Set<AnchorHook> hooks = new HashSet<>();

View File

@ -20,6 +20,7 @@
// Created by jingpeng.wang on 2020/2/25. // Created by jingpeng.wang on 2020/2/25.
// //
#import <DoricCore/Doric.h> #import <DoricCore/Doric.h>
#import <DoricCore/DoricSingleton.h>
#import <DoricCore/DoricNativeDriver.h> #import <DoricCore/DoricNativeDriver.h>
#import <DoricCore/DoricContextManager.h> #import <DoricCore/DoricContextManager.h>
@ -78,8 +79,8 @@ - (instancetype)init {
if (self = [super init]) { if (self = [super init]) {
_callbacks = [NSHashTable hashTableWithOptions:NSPointerFunctionsWeakMemory]; _callbacks = [NSHashTable hashTableWithOptions:NSPointerFunctionsWeakMemory];
_reloadingContexts = [NSHashTable hashTableWithOptions:NSPointerFunctionsWeakMemory]; _reloadingContexts = [NSHashTable hashTableWithOptions:NSPointerFunctionsWeakMemory];
[DoricNativeDriver.instance.registry registerMonitor:[DoricDevMonitor new]]; [DoricSingleton.instance.nativeDriver.registry registerMonitor:[DoricDevMonitor new]];
DoricNativeDriver.instance.registry.globalPerformanceAnchorHook = [DoricDevPerformanceAnchorHook new]; DoricSingleton.instance.nativeDriver.registry.globalPerformanceAnchorHook = [DoricDevPerformanceAnchorHook new];
} }
return self; return self;
} }

View File

@ -125,7 +125,7 @@ - (void)onClick {
[alertController addAction:startDebugging]; [alertController addAction:startDebugging];
} }
} }
if ([DoricRegistry isEnableRenderSnapshot]) { if ([Doric isEnableRenderSnapshot]) {
UIAlertAction *snapshot = [UIAlertAction actionWithTitle:@"Snapshot" style:UIAlertActionStyleDefault handler:^(UIAlertAction *_) { UIAlertAction *snapshot = [UIAlertAction actionWithTitle:@"Snapshot" style:UIAlertActionStyleDefault handler:^(UIAlertAction *_) {
DoricSnapshotView *doricSnapshotView = [[DoricSnapshotView alloc] initWithDoricContext:self.doricContext]; DoricSnapshotView *doricSnapshotView = [[DoricSnapshotView alloc] initWithDoricContext:self.doricContext];
doricSnapshotView.top = 50; doricSnapshotView.top = 50;
@ -135,7 +135,7 @@ - (void)onClick {
[alertController addAction:snapshot]; [alertController addAction:snapshot];
} }
if ([DoricRegistry isEnablePerformance]) { if ([Doric isEnablePerformance]) {
UIAlertAction *performanceAction = [UIAlertAction UIAlertAction *performanceAction = [UIAlertAction
actionWithTitle:@"Performance" actionWithTitle:@"Performance"
style:UIAlertActionStyleDefault style:UIAlertActionStyleDefault
@ -290,11 +290,11 @@ - (void)viewDidLoad {
} }
- (void)onSnapshotSwitch { - (void)onSnapshotSwitch {
[DoricRegistry enableRenderSnapshot:self.switchSnapshot.isOn]; [Doric enableRenderSnapshot:self.switchSnapshot.isOn];
} }
- (void)onPerformanceSwitch { - (void)onPerformanceSwitch {
[DoricRegistry enablePerformance:self.switchPerformance.isOn]; [Doric enablePerformance:self.switchPerformance.isOn];
} }
- (void)disconnect { - (void)disconnect {
@ -385,13 +385,13 @@ - (void)initHeaders {
self.switchSnapshot.left = self.tvSnapshot.right + 20; self.switchSnapshot.left = self.tvSnapshot.right + 20;
self.switchSnapshot.top = self.tvConnection.bottom + 15; self.switchSnapshot.top = self.tvConnection.bottom + 15;
self.tvSnapshot.centerY = self.switchSnapshot.centerY; self.tvSnapshot.centerY = self.switchSnapshot.centerY;
self.switchSnapshot.on = [DoricRegistry isEnableRenderSnapshot]; self.switchSnapshot.on = [Doric isEnableRenderSnapshot];
self.tvPerformance.left = self.tvLabel.right + 20; self.tvPerformance.left = self.tvLabel.right + 20;
self.switchPerformance.left = self.tvPerformance.right + 20; self.switchPerformance.left = self.tvPerformance.right + 20;
self.switchPerformance.top = self.switchSnapshot.bottom + 15; self.switchPerformance.top = self.switchSnapshot.bottom + 15;
self.tvPerformance.centerY = self.switchPerformance.centerY; self.tvPerformance.centerY = self.switchPerformance.centerY;
self.switchPerformance.on = [DoricRegistry isEnablePerformance]; self.switchPerformance.on = [Doric isEnablePerformance];
} }
- (void)initList { - (void)initList {

View File

@ -9,7 +9,7 @@
#import "AppDelegate.h" #import "AppDelegate.h"
#import "NavigationController.h" #import "NavigationController.h"
#import "ViewController.h" #import "ViewController.h"
#import "DoricRegistry.h" #import <DoricCore/Doric.h>
#if __has_include(<SDWebImage/SDWebImage.h>) #if __has_include(<SDWebImage/SDWebImage.h>)
@ -27,7 +27,7 @@ @implementation AppDelegate
- (void)localeChanged { - (void)localeChanged {
[DoricRegistry setEnvironmentValue:@{ [Doric setEnvironmentValue:@{
@"localeLanguage": [[NSLocale autoupdatingCurrentLocale] objectForKey:NSLocaleLanguageCode], @"localeLanguage": [[NSLocale autoupdatingCurrentLocale] objectForKey:NSLocaleLanguageCode],
@"localeCountry": [[NSLocale autoupdatingCurrentLocale] objectForKey:NSLocaleCountryCode], @"localeCountry": [[NSLocale autoupdatingCurrentLocale] objectForKey:NSLocaleCountryCode],
}]; }];

View File

@ -46,9 +46,9 @@ - (void)viewDidLoad {
it.dataSource = self; it.dataSource = self;
it.delegate = self; it.delegate = self;
}]]; }]];
[DoricRegistry register:[DemoLibrary new]]; [Doric registerLibrary:[DemoLibrary new]];
[DoricRegistry enablePerformance:YES]; [Doric enablePerformance:YES];
[DoricRegistry enableRenderSnapshot:YES]; [Doric enableRenderSnapshot:YES];
} }
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

View File

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

View File

@ -20,6 +20,7 @@
// Created by pengfei.zhou on 2020/2/28. // Created by pengfei.zhou on 2020/2/28.
// //
#import "Doric.h" #import "Doric.h"
#import "DoricSingleton.h"
@implementation Doric @implementation Doric
@ -28,6 +29,26 @@ + (void)registerLibrary:(DoricLibrary *)library {
} }
+ (void)addJSLoader:(id <DoricLoaderProtocol>)loader { + (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 @end

View File

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

View File

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

View File

@ -45,15 +45,6 @@ - (DoricRegistry *)registry {
return self.jsExecutor.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 { - (DoricAsyncResult *)invokeDoricMethod:(NSString *)method argumentsArray:(NSArray *)args {
id contextId = args.count > 0 ? args[0] : nil; id contextId = args.count > 0 ? args[0] : nil;
DoricPerformanceProfile *profile = nil; DoricPerformanceProfile *profile = nil;

View File

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

View File

@ -54,39 +54,7 @@
#import "DoricFlexNode.h" #import "DoricFlexNode.h"
#import "DoricKeyboardPlugin.h" #import "DoricKeyboardPlugin.h"
#import "DoricJSEngine.h" #import "DoricJSEngine.h"
#import "DoricSingleton.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
@interface DoricRegistry () @interface DoricRegistry ()
@ -100,22 +68,14 @@ @interface DoricRegistry ()
@implementation DoricRegistry @implementation DoricRegistry
+ (void)register:(DoricLibrary *)library { + (void)register:(DoricLibrary *)library {
[DoricLibraries.instance.libraries addObject:library]; [DoricSingleton.instance.libraries addObject:library];
for (DoricRegistry *registry in DoricLibraries.instance.registries) { for (DoricRegistry *registry in DoricSingleton.instance.registries) {
if (registry) { if (registry) {
[library load: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 { - (instancetype)initWithJSEngine:(DoricJSEngine *)jsEngine {
if (self = [super init]) { if (self = [super init]) {
@ -125,31 +85,15 @@ - (instancetype)initWithJSEngine:(DoricJSEngine *)jsEngine {
_nodes = [NSMutableDictionary new]; _nodes = [NSMutableDictionary new];
_monitors = [NSMutableSet new]; _monitors = [NSMutableSet new];
[self innerRegister]; [self innerRegister];
[DoricLibraries.instance.libraries enumerateObjectsUsingBlock:^(DoricLibrary *obj, BOOL *stop) { [DoricSingleton.instance.libraries enumerateObjectsUsingBlock:^(DoricLibrary *obj, BOOL *stop) {
[obj load:self]; [obj load:self];
}]; }];
[jsEngine setEnvironmentValue:DoricLibraries.instance.envDic]; [jsEngine setEnvironmentValue:DoricSingleton.instance.envDic];
[DoricLibraries.instance.registries addObject:self]; [DoricSingleton.instance.registries addObject:self];
} }
return 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 { - (void)innerRegister {
[self registerNativePlugin:DoricShaderPlugin.class withName:@"shader"]; [self registerNativePlugin:DoricShaderPlugin.class withName:@"shader"];
[self registerNativePlugin:DoricModalPlugin.class withName:@"modal"]; [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 "UIView+Doric.h"
#import "DoricExtensions.h" #import "DoricExtensions.h"
#import "DoricUtil.h" #import "DoricUtil.h"
#import "DoricSingleton.h"
NSString *const DORIC_MASK_RETRY = @"doric_mask_retry"; NSString *const DORIC_MASK_RETRY = @"doric_mask_retry";
@ -202,7 +203,7 @@ - (void)hideMask {
- (void)loadJSBundle { - (void)loadJSBundle {
[self showLoading]; [self showLoading];
DoricAsyncResult <NSString *> *result = [DoricJSLoaderManager.instance request:self.source]; DoricAsyncResult <NSString *> *result = [DoricSingleton.instance.jsLoaderManager request:self.source];
result.resultCallback = ^(NSString *result) { result.resultCallback = ^(NSString *result) {
dispatch_async(dispatch_get_main_queue(), ^{ dispatch_async(dispatch_get_main_queue(), ^{
[self hideMask]; [self hideMask];

View File

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

View File

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

View File

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