add resId to cache DoricResource on native

This commit is contained in:
pengfei.zhou
2021-11-18 17:39:01 +08:00
committed by osborn
parent d746c5b4d4
commit 5b80e4e0e1
17 changed files with 107 additions and 23 deletions

View File

@@ -29,6 +29,7 @@ import pub.doric.performance.DoricPerformanceProfile;
import pub.doric.plugin.AnimatePlugin;
import pub.doric.plugin.CoordinatorPlugin;
import pub.doric.plugin.DoricJavaPlugin;
import pub.doric.plugin.ImageDecoderPlugin;
import pub.doric.plugin.KeyboardPlugin;
import pub.doric.plugin.ModalPlugin;
import pub.doric.plugin.NavBarPlugin;
@@ -114,6 +115,7 @@ public class DoricRegistry {
this.registerNativePlugin(NotchPlugin.class);
this.registerNativePlugin(KeyboardPlugin.class);
this.registerNativePlugin(ResourceLoaderPlugin.class);
this.registerNativePlugin(ImageDecoderPlugin.class);
this.registerViewNode(RootNode.class);
this.registerViewNode(TextNode.class);

View File

@@ -15,10 +15,12 @@
*/
package pub.doric.plugin;
import com.bumptech.glide.Glide;
import com.github.pengfeizhou.jscore.JSObject;
import com.github.pengfeizhou.jscore.JavaValue;
import java.util.HashMap;
import java.util.Map;
import pub.doric.DoricContext;
import pub.doric.async.AsyncResult;
import pub.doric.extension.bridge.DoricMethod;
@@ -34,6 +36,7 @@ import pub.doric.utils.DoricLog;
*/
@DoricPlugin(name = "resourceLoader")
public class ResourceLoaderPlugin extends DoricJavaPlugin {
private Map<String, DoricResource> cachedResources = new HashMap<>();
public ResourceLoaderPlugin(DoricContext doricContext) {
super(doricContext);
@@ -41,11 +44,16 @@ public class ResourceLoaderPlugin extends DoricJavaPlugin {
@DoricMethod
public void load(JSObject resource, final DoricPromise promise) {
final String resId = resource.getProperty("resId").asString().value();
final String type = resource.getProperty("type").asString().value();
final String identifier = resource.getProperty("identifier").asString().value();
DoricResource doricResource = getDoricContext().getDriver().getRegistry().getResourceManager().load(getDoricContext(), type, identifier);
DoricResource doricResource = getDoricContext().getDriver().getRegistry().getResourceManager().load(
getDoricContext(),
resId,
type,
identifier);
if (doricResource != null) {
doricResource.fetchRaw().setCallback(new AsyncResult.Callback<byte[]>() {
doricResource.fetch().setCallback(new AsyncResult.Callback<byte[]>() {
@Override
public void onResult(byte[] rawData) {
promise.resolve(new JavaValue(rawData));

View File

@@ -26,6 +26,7 @@ import pub.doric.async.AsyncResult;
public abstract class DoricResource {
protected final DoricContext doricContext;
protected final String identifier;
private AsyncResult<byte[]> rawResult;
public DoricResource(DoricContext doricContext, String identifier) {
this.doricContext = doricContext;
@@ -33,4 +34,11 @@ public abstract class DoricResource {
}
public abstract AsyncResult<byte[]> fetchRaw();
public AsyncResult<byte[]> fetch() {
if (rawResult == null) {
rawResult = fetchRaw();
}
return rawResult;
}
}

View File

@@ -17,6 +17,7 @@ package pub.doric.resource;
import java.util.HashMap;
import java.util.Map;
import java.util.WeakHashMap;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
@@ -29,6 +30,7 @@ import pub.doric.DoricContext;
*/
public class DoricResourceManager {
private final Map<String, DoricResourceLoader> mResourceLoaders = new HashMap<>();
private final Map<String, DoricResource> cachedResources = new WeakHashMap<>();
public void registerLoader(DoricResourceLoader loader) {
mResourceLoaders.put(loader.resourceType(), loader);
@@ -39,11 +41,18 @@ public class DoricResourceManager {
}
@Nullable
public DoricResource load(@NonNull DoricContext doricContext, @NonNull String type, @NonNull String identifier) {
DoricResourceLoader loader = mResourceLoaders.get(type);
if (loader != null) {
return loader.load(doricContext, identifier);
public DoricResource load(@NonNull DoricContext doricContext,
@NonNull String resId,
@NonNull String type,
@NonNull String identifier) {
DoricResource resource = cachedResources.get(resId);
if (resource == null) {
DoricResourceLoader loader = mResourceLoaders.get(type);
if (loader != null) {
resource = loader.load(doricContext, identifier);
cachedResources.put(resId, resource);
}
}
return null;
return resource;
}
}

View File

@@ -397,11 +397,13 @@ public class ImageNode extends ViewNode<ImageView> {
return;
}
JSObject resource = prop.asObject();
final String resourceId = resource.getProperty("resId").asString().value();
final String type = resource.getProperty("type").asString().value();
final String identifier = resource.getProperty("identifier").asString().value();
DoricResource doricResource = getDoricContext().getDriver().getRegistry().getResourceManager().load(getDoricContext(), type, identifier);
DoricResource doricResource = getDoricContext().getDriver().getRegistry().getResourceManager()
.load(getDoricContext(), resourceId, type, identifier);
if (doricResource != null) {
doricResource.fetchRaw().setCallback(new AsyncResult.Callback<byte[]>() {
doricResource.fetch().setCallback(new AsyncResult.Callback<byte[]>() {
@Override
public void onResult(byte[] imageData) {
loadIntoTarget(Glide.with(getContext()).load(imageData));