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.refresh.RefreshableNode;
import pub.doric.resource.DoricAndroidAssetsLoader; import pub.doric.resource.DoricAndroidAssetsLoader;
import pub.doric.resource.DoricAndroidLoader; import pub.doric.resource.DoricAndroidLoader;
import pub.doric.resource.DoricArrayBufferLoader;
import pub.doric.resource.DoricAssetsLoader; import pub.doric.resource.DoricAssetsLoader;
import pub.doric.resource.DoricBase64Loader; import pub.doric.resource.DoricBase64Loader;
import pub.doric.resource.DoricLocalLoader; import pub.doric.resource.DoricLocalLoader;
@ -146,6 +147,8 @@ public class DoricRegistry {
this.getResourceManager().registerLoader(new DoricLocalLoader()); this.getResourceManager().registerLoader(new DoricLocalLoader());
this.getResourceManager().registerLoader(new DoricRemoteLoader()); this.getResourceManager().registerLoader(new DoricRemoteLoader());
this.getResourceManager().registerLoader(new DoricBase64Loader()); this.getResourceManager().registerLoader(new DoricBase64Loader());
this.getResourceManager().registerLoader(new DoricArrayBufferLoader());
initRegistry(this); initRegistry(this);
doricJSEngine.setEnvironmentValue(DoricSingleton.getInstance().envMap); doricJSEngine.setEnvironmentValue(DoricSingleton.getInstance().envMap);
DoricSingleton.getInstance().registries.add(new WeakReference<>(this)); 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; package pub.doric.resource;
import com.github.pengfeizhou.jscore.JSArrayBuffer;
import com.github.pengfeizhou.jscore.JSObject; import com.github.pengfeizhou.jscore.JSObject;
import java.util.HashMap; import java.util.HashMap;
@ -22,6 +23,7 @@ import java.util.Map;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import androidx.annotation.UiThread;
import pub.doric.DoricContext; import pub.doric.DoricContext;
/** /**
@ -41,6 +43,7 @@ public class DoricResourceManager {
} }
@Nullable @Nullable
@UiThread
public synchronized DoricResource load(@NonNull DoricContext doricContext, public synchronized DoricResource load(@NonNull DoricContext doricContext,
@NonNull JSObject resource) { @NonNull JSObject resource) {
String resId = resource.getProperty("resId").asString().value(); String resId = resource.getProperty("resId").asString().value();
@ -51,6 +54,10 @@ public class DoricResourceManager {
DoricResourceLoader loader = mResourceLoaders.get(type); DoricResourceLoader loader = mResourceLoaders.get(type);
if (loader != null) { if (loader != null) {
doricResource = loader.load(doricContext, identifier); doricResource = loader.load(doricContext, identifier);
if (doricResource instanceof DoricArrayBufferResource) {
JSArrayBuffer buffer = resource.getProperty("data").asArrayBuffer();
((DoricArrayBufferResource) doricResource).setValue(buffer);
}
doricContext.cacheResource(resId, doricResource); doricContext.cacheResource(resId, doricResource);
} }
} }

View File

@ -66,6 +66,7 @@
#import "DoricBlurEffectViewNode.h" #import "DoricBlurEffectViewNode.h"
#import "DoricAeroEffectViewNode.h" #import "DoricAeroEffectViewNode.h"
#import "DoricAssetsResourceLoader.h" #import "DoricAssetsResourceLoader.h"
#import "DoricArrayBufferResourceLoader.h"
@interface DoricRegistry () @interface DoricRegistry ()
@ -151,6 +152,7 @@ - (void)innerRegister {
[self.loaderManager registerLoader:[DoricBase64ResourceLoader new]]; [self.loaderManager registerLoader:[DoricBase64ResourceLoader new]];
[self.loaderManager registerLoader:[DoricCommonBundleResourceLoader new]]; [self.loaderManager registerLoader:[DoricCommonBundleResourceLoader new]];
[self.loaderManager registerLoader:[DoricAssetsResourceLoader new]]; [self.loaderManager registerLoader:[DoricAssetsResourceLoader new]];
[self.loaderManager registerLoader:[DoricArrayBufferResourceLoader new]];
} }
- (void)registerJSBundle:(NSString *)bundle withName:(NSString *)name { - (void)registerJSBundle:(NSString *)bundle withName:(NSString *)name {

View File

@ -0,0 +1,25 @@
/*
* 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.
*/
//
// Created by pengfei.zhou on 2022/3/4.
//
#import <Foundation/Foundation.h>
#import "DoricResource.h"
@interface DoricArrayBufferResource : DoricResource
@property(nonatomic, strong) NSData *data;
@end

View File

@ -0,0 +1,29 @@
/*
* 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.
*/
//
// Created by pengfei.zhou on 2022/3/4.
//
#import "DoricArrayBufferResource.h"
@implementation DoricArrayBufferResource
- (DoricAsyncResult <NSData *> *)fetchRaw {
DoricAsyncResult *asyncResult = [DoricAsyncResult new];
[asyncResult setupResult:self.data];
return asyncResult;
}
@end

View File

@ -0,0 +1,24 @@
/*
* 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.
*/
//
// Created by pengfei.zhou on 2022/3/4.
//
#import <Foundation/Foundation.h>
#import "DoricResourceLoader.h"
@interface DoricArrayBufferResourceLoader : NSObject <DoricResourceLoader>
@end

View File

@ -0,0 +1,33 @@
/*
* 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.
*/
//
// Created by pengfei.zhou on 2022/3/4.
//
#import "DoricArrayBufferResourceLoader.h"
#import "DoricArrayBufferResource.h"
@implementation DoricArrayBufferResourceLoader
- (NSString *)resourceType {
return @"arrayBuffer";
}
- (__kindof DoricResource *)load:(NSString *)identifier withContext:(DoricContext *)context {
DoricArrayBufferResource *resource = [[DoricArrayBufferResource alloc] initWithContext:context
identifier:[NSString stringWithFormat:@"assets/%@", identifier]];
return resource;
}
@end

View File

@ -19,6 +19,7 @@
#import "DoricResourceManager.h" #import "DoricResourceManager.h"
#import "DoricContext.h" #import "DoricContext.h"
#import "DoricArrayBufferResource.h"
@interface DoricResourceManager () @interface DoricResourceManager ()
@property(nonatomic, strong) NSMutableDictionary <NSString *, id <DoricResourceLoader>> *loaders; @property(nonatomic, strong) NSMutableDictionary <NSString *, id <DoricResourceLoader>> *loaders;
@ -57,6 +58,9 @@ - (__kindof DoricResource *)load:(NSDictionary *)resource
if (!doricResource) { if (!doricResource) {
id <DoricResourceLoader> loader = self.loaders[type]; id <DoricResourceLoader> loader = self.loaders[type];
doricResource = [loader load:identifier withContext:context]; doricResource = [loader load:identifier withContext:context];
if ([doricResource isKindOfClass:DoricArrayBufferResource.class]) {
((DoricArrayBufferResource *) doricResource).data = resource[@"data"];
}
[context.cachedResources setObject:doricResource forKey:resId]; [context.cachedResources setObject:doricResource forKey:resId];
} }
}); });

View File

@ -83,3 +83,18 @@ export class BundleResource extends iOSResource {
super("bundle", `${bundleName}://${fileName}`) super("bundle", `${bundleName}://${fileName}`)
} }
} }
export class ArrayBufferResource extends Resource {
data: ArrayBuffer;
constructor(data: ArrayBuffer) {
super("arrayBuffer", uniqueId("buffer"));
this.data = data;
}
toModel() {
const ret = super.toModel();
(ret as any).data = this.data;
return ret;
}
}