Skip to content
This repository has been archived by the owner on Jun 3, 2021. It is now read-only.

Commit

Permalink
[iOS] Add completion callback for registerService (#1776)
Browse files Browse the repository at this point in the history
  • Loading branch information
wqyfavor authored and cxfeng1 committed Nov 19, 2018
1 parent e3151ef commit eec53b1
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 12 deletions.
30 changes: 29 additions & 1 deletion ios/sdk/WeexSDK/Sources/Engine/WXSDKEngine.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,20 @@
*/
+ (void)registerService:(NSString *)name withScript:(NSString *)serviceScript withOptions:(NSDictionary *)options;

/**
* @abstract Registers a component for a given name, options and js code
*
* @param name The service name to register
*
* @param options The service options to register
*
* @param serviceScript service js code to invoke
*
* @param completion Completion callback. JS is executed in asynchronously.
*
*/
+ (void)registerService:(NSString *)name withScript:(NSString *)serviceScript withOptions:(NSDictionary *)options completion:(void(^)(BOOL result))completion;

/**
* @abstract Registers a component for a given name, options and js url
*
Expand All @@ -92,7 +106,21 @@
* @param serviceScriptUrl The service url to register
*
*/
+ (void)registerService:(NSString *)name withScriptUrl:(NSURL *)serviceScriptUrl WithOptions:(NSDictionary *)options;
+ (void)registerService:(NSString *)name withScriptUrl:(NSURL *)serviceScriptUrl withOptions:(NSDictionary *)options;

/**
* @abstract Registers a component for a given name, options and js url
*
* @param name The service name to register
*
* @param options The service options to register
*
* @param serviceScriptUrl The service url to register
*
* @param completion Completion callback. JS is executed in asynchronously.
*
*/
+ (void)registerService:(NSString *)name withScriptUrl:(NSURL *)serviceScriptUrl withOptions:(NSDictionary *)options completion:(void(^)(BOOL result))completion;

/**
* @abstract Registers a component for a given name, options and js code
Expand Down
17 changes: 14 additions & 3 deletions ios/sdk/WeexSDK/Sources/Engine/WXSDKEngine.m
Original file line number Diff line number Diff line change
Expand Up @@ -166,14 +166,25 @@ + (void)registerComponent:(NSString *)name withClass:(Class)clazz withProperties


# pragma mark Service Register

+ (void)registerService:(NSString *)name withScript:(NSString *)serviceScript withOptions:(NSDictionary *)options
{
[[WXSDKManager bridgeMgr] registerService:name withService:serviceScript withOptions:options];
[[WXSDKManager bridgeMgr] registerService:name withService:serviceScript withOptions:options completion:nil];
}

+ (void)registerService:(NSString *)name withScript:(NSString *)serviceScript withOptions:(NSDictionary *)options completion:(void(^)(BOOL result))completion
{
[[WXSDKManager bridgeMgr] registerService:name withService:serviceScript withOptions:options completion:completion];
}

+ (void)registerService:(NSString *)name withScriptUrl:(NSURL *)serviceScriptUrl withOptions:(NSDictionary *)options
{
[[WXSDKManager bridgeMgr] registerService:name withServiceUrl:serviceScriptUrl withOptions:options completion:nil];
}

+ (void)registerService:(NSString *)name withScriptUrl:(NSURL *)serviceScriptUrl WithOptions:(NSDictionary *)options
+ (void)registerService:(NSString *)name withScriptUrl:(NSURL *)serviceScriptUrl withOptions:(NSDictionary *)options completion:(void(^)(BOOL result))completion
{
[[WXSDKManager bridgeMgr] registerService:name withServiceUrl:serviceScriptUrl withOptions:options];
[[WXSDKManager bridgeMgr] registerService:name withServiceUrl:serviceScriptUrl withOptions:options completion:completion];
}

+ (void)unregisterService:(NSString *)name
Expand Down
7 changes: 4 additions & 3 deletions ios/sdk/WeexSDK/Sources/Manager/WXBridgeManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -102,18 +102,19 @@ extern void WXPerformBlockOnBridgeThread(void (^block)(void));
* @param name : service name
* @param serviceScript : script code
* @param options : service options
* @param completion : completion callback
**/
- (void)registerService:(NSString *)name withService:(NSString *)serviceScript withOptions:(NSDictionary *)options;
- (void)registerService:(NSString *)name withService:(NSString *)serviceScript withOptions:(NSDictionary *)options completion:(void(^)(BOOL result))completion;


/**
* Register JS service Script
* @param name : service name
* @param serviceScriptUrl : script url
* @param options : service options
* @param completion : completion callback
**/

-(void)registerService:(NSString *)name withServiceUrl:(NSURL *)serviceScriptUrl withOptions:(NSDictionary *)options;
-(void)registerService:(NSString *)name withServiceUrl:(NSURL *)serviceScriptUrl withOptions:(NSDictionary *)options completion:(void(^)(BOOL result))completion;

/**
* Unregister JS service Script
Expand Down
26 changes: 21 additions & 5 deletions ios/sdk/WeexSDK/Sources/Manager/WXBridgeManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -288,28 +288,41 @@ - (JSValue *)callJSMethodWithResult:(WXCallJSMethod *)method
return value;
}

-(void)registerService:(NSString *)name withServiceUrl:(NSURL *)serviceScriptUrl withOptions:(NSDictionary *)options
- (void)registerService:(NSString *)name withServiceUrl:(NSURL *)serviceScriptUrl withOptions:(NSDictionary *)options completion:(void(^)(BOOL result))completion
{
if (!name || !serviceScriptUrl || !options) return;
if (!name || !serviceScriptUrl || !options) {
if (completion) {
completion(NO);
}
return;
}
__weak typeof(self) weakSelf = self;
WXResourceRequest *request = [WXResourceRequest requestWithURL:serviceScriptUrl resourceType:WXResourceTypeServiceBundle referrer:@"" cachePolicy:NSURLRequestUseProtocolCachePolicy];
WXResourceLoader *serviceBundleLoader = [[WXResourceLoader alloc] initWithRequest:request];;
serviceBundleLoader.onFinished = ^(WXResourceResponse *response, NSData *data) {
__strong typeof(weakSelf) strongSelf = weakSelf;
NSString *jsServiceString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
[strongSelf registerService:name withService:jsServiceString withOptions:options];
[strongSelf registerService:name withService:jsServiceString withOptions:options completion:completion];
};

serviceBundleLoader.onFailed = ^(NSError *loadError) {
WXLogError(@"No script URL found");
if (completion) {
completion(NO);
}
};

[serviceBundleLoader start];
}

- (void)registerService:(NSString *)name withService:(NSString *)serviceScript withOptions:(NSDictionary *)options
- (void)registerService:(NSString *)name withService:(NSString *)serviceScript withOptions:(NSDictionary *)options completion:(void(^)(BOOL result))completion
{
if (!name || !serviceScript || !options) return;
if (!name || !serviceScript || !options) {
if (completion) {
completion(NO);
}
return;
}

NSString *script = [WXServiceFactory registerServiceScript:name withRawScript:serviceScript withOptions:options];

Expand All @@ -318,6 +331,9 @@ - (void)registerService:(NSString *)name withService:(NSString *)serviceScript w
// save it when execute
[WXDebugTool cacheJsService:name withScript:serviceScript withOptions:options];
[weakSelf.bridgeCtx executeJsService:script withName:name];
if (completion) {
completion(YES);
}
});
}

Expand Down

0 comments on commit eec53b1

Please sign in to comment.