refact: use DoricSingleton to hold all static or singleton objects
This commit is contained in:
@@ -20,8 +20,8 @@ import android.app.Application;
|
||||
import com.facebook.soloader.SoLoader;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Map;
|
||||
|
||||
import pub.doric.loader.DoricJSLoaderManager;
|
||||
import pub.doric.loader.IDoricJSLoader;
|
||||
|
||||
/**
|
||||
@@ -56,7 +56,7 @@ public class Doric {
|
||||
* @param doricLibrary Which registered in global
|
||||
*/
|
||||
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
|
||||
*/
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -135,7 +135,7 @@ public class DoricContext {
|
||||
}
|
||||
|
||||
public void init(String initData) {
|
||||
if (DoricRegistry.isEnableRenderSnapshot()) {
|
||||
if (Doric.isEnableRenderSnapshot()) {
|
||||
callEntity("__enableSnapshot__");
|
||||
}
|
||||
this.extra = initData;
|
||||
@@ -158,7 +158,7 @@ public class DoricContext {
|
||||
|
||||
public IDoricDriver getDriver() {
|
||||
if (doricDriver == null) {
|
||||
doricDriver = DoricNativeDriver.getInstance();
|
||||
doricDriver = DoricSingleton.getInstance().getNativeDriver();
|
||||
}
|
||||
return doricDriver;
|
||||
}
|
||||
|
@@ -35,17 +35,8 @@ public class DoricContextManager {
|
||||
private final AtomicInteger counter = new AtomicInteger();
|
||||
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() {
|
||||
return Inner.sInstance;
|
||||
return DoricSingleton.getInstance().getContextManager();
|
||||
}
|
||||
|
||||
DoricContext createContext(Context context, final String script, final String source, String extra) {
|
||||
|
@@ -43,21 +43,13 @@ public class DoricNativeDriver implements IDoricDriver {
|
||||
private final Handler mUIHandler;
|
||||
private final Handler mJSHandler;
|
||||
|
||||
private static class Inner {
|
||||
private static final DoricNativeDriver sInstance = new DoricNativeDriver();
|
||||
}
|
||||
|
||||
private DoricNativeDriver() {
|
||||
public DoricNativeDriver() {
|
||||
doricJSEngine = new DoricJSEngine();
|
||||
mBridgeExecutor = Executors.newCachedThreadPool();
|
||||
mUIHandler = new Handler(Looper.getMainLooper());
|
||||
mJSHandler = doricJSEngine.getJSHandler();
|
||||
}
|
||||
|
||||
public static DoricNativeDriver getInstance() {
|
||||
return Inner.sInstance;
|
||||
}
|
||||
|
||||
@Override
|
||||
public AsyncResult<JSDecoder> invokeContextEntityMethod(final String contextId, final String method, final Object... args) {
|
||||
final AsyncResult<JSDecoder> asyncResult = new AsyncResult<>();
|
||||
|
@@ -198,7 +198,7 @@ public class DoricPanelFragment extends Fragment implements IDoricNavigator {
|
||||
final String alias = argument.getString("alias");
|
||||
String source = argument.getString("source");
|
||||
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
|
||||
public void onResult(String result) {
|
||||
if (getActivity() == null) {
|
||||
|
@@ -19,13 +19,10 @@ import android.graphics.drawable.Drawable;
|
||||
import android.text.TextUtils;
|
||||
|
||||
import java.lang.ref.WeakReference;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
import pub.doric.engine.DoricJSEngine;
|
||||
import pub.doric.performance.DoricPerformanceProfile;
|
||||
@@ -71,10 +68,6 @@ import pub.doric.utils.DoricMetaInfo;
|
||||
* @CreateDate: 2019-07-20
|
||||
*/
|
||||
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<ViewNode>> nodeInfoMap = new HashMap<>();
|
||||
|
||||
@@ -84,52 +77,13 @@ public class DoricRegistry {
|
||||
|
||||
private Drawable defaultErrorDrawable = null;
|
||||
|
||||
private static boolean enablePerformance = false;
|
||||
|
||||
private static boolean enableRenderSnapshot = false;
|
||||
|
||||
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) {
|
||||
private void initRegistry(DoricRegistry doricRegistry) {
|
||||
for (DoricLibrary library : DoricSingleton.getInstance().doricLibraries) {
|
||||
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;
|
||||
|
||||
public DoricRegistry(DoricJSEngine doricJSEngine) {
|
||||
@@ -168,12 +122,12 @@ public class DoricRegistry {
|
||||
this.registerViewNode(SwitchNode.class);
|
||||
this.registerViewNode(FlexNode.class);
|
||||
initRegistry(this);
|
||||
doricJSEngine.setEnvironmentValue(envMap);
|
||||
registries.add(new WeakReference<>(this));
|
||||
doricJSEngine.setEnvironmentValue(DoricSingleton.getInstance().envMap);
|
||||
DoricSingleton.getInstance().registries.add(new WeakReference<>(this));
|
||||
}
|
||||
|
||||
public void registerJSBundle(String name, String bundle) {
|
||||
bundles.put(name, bundle);
|
||||
DoricSingleton.getInstance().bundles.put(name, bundle);
|
||||
}
|
||||
|
||||
public void registerNativePlugin(Class<? extends DoricJavaPlugin> pluginClass) {
|
||||
@@ -199,10 +153,10 @@ public class DoricRegistry {
|
||||
}
|
||||
|
||||
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();
|
||||
if (doricJSEngine == null) {
|
||||
return;
|
||||
@@ -259,4 +213,7 @@ public class DoricRegistry {
|
||||
return globalPerformanceAnchorHook;
|
||||
}
|
||||
|
||||
public static void register(DoricLibrary doricLibrary) {
|
||||
DoricSingleton.getInstance().registerLibrary(doricLibrary);
|
||||
}
|
||||
}
|
||||
|
@@ -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;
|
||||
}
|
||||
}
|
@@ -32,17 +32,13 @@ import pub.doric.async.AsyncResult;
|
||||
*/
|
||||
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 DoricHttpJSLoader());
|
||||
}
|
||||
|
||||
private static class Inner {
|
||||
private static final DoricJSLoaderManager sInstance = new DoricJSLoaderManager();
|
||||
}
|
||||
|
||||
public void addJSLoader(IDoricJSLoader jsLoader) {
|
||||
jsLoaders.add(jsLoader);
|
||||
}
|
||||
@@ -51,10 +47,6 @@ public class DoricJSLoaderManager {
|
||||
return jsLoaders;
|
||||
}
|
||||
|
||||
public static DoricJSLoaderManager getInstance() {
|
||||
return Inner.sInstance;
|
||||
}
|
||||
|
||||
public AsyncResult<String> loadJSBundle(String source) {
|
||||
if (!TextUtils.isEmpty(source)) {
|
||||
if (source.startsWith("_internal_://")) {
|
||||
|
@@ -17,14 +17,13 @@ package pub.doric.performance;
|
||||
|
||||
import android.os.Handler;
|
||||
import android.os.HandlerThread;
|
||||
import android.util.Log;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
import pub.doric.DoricRegistry;
|
||||
import pub.doric.Doric;
|
||||
|
||||
/**
|
||||
* @Description: pub.doric.performance
|
||||
@@ -43,7 +42,7 @@ public class DoricPerformanceProfile {
|
||||
private static final String MARK_END = "end";
|
||||
private final String name;
|
||||
|
||||
private boolean enable = DoricRegistry.isEnablePerformance();
|
||||
private boolean enable = Doric.isEnablePerformance();
|
||||
private static final Handler performanceHandler;
|
||||
private final Set<AnchorHook> hooks = new HashSet<>();
|
||||
|
||||
|
Reference in New Issue
Block a user