From e8e344ed2108649eed0fc1f52efb6758becb49c0 Mon Sep 17 00:00:00 2001 From: "pengfei.zhou" Date: Mon, 7 Mar 2022 18:31:14 +0800 Subject: [PATCH] feat: add ArrayBufferResource --- .../main/java/pub/doric/DoricRegistry.java | 3 ++ .../resource/DoricArrayBufferLoader.java | 35 ++++++++++++++ .../resource/DoricArrayBufferResource.java | 46 +++++++++++++++++++ .../doric/resource/DoricResourceManager.java | 7 +++ doric-iOS/Pod/Classes/DoricRegistry.m | 2 + .../Resource/DoricArrayBufferResource.h | 25 ++++++++++ .../Resource/DoricArrayBufferResource.m | 29 ++++++++++++ .../Resource/DoricArrayBufferResourceLoader.h | 24 ++++++++++ .../Resource/DoricArrayBufferResourceLoader.m | 33 +++++++++++++ .../Classes/Resource/DoricResourceManager.m | 4 ++ doric-js/src/util/resource.ts | 17 ++++++- 11 files changed, 224 insertions(+), 1 deletion(-) create mode 100644 doric-android/doric/src/main/java/pub/doric/resource/DoricArrayBufferLoader.java create mode 100644 doric-android/doric/src/main/java/pub/doric/resource/DoricArrayBufferResource.java create mode 100644 doric-iOS/Pod/Classes/Resource/DoricArrayBufferResource.h create mode 100644 doric-iOS/Pod/Classes/Resource/DoricArrayBufferResource.m create mode 100644 doric-iOS/Pod/Classes/Resource/DoricArrayBufferResourceLoader.h create mode 100644 doric-iOS/Pod/Classes/Resource/DoricArrayBufferResourceLoader.m diff --git a/doric-android/doric/src/main/java/pub/doric/DoricRegistry.java b/doric-android/doric/src/main/java/pub/doric/DoricRegistry.java index effe8f65..c07a3d4c 100644 --- a/doric-android/doric/src/main/java/pub/doric/DoricRegistry.java +++ b/doric-android/doric/src/main/java/pub/doric/DoricRegistry.java @@ -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)); diff --git a/doric-android/doric/src/main/java/pub/doric/resource/DoricArrayBufferLoader.java b/doric-android/doric/src/main/java/pub/doric/resource/DoricArrayBufferLoader.java new file mode 100644 index 00000000..355af8ff --- /dev/null +++ b/doric-android/doric/src/main/java/pub/doric/resource/DoricArrayBufferLoader.java @@ -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); + } +} diff --git a/doric-android/doric/src/main/java/pub/doric/resource/DoricArrayBufferResource.java b/doric-android/doric/src/main/java/pub/doric/resource/DoricArrayBufferResource.java new file mode 100644 index 00000000..f8401222 --- /dev/null +++ b/doric-android/doric/src/main/java/pub/doric/resource/DoricArrayBufferResource.java @@ -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 fetchRaw() { + final AsyncResult result = new AsyncResult<>(); + result.setResult(this.value.value()); + return result; + } +} diff --git a/doric-android/doric/src/main/java/pub/doric/resource/DoricResourceManager.java b/doric-android/doric/src/main/java/pub/doric/resource/DoricResourceManager.java index 9fbefdbf..38b8c3bd 100644 --- a/doric-android/doric/src/main/java/pub/doric/resource/DoricResourceManager.java +++ b/doric-android/doric/src/main/java/pub/doric/resource/DoricResourceManager.java @@ -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); } } diff --git a/doric-iOS/Pod/Classes/DoricRegistry.m b/doric-iOS/Pod/Classes/DoricRegistry.m index 84163d6b..eb8f9842 100644 --- a/doric-iOS/Pod/Classes/DoricRegistry.m +++ b/doric-iOS/Pod/Classes/DoricRegistry.m @@ -66,6 +66,7 @@ #import "DoricBlurEffectViewNode.h" #import "DoricAeroEffectViewNode.h" #import "DoricAssetsResourceLoader.h" +#import "DoricArrayBufferResourceLoader.h" @interface DoricRegistry () @@ -151,6 +152,7 @@ - (void)innerRegister { [self.loaderManager registerLoader:[DoricBase64ResourceLoader new]]; [self.loaderManager registerLoader:[DoricCommonBundleResourceLoader new]]; [self.loaderManager registerLoader:[DoricAssetsResourceLoader new]]; + [self.loaderManager registerLoader:[DoricArrayBufferResourceLoader new]]; } - (void)registerJSBundle:(NSString *)bundle withName:(NSString *)name { diff --git a/doric-iOS/Pod/Classes/Resource/DoricArrayBufferResource.h b/doric-iOS/Pod/Classes/Resource/DoricArrayBufferResource.h new file mode 100644 index 00000000..de056598 --- /dev/null +++ b/doric-iOS/Pod/Classes/Resource/DoricArrayBufferResource.h @@ -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 +#import "DoricResource.h" + +@interface DoricArrayBufferResource : DoricResource +@property(nonatomic, strong) NSData *data; +@end \ No newline at end of file diff --git a/doric-iOS/Pod/Classes/Resource/DoricArrayBufferResource.m b/doric-iOS/Pod/Classes/Resource/DoricArrayBufferResource.m new file mode 100644 index 00000000..7bea1aa8 --- /dev/null +++ b/doric-iOS/Pod/Classes/Resource/DoricArrayBufferResource.m @@ -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 *)fetchRaw { + DoricAsyncResult *asyncResult = [DoricAsyncResult new]; + [asyncResult setupResult:self.data]; + return asyncResult; +} +@end \ No newline at end of file diff --git a/doric-iOS/Pod/Classes/Resource/DoricArrayBufferResourceLoader.h b/doric-iOS/Pod/Classes/Resource/DoricArrayBufferResourceLoader.h new file mode 100644 index 00000000..19d8704b --- /dev/null +++ b/doric-iOS/Pod/Classes/Resource/DoricArrayBufferResourceLoader.h @@ -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 +#import "DoricResourceLoader.h" + +@interface DoricArrayBufferResourceLoader : NSObject +@end \ No newline at end of file diff --git a/doric-iOS/Pod/Classes/Resource/DoricArrayBufferResourceLoader.m b/doric-iOS/Pod/Classes/Resource/DoricArrayBufferResourceLoader.m new file mode 100644 index 00000000..6d8a8bf5 --- /dev/null +++ b/doric-iOS/Pod/Classes/Resource/DoricArrayBufferResourceLoader.m @@ -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 \ No newline at end of file diff --git a/doric-iOS/Pod/Classes/Resource/DoricResourceManager.m b/doric-iOS/Pod/Classes/Resource/DoricResourceManager.m index 2c29cb48..bc082202 100644 --- a/doric-iOS/Pod/Classes/Resource/DoricResourceManager.m +++ b/doric-iOS/Pod/Classes/Resource/DoricResourceManager.m @@ -19,6 +19,7 @@ #import "DoricResourceManager.h" #import "DoricContext.h" +#import "DoricArrayBufferResource.h" @interface DoricResourceManager () @property(nonatomic, strong) NSMutableDictionary > *loaders; @@ -57,6 +58,9 @@ - (__kindof DoricResource *)load:(NSDictionary *)resource if (!doricResource) { id loader = self.loaders[type]; doricResource = [loader load:identifier withContext:context]; + if ([doricResource isKindOfClass:DoricArrayBufferResource.class]) { + ((DoricArrayBufferResource *) doricResource).data = resource[@"data"]; + } [context.cachedResources setObject:doricResource forKey:resId]; } }); diff --git a/doric-js/src/util/resource.ts b/doric-js/src/util/resource.ts index f63a720e..56944259 100644 --- a/doric-js/src/util/resource.ts +++ b/doric-js/src/util/resource.ts @@ -82,4 +82,19 @@ export class BundleResource extends iOSResource { constructor(bundleName: string, fileName: string) { super("bundle", `${bundleName}://${fileName}`) } -} \ No newline at end of file +} + +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; + } +}