optimize: make lazy registered doric library effective

This commit is contained in:
pengfei.zhou 2021-06-22 14:23:25 +08:00 committed by osborn
parent 28713aebbd
commit 8339a64663
3 changed files with 28 additions and 9 deletions

View File

@ -18,8 +18,11 @@ package pub.doric;
import android.graphics.drawable.Drawable; import android.graphics.drawable.Drawable;
import android.text.TextUtils; import android.text.TextUtils;
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 java.util.concurrent.ConcurrentHashMap;
@ -66,14 +69,15 @@ import pub.doric.utils.DoricMetaInfo;
* @CreateDate: 2019-07-20 * @CreateDate: 2019-07-20
*/ */
public class DoricRegistry { public class DoricRegistry {
private static Map<String, String> bundles = new ConcurrentHashMap<>(); private static final Map<String, String> bundles = new ConcurrentHashMap<>();
private static Set<DoricLibrary> doricLibraries = new HashSet<>(); private static final Set<DoricLibrary> doricLibraries = new HashSet<>();
private Map<String, Object> extendedEnvValues = new HashMap<>(); private static final List<WeakReference<DoricRegistry>> registries = new ArrayList<>();
private final Map<String, Object> extendedEnvValues = new HashMap<>();
private Map<String, DoricMetaInfo<DoricJavaPlugin>> pluginInfoMap = new HashMap<>(); private final Map<String, DoricMetaInfo<DoricJavaPlugin>> pluginInfoMap = new HashMap<>();
private Map<String, DoricMetaInfo<ViewNode>> nodeInfoMap = new HashMap<>(); private final Map<String, DoricMetaInfo<ViewNode>> nodeInfoMap = new HashMap<>();
private Set<IDoricMonitor> monitors = new HashSet<>(); private final Set<IDoricMonitor> monitors = new HashSet<>();
private Drawable defaultPlaceHolderDrawable = null; private Drawable defaultPlaceHolderDrawable = null;
@ -85,9 +89,14 @@ public class DoricRegistry {
} }
} }
public static void register(DoricLibrary doricLibrary) { public static void register(DoricLibrary doricLibrary) {
doricLibraries.add(doricLibrary); doricLibraries.add(doricLibrary);
for (WeakReference<DoricRegistry> registryWeakReference : registries) {
DoricRegistry registry = registryWeakReference.get();
if (registry != null) {
doricLibrary.load(registry);
}
}
} }
public DoricRegistry() { public DoricRegistry() {
@ -125,6 +134,7 @@ public class DoricRegistry {
this.registerViewNode(SwitchNode.class); this.registerViewNode(SwitchNode.class);
this.registerViewNode(FlexNode.class); this.registerViewNode(FlexNode.class);
initRegistry(this); initRegistry(this);
registries.add(new WeakReference<>(this));
} }
public void registerJSBundle(String name, String bundle) { public void registerJSBundle(String name, String bundle) {
@ -157,7 +167,6 @@ public class DoricRegistry {
return bundles.get(name); return bundles.get(name);
} }
public void setEnvironmentVariable(String key, Object val) { public void setEnvironmentVariable(String key, Object val) {
extendedEnvValues.put(key, val); extendedEnvValues.put(key, val);
} }

View File

@ -54,7 +54,7 @@ import pub.doric.utils.DoricUtils;
*/ */
public class DoricJSEngine implements Handler.Callback, DoricTimerExtension.TimerCallback, IDoricMonitor { public class DoricJSEngine implements Handler.Callback, DoricTimerExtension.TimerCallback, IDoricMonitor {
private HandlerThread handlerThread; private final HandlerThread handlerThread;
private final Handler mJSHandler; private final Handler mJSHandler;
private final DoricBridgeExtension mDoricBridgeExtension = new DoricBridgeExtension(); private final DoricBridgeExtension mDoricBridgeExtension = new DoricBridgeExtension();
protected IDoricJSE mDoricJSE; protected IDoricJSE mDoricJSE;

View File

@ -57,6 +57,7 @@
@interface DoricLibraries : NSObject @interface DoricLibraries : NSObject
@property(nonatomic, strong) NSMutableSet <DoricLibrary *> *libraries; @property(nonatomic, strong) NSMutableSet <DoricLibrary *> *libraries;
@property(nonatomic, strong) NSMutableArray <NSValue *> *registries;
+ (instancetype)instance; + (instancetype)instance;
@end @end
@ -65,6 +66,7 @@ @implementation DoricLibraries
- (instancetype)init { - (instancetype)init {
if (self = [super init]) { if (self = [super init]) {
_libraries = [NSMutableSet new]; _libraries = [NSMutableSet new];
_registries = [NSMutableArray new];
} }
return self; return self;
} }
@ -93,6 +95,12 @@ @implementation DoricRegistry
+ (void)register:(DoricLibrary *)library { + (void)register:(DoricLibrary *)library {
[DoricLibraries.instance.libraries addObject:library]; [DoricLibraries.instance.libraries addObject:library];
for (NSValue *value in DoricLibraries.instance.registries) {
DoricRegistry *registry = value.nonretainedObjectValue;
if (registry) {
[library load:registry];
}
}
} }
- (instancetype)init { - (instancetype)init {
@ -106,6 +114,8 @@ - (instancetype)init {
[DoricLibraries.instance.libraries enumerateObjectsUsingBlock:^(DoricLibrary *obj, BOOL *stop) { [DoricLibraries.instance.libraries enumerateObjectsUsingBlock:^(DoricLibrary *obj, BOOL *stop) {
[obj load:self]; [obj load:self];
}]; }];
NSValue *value = [NSValue valueWithNonretainedObject:self];
[DoricLibraries.instance.registries addObject:value];
} }
return self; return self;
} }