From 8339a64663e676ad6c9c30957f9d9aa1304217f5 Mon Sep 17 00:00:00 2001 From: "pengfei.zhou" Date: Tue, 22 Jun 2021 14:23:25 +0800 Subject: [PATCH] optimize: make lazy registered doric library effective --- .../main/java/pub/doric/DoricRegistry.java | 25 +++++++++++++------ .../java/pub/doric/engine/DoricJSEngine.java | 2 +- doric-iOS/Pod/Classes/DoricRegistry.m | 10 ++++++++ 3 files changed, 28 insertions(+), 9 deletions(-) diff --git a/doric-android/doric/src/main/java/pub/doric/DoricRegistry.java b/doric-android/doric/src/main/java/pub/doric/DoricRegistry.java index 51bd3f48..4bb56eb6 100644 --- a/doric-android/doric/src/main/java/pub/doric/DoricRegistry.java +++ b/doric-android/doric/src/main/java/pub/doric/DoricRegistry.java @@ -18,8 +18,11 @@ package pub.doric; 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; @@ -66,14 +69,15 @@ import pub.doric.utils.DoricMetaInfo; * @CreateDate: 2019-07-20 */ public class DoricRegistry { - private static Map bundles = new ConcurrentHashMap<>(); - private static Set doricLibraries = new HashSet<>(); - private Map extendedEnvValues = new HashMap<>(); + private static final Map bundles = new ConcurrentHashMap<>(); + private static final Set doricLibraries = new HashSet<>(); + private static final List> registries = new ArrayList<>(); + private final Map extendedEnvValues = new HashMap<>(); - private Map> pluginInfoMap = new HashMap<>(); - private Map> nodeInfoMap = new HashMap<>(); + private final Map> pluginInfoMap = new HashMap<>(); + private final Map> nodeInfoMap = new HashMap<>(); - private Set monitors = new HashSet<>(); + private final Set monitors = new HashSet<>(); private Drawable defaultPlaceHolderDrawable = null; @@ -85,9 +89,14 @@ public class DoricRegistry { } } - public static void register(DoricLibrary doricLibrary) { doricLibraries.add(doricLibrary); + for (WeakReference registryWeakReference : registries) { + DoricRegistry registry = registryWeakReference.get(); + if (registry != null) { + doricLibrary.load(registry); + } + } } public DoricRegistry() { @@ -125,6 +134,7 @@ public class DoricRegistry { this.registerViewNode(SwitchNode.class); this.registerViewNode(FlexNode.class); initRegistry(this); + registries.add(new WeakReference<>(this)); } public void registerJSBundle(String name, String bundle) { @@ -157,7 +167,6 @@ public class DoricRegistry { return bundles.get(name); } - public void setEnvironmentVariable(String key, Object val) { extendedEnvValues.put(key, val); } diff --git a/doric-android/doric/src/main/java/pub/doric/engine/DoricJSEngine.java b/doric-android/doric/src/main/java/pub/doric/engine/DoricJSEngine.java index 6193a687..42be91d1 100644 --- a/doric-android/doric/src/main/java/pub/doric/engine/DoricJSEngine.java +++ b/doric-android/doric/src/main/java/pub/doric/engine/DoricJSEngine.java @@ -54,7 +54,7 @@ import pub.doric.utils.DoricUtils; */ public class DoricJSEngine implements Handler.Callback, DoricTimerExtension.TimerCallback, IDoricMonitor { - private HandlerThread handlerThread; + private final HandlerThread handlerThread; private final Handler mJSHandler; private final DoricBridgeExtension mDoricBridgeExtension = new DoricBridgeExtension(); protected IDoricJSE mDoricJSE; diff --git a/doric-iOS/Pod/Classes/DoricRegistry.m b/doric-iOS/Pod/Classes/DoricRegistry.m index 2b17d900..45b9834a 100644 --- a/doric-iOS/Pod/Classes/DoricRegistry.m +++ b/doric-iOS/Pod/Classes/DoricRegistry.m @@ -57,6 +57,7 @@ @interface DoricLibraries : NSObject @property(nonatomic, strong) NSMutableSet *libraries; +@property(nonatomic, strong) NSMutableArray *registries; + (instancetype)instance; @end @@ -65,6 +66,7 @@ @implementation DoricLibraries - (instancetype)init { if (self = [super init]) { _libraries = [NSMutableSet new]; + _registries = [NSMutableArray new]; } return self; } @@ -93,6 +95,12 @@ @implementation DoricRegistry + (void)register:(DoricLibrary *)library { [DoricLibraries.instance.libraries addObject:library]; + for (NSValue *value in DoricLibraries.instance.registries) { + DoricRegistry *registry = value.nonretainedObjectValue; + if (registry) { + [library load:registry]; + } + } } - (instancetype)init { @@ -106,6 +114,8 @@ - (instancetype)init { [DoricLibraries.instance.libraries enumerateObjectsUsingBlock:^(DoricLibrary *obj, BOOL *stop) { [obj load:self]; }]; + NSValue *value = [NSValue valueWithNonretainedObject:self]; + [DoricLibraries.instance.registries addObject:value]; } return self; }