feat: cache resource move from global to each context

This commit is contained in:
pengfei.zhou
2021-11-26 11:58:39 +08:00
committed by osborn
parent 3ac737db00
commit 96266921ff
5 changed files with 22 additions and 10 deletions

View File

@@ -35,6 +35,7 @@ import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.WeakHashMap;
import java.util.concurrent.Callable;
import pub.doric.async.AsyncResult;
@@ -42,6 +43,7 @@ import pub.doric.navbar.IDoricNavBar;
import pub.doric.navigator.IDoricNavigator;
import pub.doric.performance.DoricPerformanceProfile;
import pub.doric.plugin.DoricJavaPlugin;
import pub.doric.resource.DoricResource;
import pub.doric.shader.RootNode;
import pub.doric.shader.ViewNode;
import pub.doric.utils.DoricConstant;
@@ -66,6 +68,7 @@ public class DoricContext {
private final Map<String, Map<String, ViewNode<?>>> mHeadNodes = new HashMap<>();
private final DoricPerformanceProfile performanceProfile;
private final Map<String, Animator> animators = new HashMap<>();
private final Map<String, DoricResource> cachedResources = new WeakHashMap<>();
public Collection<ViewNode<?>> allHeadNodes(String type) {
Map<String, ViewNode<?>> headNode = mHeadNodes.get(type);
@@ -216,6 +219,7 @@ public class DoricContext {
javaPlugin.onTearDown();
}
mPluginMap.clear();
cachedResources.clear();
return null;
}
}, ThreadMode.UI);
@@ -355,4 +359,12 @@ public class DoricContext {
public void removeAnimator(String animatorId) {
animators.remove(animatorId);
}
public void cacheResource(String resId, DoricResource doricResource) {
this.cachedResources.put(resId, doricResource);
}
public DoricResource getCachedResource(String resId) {
return this.cachedResources.get(resId);
}
}

View File

@@ -19,7 +19,6 @@ import com.github.pengfeizhou.jscore.JSObject;
import java.util.HashMap;
import java.util.Map;
import java.util.WeakHashMap;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
@@ -32,7 +31,6 @@ import pub.doric.DoricContext;
*/
public class DoricResourceManager {
private final Map<String, DoricResourceLoader> mResourceLoaders = new HashMap<>();
private final Map<String, DoricResource> cachedResources = new WeakHashMap<>();
public synchronized void registerLoader(DoricResourceLoader loader) {
mResourceLoaders.put(loader.resourceType(), loader);
@@ -48,7 +46,7 @@ public class DoricResourceManager {
String resId = resource.getProperty("resId").asString().value();
String type = resource.getProperty("type").asString().value();
String identifier = resource.getProperty("identifier").asString().value();
DoricResource doricResource = cachedResources.get(resId);
DoricResource doricResource = doricContext.getCachedResource(resId);
if (doricResource == null) {
if ("arrayBuffer".equals(type)) {
doricResource = new DoricArrayBufferResource(
@@ -59,7 +57,7 @@ public class DoricResourceManager {
DoricResourceLoader loader = mResourceLoaders.get(type);
if (loader != null) {
doricResource = loader.load(doricContext, identifier);
cachedResources.put(resId, doricResource);
doricContext.cacheResource(resId, doricResource);
}
}
}