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

Commit fd4b750

Browse files
wqyfavorwqyfavor
authored and
wqyfavor
committed
[iOS] Add completion callback for registerService (#1776)
1 parent 6b9cbe0 commit fd4b750

File tree

4 files changed

+68
-12
lines changed

4 files changed

+68
-12
lines changed

ios/sdk/WeexSDK/Sources/Engine/WXSDKEngine.h

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,20 @@
8282
*/
8383
+ (void)registerService:(NSString *)name withScript:(NSString *)serviceScript withOptions:(NSDictionary *)options;
8484

85+
/**
86+
* @abstract Registers a component for a given name, options and js code
87+
*
88+
* @param name The service name to register
89+
*
90+
* @param options The service options to register
91+
*
92+
* @param serviceScript service js code to invoke
93+
*
94+
* @param completion Completion callback. JS is executed in asynchronously.
95+
*
96+
*/
97+
+ (void)registerService:(NSString *)name withScript:(NSString *)serviceScript withOptions:(NSDictionary *)options completion:(void(^)(BOOL result))completion;
98+
8599
/**
86100
* @abstract Registers a component for a given name, options and js url
87101
*
@@ -92,7 +106,21 @@
92106
* @param serviceScriptUrl The service url to register
93107
*
94108
*/
95-
+ (void)registerService:(NSString *)name withScriptUrl:(NSURL *)serviceScriptUrl WithOptions:(NSDictionary *)options;
109+
+ (void)registerService:(NSString *)name withScriptUrl:(NSURL *)serviceScriptUrl withOptions:(NSDictionary *)options;
110+
111+
/**
112+
* @abstract Registers a component for a given name, options and js url
113+
*
114+
* @param name The service name to register
115+
*
116+
* @param options The service options to register
117+
*
118+
* @param serviceScriptUrl The service url to register
119+
*
120+
* @param completion Completion callback. JS is executed in asynchronously.
121+
*
122+
*/
123+
+ (void)registerService:(NSString *)name withScriptUrl:(NSURL *)serviceScriptUrl withOptions:(NSDictionary *)options completion:(void(^)(BOOL result))completion;
96124

97125
/**
98126
* @abstract Registers a component for a given name, options and js code

ios/sdk/WeexSDK/Sources/Engine/WXSDKEngine.m

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -166,14 +166,25 @@ + (void)registerComponent:(NSString *)name withClass:(Class)clazz withProperties
166166

167167

168168
# pragma mark Service Register
169+
169170
+ (void)registerService:(NSString *)name withScript:(NSString *)serviceScript withOptions:(NSDictionary *)options
170171
{
171-
[[WXSDKManager bridgeMgr] registerService:name withService:serviceScript withOptions:options];
172+
[[WXSDKManager bridgeMgr] registerService:name withService:serviceScript withOptions:options completion:nil];
173+
}
174+
175+
+ (void)registerService:(NSString *)name withScript:(NSString *)serviceScript withOptions:(NSDictionary *)options completion:(void(^)(BOOL result))completion
176+
{
177+
[[WXSDKManager bridgeMgr] registerService:name withService:serviceScript withOptions:options completion:completion];
178+
}
179+
180+
+ (void)registerService:(NSString *)name withScriptUrl:(NSURL *)serviceScriptUrl withOptions:(NSDictionary *)options
181+
{
182+
[[WXSDKManager bridgeMgr] registerService:name withServiceUrl:serviceScriptUrl withOptions:options completion:nil];
172183
}
173184

174-
+ (void)registerService:(NSString *)name withScriptUrl:(NSURL *)serviceScriptUrl WithOptions:(NSDictionary *)options
185+
+ (void)registerService:(NSString *)name withScriptUrl:(NSURL *)serviceScriptUrl withOptions:(NSDictionary *)options completion:(void(^)(BOOL result))completion
175186
{
176-
[[WXSDKManager bridgeMgr] registerService:name withServiceUrl:serviceScriptUrl withOptions:options];
187+
[[WXSDKManager bridgeMgr] registerService:name withServiceUrl:serviceScriptUrl withOptions:options completion:completion];
177188
}
178189

179190
+ (void)unregisterService:(NSString *)name

ios/sdk/WeexSDK/Sources/Manager/WXBridgeManager.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,18 +102,19 @@ extern void WXPerformBlockOnBridgeThread(void (^block)(void));
102102
* @param name : service name
103103
* @param serviceScript : script code
104104
* @param options : service options
105+
* @param completion : completion callback
105106
**/
106-
- (void)registerService:(NSString *)name withService:(NSString *)serviceScript withOptions:(NSDictionary *)options;
107+
- (void)registerService:(NSString *)name withService:(NSString *)serviceScript withOptions:(NSDictionary *)options completion:(void(^)(BOOL result))completion;
107108

108109

109110
/**
110111
* Register JS service Script
111112
* @param name : service name
112113
* @param serviceScriptUrl : script url
113114
* @param options : service options
115+
* @param completion : completion callback
114116
**/
115-
116-
-(void)registerService:(NSString *)name withServiceUrl:(NSURL *)serviceScriptUrl withOptions:(NSDictionary *)options;
117+
-(void)registerService:(NSString *)name withServiceUrl:(NSURL *)serviceScriptUrl withOptions:(NSDictionary *)options completion:(void(^)(BOOL result))completion;
117118

118119
/**
119120
* Unregister JS service Script

ios/sdk/WeexSDK/Sources/Manager/WXBridgeManager.m

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -288,28 +288,41 @@ - (JSValue *)callJSMethodWithResult:(WXCallJSMethod *)method
288288
return value;
289289
}
290290

291-
-(void)registerService:(NSString *)name withServiceUrl:(NSURL *)serviceScriptUrl withOptions:(NSDictionary *)options
291+
- (void)registerService:(NSString *)name withServiceUrl:(NSURL *)serviceScriptUrl withOptions:(NSDictionary *)options completion:(void(^)(BOOL result))completion
292292
{
293-
if (!name || !serviceScriptUrl || !options) return;
293+
if (!name || !serviceScriptUrl || !options) {
294+
if (completion) {
295+
completion(NO);
296+
}
297+
return;
298+
}
294299
__weak typeof(self) weakSelf = self;
295300
WXResourceRequest *request = [WXResourceRequest requestWithURL:serviceScriptUrl resourceType:WXResourceTypeServiceBundle referrer:@"" cachePolicy:NSURLRequestUseProtocolCachePolicy];
296301
WXResourceLoader *serviceBundleLoader = [[WXResourceLoader alloc] initWithRequest:request];;
297302
serviceBundleLoader.onFinished = ^(WXResourceResponse *response, NSData *data) {
298303
__strong typeof(weakSelf) strongSelf = weakSelf;
299304
NSString *jsServiceString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
300-
[strongSelf registerService:name withService:jsServiceString withOptions:options];
305+
[strongSelf registerService:name withService:jsServiceString withOptions:options completion:completion];
301306
};
302307

303308
serviceBundleLoader.onFailed = ^(NSError *loadError) {
304309
WXLogError(@"No script URL found");
310+
if (completion) {
311+
completion(NO);
312+
}
305313
};
306314

307315
[serviceBundleLoader start];
308316
}
309317

310-
- (void)registerService:(NSString *)name withService:(NSString *)serviceScript withOptions:(NSDictionary *)options
318+
- (void)registerService:(NSString *)name withService:(NSString *)serviceScript withOptions:(NSDictionary *)options completion:(void(^)(BOOL result))completion
311319
{
312-
if (!name || !serviceScript || !options) return;
320+
if (!name || !serviceScript || !options) {
321+
if (completion) {
322+
completion(NO);
323+
}
324+
return;
325+
}
313326

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

@@ -318,6 +331,9 @@ - (void)registerService:(NSString *)name withService:(NSString *)serviceScript w
318331
// save it when execute
319332
[WXDebugTool cacheJsService:name withScript:serviceScript withOptions:options];
320333
[weakSelf.bridgeCtx executeJsService:script withName:name];
334+
if (completion) {
335+
completion(YES);
336+
}
321337
});
322338
}
323339

0 commit comments

Comments
 (0)