feat:add JSLoader for iOS
This commit is contained in:
parent
656e72c174
commit
76bceb084e
25
iOS/Loader/DoricHttpJSLoader.h
Normal file
25
iOS/Loader/DoricHttpJSLoader.h
Normal file
@ -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 <Foundation/Foundation.h>
|
||||
|
||||
#import "DoricLoaderProtocol.h"
|
||||
|
||||
@interface DoricHttpJSLoader : NSObject <DoricLoaderProtocol>
|
||||
@end
|
45
iOS/Loader/DoricHttpJSLoader.m
Normal file
45
iOS/Loader/DoricHttpJSLoader.m
Normal file
@ -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 <NSString *> *)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
|
33
iOS/Loader/DoricJSLoaderManager.h
Normal file
33
iOS/Loader/DoricJSLoaderManager.h
Normal file
@ -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 <Foundation/Foundation.h>
|
||||
#import "DoricLoaderProtocol.h"
|
||||
#import "DoricAsyncResult.h"
|
||||
|
||||
@interface DoricJSLoaderManager : NSObject
|
||||
+ (instancetype)instance;
|
||||
|
||||
- (void)addJSLoader:(id <DoricLoaderProtocol>)loader;
|
||||
|
||||
- (DoricAsyncResult <NSString *> *)request:(NSString *)scheme;
|
||||
@end
|
68
iOS/Loader/DoricJSLoaderManager.m
Normal file
68
iOS/Loader/DoricJSLoaderManager.m
Normal file
@ -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 <id <DoricLoaderProtocol>> *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 <DoricLoaderProtocol>)loader {
|
||||
self.loaders = [[self.loaders mutableCopy] also:^(NSMutableSet *it) {
|
||||
[it addObject:loader];
|
||||
}];
|
||||
}
|
||||
|
||||
- (DoricAsyncResult <NSString *> *)request:(NSString *)scheme {
|
||||
__block DoricAsyncResult *ret;
|
||||
[self.loaders enumerateObjectsUsingBlock:^(id <DoricLoaderProtocol> obj, BOOL *stop) {
|
||||
if ([obj filter:scheme]) {
|
||||
ret = [obj request:scheme];
|
||||
*stop = YES;
|
||||
}
|
||||
}];
|
||||
return ret;
|
||||
}
|
||||
@end
|
27
iOS/Loader/DoricLoaderProtocol.h
Normal file
27
iOS/Loader/DoricLoaderProtocol.h
Normal file
@ -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 <Foundation/Foundation.h>
|
||||
#import "DoricAsyncResult.h"
|
||||
|
||||
@protocol DoricLoaderProtocol <NSObject>
|
||||
- (BOOL)filter:(NSString *)scheme;
|
||||
|
||||
- (DoricAsyncResult <NSString *> *)request:(NSString *)scheme;
|
||||
@end
|
25
iOS/Loader/DoricMainBundleJSLoader.h
Normal file
25
iOS/Loader/DoricMainBundleJSLoader.h
Normal file
@ -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 <Foundation/Foundation.h>
|
||||
|
||||
#import "DoricLoaderProtocol.h"
|
||||
|
||||
@interface DoricMainBundleJSLoader : NSObject <DoricLoaderProtocol>
|
||||
@end
|
42
iOS/Loader/DoricMainBundleJSLoader.m
Normal file
42
iOS/Loader/DoricMainBundleJSLoader.m
Normal file
@ -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 <NSString *> *)request:(NSString *)scheme {
|
||||
DoricAsyncResult <NSString *> *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
|
Reference in New Issue
Block a user