feat: add ArrayBufferResource

This commit is contained in:
pengfei.zhou
2022-03-07 18:31:14 +08:00
committed by osborn
parent fa6604f25e
commit e8e344ed21
11 changed files with 224 additions and 1 deletions

View File

@@ -45,6 +45,7 @@ import pub.doric.plugin.StoragePlugin;
import pub.doric.refresh.RefreshableNode;
import pub.doric.resource.DoricAndroidAssetsLoader;
import pub.doric.resource.DoricAndroidLoader;
import pub.doric.resource.DoricArrayBufferLoader;
import pub.doric.resource.DoricAssetsLoader;
import pub.doric.resource.DoricBase64Loader;
import pub.doric.resource.DoricLocalLoader;
@@ -146,6 +147,8 @@ public class DoricRegistry {
this.getResourceManager().registerLoader(new DoricLocalLoader());
this.getResourceManager().registerLoader(new DoricRemoteLoader());
this.getResourceManager().registerLoader(new DoricBase64Loader());
this.getResourceManager().registerLoader(new DoricArrayBufferLoader());
initRegistry(this);
doricJSEngine.setEnvironmentValue(DoricSingleton.getInstance().envMap);
DoricSingleton.getInstance().registries.add(new WeakReference<>(this));

View File

@@ -0,0 +1,35 @@
/*
* Copyright [2022] [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.resource;
import pub.doric.DoricContext;
/**
* @Description: Load arrayBuffer
* @Author: pengfei.zhou
* @CreateDate: 2022/2/28
*/
public class DoricArrayBufferLoader implements DoricResourceLoader {
@Override
public String resourceType() {
return "arrayBuffer";
}
@Override
public DoricResource load(DoricContext doricContext, String identifier) {
return new DoricArrayBufferResource(doricContext, identifier);
}
}

View File

@@ -0,0 +1,46 @@
/*
* Copyright [2022] [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.resource;
import com.github.pengfeizhou.jscore.JSArrayBuffer;
import pub.doric.DoricContext;
import pub.doric.async.AsyncResult;
/**
* @Description: Support ArrayBuffer
* @Author: pengfei.zhou
* @CreateDate: 2022/2/28
*/
class DoricArrayBufferResource extends DoricResource {
private JSArrayBuffer value;
public DoricArrayBufferResource(DoricContext doricContext, String identifier) {
super(doricContext, identifier);
}
public void setValue(JSArrayBuffer value) {
this.value = value;
}
@Override
public AsyncResult<byte[]> fetchRaw() {
final AsyncResult<byte[]> result = new AsyncResult<>();
result.setResult(this.value.value());
return result;
}
}

View File

@@ -15,6 +15,7 @@
*/
package pub.doric.resource;
import com.github.pengfeizhou.jscore.JSArrayBuffer;
import com.github.pengfeizhou.jscore.JSObject;
import java.util.HashMap;
@@ -22,6 +23,7 @@ import java.util.Map;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.UiThread;
import pub.doric.DoricContext;
/**
@@ -41,6 +43,7 @@ public class DoricResourceManager {
}
@Nullable
@UiThread
public synchronized DoricResource load(@NonNull DoricContext doricContext,
@NonNull JSObject resource) {
String resId = resource.getProperty("resId").asString().value();
@@ -51,6 +54,10 @@ public class DoricResourceManager {
DoricResourceLoader loader = mResourceLoaders.get(type);
if (loader != null) {
doricResource = loader.load(doricContext, identifier);
if (doricResource instanceof DoricArrayBufferResource) {
JSArrayBuffer buffer = resource.getProperty("data").asArrayBuffer();
((DoricArrayBufferResource) doricResource).setValue(buffer);
}
doricContext.cacheResource(resId, doricResource);
}
}