Skip to content

Commit

Permalink
feat(ios): user can mount custom objects before execute JS
Browse files Browse the repository at this point in the history
  • Loading branch information
ozonelmy authored and xuqingkuang committed Apr 23, 2020
1 parent ce76475 commit 0df5b43
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 7 deletions.
20 changes: 13 additions & 7 deletions ios/sdk/base/HippyBatchedBridge.mm
Expand Up @@ -217,17 +217,23 @@ - (void)start
dispatch_group_notify(initModulesAndLoadSource, bridgeQueue, ^{
HippyBatchedBridge *strongSelf = weakSelf;
if (sourceCode && strongSelf.loading) {
//加载common包之前注入一些全局变量信息
NSString *deviceInfo = [self deviceInfo];
[strongSelf->_javaScriptExecutor injectJSONText:deviceInfo asGlobalObjectNamed:@"__HIPPYNATIVEGLOBAL__" callback:^(NSError *error) {

// load bootstrap.js after config has been setted
//mount custom objects before executing JS Code
dispatch_group_t objectGroup = dispatch_group_create();
NSDictionary *objects = [self objectsBeforeExecuteCode];
for (NSString *key in [objects allKeys]) {
NSString *value = objects[key];
HippyAssert([value isKindOfClass:[NSString class]], @"value must be NSString");
dispatch_group_enter(objectGroup);
[strongSelf->_javaScriptExecutor injectJSONText:value asGlobalObjectNamed:key callback:^(NSError *error) {
dispatch_group_leave(objectGroup);
}];
}
dispatch_group_notify(objectGroup, bridgeQueue, ^{
if (self->_javaScriptExecutor.pEnv.lock()) {
self->_javaScriptExecutor.pEnv.lock()->loadModules();
}

[strongSelf executeSourceCode:sourceCode];
}];
});
}
});

Expand Down
11 changes: 11 additions & 0 deletions ios/sdk/base/HippyBridge.h
Expand Up @@ -174,6 +174,17 @@ HIPPY_EXTERN NSString *HippyBridgeModuleNameForClass(Class bridgeModuleClass);
*/
- (void)whitelistedModulesDidChange;

/**
* Called and inject Object before Hippy execute JS source code
* Keys will be mounted at JS Global Object.
* Values will be mounted at Keys.
* Values must be JSON Strings.
* Default behivor is to insert device info mounted at __HIPPYNATIVEGLOBAL__ object
* subclass can override it and mount custom objects
* IMPORTANT:subclass must call [super objectsBeforeExecuteCode] and add its entries to yours
*/
- (NSDictionary *)objectsBeforeExecuteCode;

/**
* All registered bridge module classes.
*/
Expand Down
9 changes: 9 additions & 0 deletions ios/sdk/base/HippyBridge.mm
Expand Up @@ -307,6 +307,15 @@ - (void)whitelistedModulesDidChange
[self.batchedBridge whitelistedModulesDidChange];
}

- (NSString *)deviceInfo {
//implement in subclass HippyBatchedBridge.mm
return @"";
}

- (NSDictionary *)objectsBeforeExecuteCode {
return @{@"__HIPPYNATIVEGLOBAL__": [self deviceInfo]};
}

- (void)reload
{
/**
Expand Down

0 comments on commit 0df5b43

Please sign in to comment.