Skip to content

Commit

Permalink
* [ios] js service bug fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
IskenHuang committed Dec 26, 2016
1 parent 5914d27 commit c068bd7
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 36 deletions.
57 changes: 28 additions & 29 deletions ios/sdk/WeexSDK/Sources/Bridge/WXBridgeContext.m
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ @interface WXBridgeContext ()
//store some methods temporarily before JSFramework is loaded
@property (nonatomic, strong) NSMutableArray *methodQueue;
// store template service
@property (nonatomic, strong) NSMutableDictionary *jsServiceDic;
@property (nonatomic, strong) NSMutableArray *jsServiceQueue;

@end

Expand All @@ -61,7 +61,7 @@ - (instancetype) init
if (self) {
_methodQueue = [NSMutableArray new];
_frameworkLoadFinished = NO;
_jsServiceDic = [NSMutableDictionary dictionary];
_jsServiceQueue = [NSMutableArray new];
}
return self;
}
Expand Down Expand Up @@ -248,8 +248,6 @@ - (void)executeJsFramework:(NSString *)script

[self.jsBridge executeJSFramework:script];

[self executeAllJsService];

WX_MONITOR_PERF_END(WXPTFrameworkExecute);

if ([self.jsBridge exception]) {
Expand All @@ -260,6 +258,8 @@ - (void)executeJsFramework:(NSString *)script
//the JSFramework has been load successfully.
self.frameworkLoadFinished = YES;

[self executeAllJsService];

JSValue *frameworkVersion = [self.jsBridge callJSMethod:@"getJSFMVersion" args:nil];
if (frameworkVersion && [frameworkVersion isString]) {
[WXAppConfiguration setJSFrameworkVersion:[frameworkVersion toString]];
Expand Down Expand Up @@ -297,37 +297,36 @@ - (void)executeJsMethod:(WXBridgeMethod *)method

- (void)executeAllJsService
{
WXAssertBridgeThread();

// TODO: for in executeJSServices
for(NSString *name in _jsServiceDic) {
NSString *script = [_jsServiceDic objectForKey:name];
for(NSDictionary *service in _jsServiceQueue) {
NSString *script = [service valueForKey:@"script"];
NSString *name = [service valueForKey:@"name"];
[self executeJsService:script withName:name];
}

[_jsServiceQueue removeAllObjects];
}

- (void)executeJsService:(NSString *)script withName: (NSString *)name
- (void)executeJsService:(NSString *)script withName:(NSString *)name
{
WXAssertBridgeThread();
if(!self.frameworkLoadFinished) {
[_jsServiceDic setObject:script forKey:name];
return;
}

WXAssert(script, @"param script required!");
[self.jsBridge executeJs: script];

if(_jsServiceDic[name]) {
[_jsServiceDic removeObjectForKey:name];
if(self.frameworkLoadFinished) {
// TODO - FOR DEBUG DELETE IT!! simulator registerService. because jsfm not work now.
[self.jsBridge executeJs: @"var global = { registerService: function(){ console.log('registerService') }, unregisterService: function(){ console.log('unregisterService') } }"];

WXAssert(script, @"param script required!");
[self.jsBridge executeJs: script];

if ([self.jsBridge exception]) {
NSString *message = [NSString stringWithFormat:@"JSService executes error: %@", [self.jsBridge exception]];
WX_MONITOR_FAIL(WXMTJSService, WX_ERR_JSFRAMEWORK_EXECUTE, message);
} else {
// success
}
}else {
[_jsServiceQueue addObject:@{
@"name": name,
@"script": script
}];
}

if ([self.jsBridge exception]) {
NSString *message = [NSString stringWithFormat:@"JSService executes error: %@", [self.jsBridge exception]];
WX_MONITOR_FAIL(WXMTJSService, WX_ERR_JSFRAMEWORK_EXECUTE, message);
} else {
// success
};

}

- (void)registerModules:(NSDictionary *)modules
Expand Down
2 changes: 1 addition & 1 deletion ios/sdk/WeexSDK/Sources/Manager/WXBridgeManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ - (void)registerService:(NSString *)name withService:(NSString *)serviceScript w

__weak typeof(self) weakSelf = self;
WXPerformBlockOnBridgeThread(^(){
[weakSelf.bridgeCtx executeJsService:script withName:@""];
[weakSelf.bridgeCtx executeJsService:script withName:name];
});
}

Expand Down
20 changes: 14 additions & 6 deletions ios/sdk/WeexSDK/Sources/Manager/WXServiceFactory.m
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,22 @@ - (NSDictionary *)registerService:(NSString *)name withService:(NSString *)servi
}

- (NSString *) getServiceScript:(NSString *)name withService:(NSString *)serviceScript withOptions:(NSDictionary *)options {
NSError *error;

// setup options for service
NSMutableDictionary *optionDic = [NSMutableDictionary dictionaryWithDictionary:options];
[optionDic setObject:name forKey:@"serviceName"];
NSDictionary *funcDic = @{
@"register": @"global.registerService",
@"unregister": @"global.unregisterService"
};
NSString *script = [NSString stringWithFormat:@";(fucntion(service, options){ %@ })(%@, %@);"
, serviceScript, funcDic, optionDic];
NSData *optionsData = [NSJSONSerialization dataWithJSONObject:optionDic options:NSJSONWritingPrettyPrinted error:&error];
NSString *optionsString = [[NSString alloc] initWithData:optionsData encoding:NSUTF8StringEncoding];

// setup global function to service
NSString *funcString = @"{"
@"register: global.registerService,"
@"unregister: global.unregisterService"
@"}";

NSString *script = [NSString stringWithFormat:@";(function(service, options){ %@ })(%@, %@);"
, serviceScript, funcString, optionsString];
return script;
}

Expand Down

0 comments on commit c068bd7

Please sign in to comment.