diff --git a/iOS/Loader/DoricHttpJSLoader.h b/iOS/Loader/DoricHttpJSLoader.h new file mode 100644 index 00000000..ce146f58 --- /dev/null +++ b/iOS/Loader/DoricHttpJSLoader.h @@ -0,0 +1,25 @@ +/* + * Copyright [2019] [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 2019/11/23. +// + +#import + +#import "DoricLoaderProtocol.h" + +@interface DoricHttpJSLoader : NSObject +@end \ No newline at end of file diff --git a/iOS/Loader/DoricHttpJSLoader.m b/iOS/Loader/DoricHttpJSLoader.m new file mode 100644 index 00000000..d9bd6a2c --- /dev/null +++ b/iOS/Loader/DoricHttpJSLoader.m @@ -0,0 +1,45 @@ +/* + * Copyright [2019] [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 2019/11/23. +// + +#import "DoricHttpJSLoader.h" + + +@implementation DoricHttpJSLoader + +- (BOOL)filter:(NSString *)scheme { + return [scheme hasPrefix:@"http"]; +} + +- (DoricAsyncResult *)request:(NSString *)scheme { + DoricAsyncResult *ret = [DoricAsyncResult new]; + NSURL *URL = [NSURL URLWithString:scheme]; + NSURLRequest *request = [NSURLRequest requestWithURL:URL]; + [[[NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]] + dataTaskWithRequest:request + completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) { + if (!error) { + NSString *dataStr = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; + [ret setupResult:dataStr]; + } else { + [ret setupError:[[NSException alloc] initWithName:@"DoricJSLoaderManager Exception" reason:error.description userInfo:nil]]; + } + }] resume]; + return ret; +} +@end \ No newline at end of file diff --git a/iOS/Loader/DoricJSLoaderManager.h b/iOS/Loader/DoricJSLoaderManager.h new file mode 100644 index 00000000..efbf2846 --- /dev/null +++ b/iOS/Loader/DoricJSLoaderManager.h @@ -0,0 +1,33 @@ +/* + * Copyright [2019] [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. + */ +// +// DoricJSLoaderManager.h +// Doric +// +// Created by pengfei.zhou on 2019/11/23. +// + +#import +#import "DoricLoaderProtocol.h" +#import "DoricAsyncResult.h" + +@interface DoricJSLoaderManager : NSObject ++ (instancetype)instance; + +- (void)addJSLoader:(id )loader; + +- (DoricAsyncResult *)request:(NSString *)scheme; +@end diff --git a/iOS/Loader/DoricJSLoaderManager.m b/iOS/Loader/DoricJSLoaderManager.m new file mode 100644 index 00000000..41cf0fc5 --- /dev/null +++ b/iOS/Loader/DoricJSLoaderManager.m @@ -0,0 +1,68 @@ +/* + * Copyright [2019] [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. + */ +// +// DoricJSLoaderManager.m +// Doric +// +// Created by pengfei.zhou on 2019/11/23. +// + +#import "DoricJSLoaderManager.h" +#import "DoricMainBundleJSLoader.h" +#import "DoricHttpJSLoader.h" +#import "Doric.h" + +@interface DoricJSLoaderManager () +@property(nonatomic, copy) NSSet > *loaders; +@end + +@implementation DoricJSLoaderManager ++ (instancetype)instance { + static DoricJSLoaderManager *_instance; + static dispatch_once_t onceToken; + dispatch_once(&onceToken, ^{ + _instance = [DoricJSLoaderManager new]; + }); + return _instance; +} + +- (instancetype)init { + if (self = [super init]) { + _loaders = [[NSSet alloc] initWithArray:@[ + [DoricMainBundleJSLoader new], + [DoricHttpJSLoader new], + ]]; + } + return self; +} + +- (void)addJSLoader:(id )loader { + self.loaders = [[self.loaders mutableCopy] also:^(NSMutableSet *it) { + [it addObject:loader]; + }]; +} + +- (DoricAsyncResult *)request:(NSString *)scheme { + __block DoricAsyncResult *ret; + [self.loaders enumerateObjectsUsingBlock:^(id obj, BOOL *stop) { + if ([obj filter:scheme]) { + ret = [obj request:scheme]; + *stop = YES; + } + }]; + return ret; +} +@end diff --git a/iOS/Loader/DoricLoaderProtocol.h b/iOS/Loader/DoricLoaderProtocol.h new file mode 100644 index 00000000..b3b509f0 --- /dev/null +++ b/iOS/Loader/DoricLoaderProtocol.h @@ -0,0 +1,27 @@ +/* + * Copyright [2019] [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 2019/11/23. +// + +#import +#import "DoricAsyncResult.h" + +@protocol DoricLoaderProtocol +- (BOOL)filter:(NSString *)scheme; + +- (DoricAsyncResult *)request:(NSString *)scheme; +@end \ No newline at end of file diff --git a/iOS/Loader/DoricMainBundleJSLoader.h b/iOS/Loader/DoricMainBundleJSLoader.h new file mode 100644 index 00000000..ff5d79ae --- /dev/null +++ b/iOS/Loader/DoricMainBundleJSLoader.h @@ -0,0 +1,25 @@ +/* + * Copyright [2019] [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 2019/11/23. +// + +#import + +#import "DoricLoaderProtocol.h" + +@interface DoricMainBundleJSLoader : NSObject +@end \ No newline at end of file diff --git a/iOS/Loader/DoricMainBundleJSLoader.m b/iOS/Loader/DoricMainBundleJSLoader.m new file mode 100644 index 00000000..63663098 --- /dev/null +++ b/iOS/Loader/DoricMainBundleJSLoader.m @@ -0,0 +1,42 @@ +/* + * Copyright [2019] [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 2019/11/23. +// + +#import "DoricMainBundleJSLoader.h" + + +@implementation DoricMainBundleJSLoader +- (BOOL)filter:(NSString *)scheme { + return [scheme hasPrefix:@"assets"]; +} + +- (DoricAsyncResult *)request:(NSString *)scheme { + DoricAsyncResult *ret = [DoricAsyncResult new]; + NSString *path = [[NSBundle mainBundle] bundlePath]; + NSString *fullPath = [path stringByAppendingPathComponent:[scheme substringFromIndex:@"assets://".length]]; + NSError *error; + NSString *jsContent = [NSString stringWithContentsOfFile:fullPath encoding:NSUTF8StringEncoding error:&error]; + if (error) { + [ret setupError:[NSException new]]; + } else { + [ret setupResult:jsContent]; + } + return ret; +} + +@end \ No newline at end of file