Skip to content

Commit

Permalink
refactor(ios): Revert rename and sdk frame refactor part 13
Browse files Browse the repository at this point in the history
HippyRootView and sdk start related change
  • Loading branch information
wwwcg committed Oct 12, 2023
1 parent 2fe5a86 commit d9f5695
Show file tree
Hide file tree
Showing 13 changed files with 1,084 additions and 135 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@

static NSString *const engineKey = @"Demo";

@interface HippyDemoViewController () <HippyMethodInterceptorProtocol, HippyBridgeDelegate> {
@interface HippyDemoViewController () <HippyMethodInterceptorProtocol, HippyBridgeDelegate, HippyRootViewDelegate> {
DriverType _driverType;
RenderType _renderType;
BOOL _isDebugMode;
Expand Down Expand Up @@ -111,13 +111,13 @@ - (void)runHippyCache {

- (void)runHippyDemo {
NSDictionary *launchOptions = @{@"EnableTurbo": @(DEMO_ENABLE_TURBO), @"DebugMode": @(_isDebugMode)};
NSString *executorKey = [NSString stringWithFormat:@"%@_%u", engineKey, arc4random()];

NSString *uniqueEngineKey = [NSString stringWithFormat:@"%@_%u", engineKey, arc4random()];
_hippyBridge = [[HippyBridge alloc] initWithDelegate:self
moduleProvider:nil
launchOptions:launchOptions
executorKey:executorKey];
_hippyBridge.contextName = executorKey;
engineKey:uniqueEngineKey];
_hippyBridge.contextName = uniqueEngineKey;
_hippyBridge.moduleName = @"Demo";
_hippyBridge.methodInterceptor = self;

Expand All @@ -132,15 +132,17 @@ - (void)mountConnector:(HippyBridge *)hippyBridge {
isSimulator = YES;
#endif

HippyRootView *rootView = [[HippyRootView alloc] initWithFrame:self.contentAreaView.bounds];
HippyRootView *rootView = [[HippyRootView alloc] initWithBridge:hippyBridge
moduleName:@"Demo"
initialProperties:@{@"isSimulator": @(isSimulator)}
delegate:self];
rootView.frame = self.contentAreaView.bounds;
rootView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
[hippyBridge setRootView:rootView];
NSNumber *rootTag = [rootView hippyTag];

if (_isDebugMode) {
hippyBridge.sandboxDirectory = [_debugURL URLByDeletingLastPathComponent];
[hippyBridge loadBundleURL:_debugURL completion:^(NSURL * _Nullable, NSError * _Nullable) {
[hippyBridge loadInstanceForRootView:rootTag withProperties:@{@"isSimulator": @(isSimulator)}];
[rootView runHippyApplication];
}];
} else {
NSURL *vendorBundleURL = [self vendorBundleURL];
Expand All @@ -151,7 +153,7 @@ - (void)mountConnector:(HippyBridge *)hippyBridge {
hippyBridge.sandboxDirectory = [indexBundleURL URLByDeletingLastPathComponent];
[hippyBridge loadBundleURL:indexBundleURL completion:^(NSURL * _Nullable, NSError * _Nullable) {
NSLog(@"url %@ load finish", indexBundleURL);
[hippyBridge loadInstanceForRootView:rootTag withProperties:@{@"isSimulator": @(isSimulator)}];
[rootView runHippyApplication];
}];
}

Expand Down
2 changes: 1 addition & 1 deletion framework/examples/ios-demo/HippyDemo/TestModule.mm
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ - (void)runCommonDemo:(nonnull NSString *)bundleUrl {
_connector = [[HippyBridge alloc] initWithDelegate:self
moduleProvider:nil
launchOptions:launchOptions
executorKey:engineKey];
engineKey:engineKey];
[_connector setInspectable:YES];
//set custom vfs loader
_connector.sandboxDirectory = sandboxDirectory;
Expand Down
10 changes: 4 additions & 6 deletions framework/ios/base/bridge/HippyBridge.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,13 @@
*/

#import <UIKit/UIKit.h>

#import "HippyBridgeDelegate.h"
#import "HippyBridgeModule.h"
#import "HippyMethodInterceptorProtocol.h"
#import "HippyModulesSetup.h"
#import "HippyImageProviderProtocol.h"
#import "HippyInvalidating.h"
#import "HippyDefines.h"

#include <memory>

@class HippyJSExecutor;
Expand Down Expand Up @@ -98,13 +96,13 @@ HIPPY_EXTERN NSString *HippyBridgeModuleNameForClass(Class bridgeModuleClass);
* @param delegate bridge delegate
* @param block for user-defined module
* @param launchOptions launch options, will not be sent to frontend
* @param executorKey key to engine instance. HippyBridge with same engine key will share same engine intance
* @param engineKey key to engine instance. HippyBridge with same engine key will share same engine intance
* @return A HippyBridge instance
*/
- (instancetype)initWithDelegate:(id<HippyBridgeDelegate>)delegate
moduleProvider:(nullable HippyBridgeModuleProviderBlock)block
launchOptions:(NSDictionary *)launchOptions
executorKey:(NSString *)executorKey;
launchOptions:(nullable NSDictionary *)launchOptions
engineKey:(nullable NSString *)engineKey;

/**
* Context name for HippyBridge
Expand Down Expand Up @@ -258,7 +256,7 @@ HIPPY_EXTERN NSString *HippyBridgeModuleNameForClass(Class bridgeModuleClass);

@property (nonatomic, assign) BOOL debugMode;

@property (nonatomic, strong) NSString *appVerson; //
@property (nonatomic, strong) NSString *appVerson;

@property (nonatomic, assign) HippyInvalidateReason invalidateReason;

Expand Down
48 changes: 24 additions & 24 deletions framework/ios/base/bridge/HippyBridge.mm
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,6 @@ @interface HippyBridge() {
BOOL _wasBatchActive;
HippyDisplayLink *_displayLink;
HippyBridgeModuleProviderBlock _moduleProvider;
NSString *_engineKey;
BOOL _valid;
HippyBundleOperationQueue *_bundlesQueue;
NSMutableArray<NSURL *> *_bundleURLs;
Expand All @@ -109,6 +108,13 @@ @interface HippyBridge() {

}

/// 用于标记bridge所使用的JS引擎的Key
///
/// 注意:传入相同值的bridge将共享底层JS引擎。
/// 在共享情况下,只有全部bridge实例均释放,JS引擎资源才会销毁。
/// 默认情况下对每个bridge使用独立JS引擎
@property (nonatomic, strong) NSString *engineKey;

@property(readwrite, strong) dispatch_semaphore_t moduleSemaphore;
@property(readwrite, assign) NSInteger loadingCount;

Expand Down Expand Up @@ -141,14 +147,14 @@ + (void)initialize {
- (instancetype)initWithDelegate:(id<HippyBridgeDelegate>)delegate
moduleProvider:(HippyBridgeModuleProviderBlock)block
launchOptions:(NSDictionary *)launchOptions
executorKey:(NSString *)executorKey {
engineKey:(NSString *)engineKey {
if (self = [super init]) {
_delegate = delegate;
_moduleProvider = block;
_bundleURLs = [NSMutableArray array];
_debugMode = [launchOptions[@"DebugMode"] boolValue];
_enableTurbo = !!launchOptions[@"EnableTurbo"] ? [launchOptions[@"EnableTurbo"] boolValue] : YES;
_engineKey = executorKey;
_engineKey = engineKey.length > 0 ? engineKey : [NSString stringWithFormat:@"%p", self];
_invalidateReason = HippyInvalidateReasonDealloc;
_valid = YES;
_bundlesQueue = [[HippyBundleOperationQueue alloc] init];
Expand Down Expand Up @@ -221,12 +227,8 @@ - (void)bindKeys {
#endif
}

- (NSString *)engineKey {
return _engineKey ?: [NSString stringWithFormat:@"%p", self];
}

- (void)setUpNativeRenderManager {
auto engineResource = [[HippyJSEnginesMapper defaultInstance] JSEngineResourceForKey:[self engineKey]];
auto engineResource = [[HippyJSEnginesMapper defaultInstance] JSEngineResourceForKey:self.engineKey];
auto domManager = engineResource->GetDomManager();
//Create NativeRenderManager
auto nativeRenderManager = std::make_shared<NativeRenderManager>();
Expand Down Expand Up @@ -299,6 +301,9 @@ - (BOOL)moduleIsInitialized:(Class)moduleClass {
return [_moduleSetup moduleIsInitialized:moduleClass];
}


#pragma mark - Debug Reload

- (void)reload {
if ([self.delegate respondsToSelector:@selector(reload:)]) {
self.invalidateReason = HippyInvalidateReasonReload;
Expand All @@ -316,13 +321,16 @@ - (void)requestReload {
}
}


#pragma mark - Bridge SetUp

- (void)setUp {
_valid = YES;
self.moduleSemaphore = dispatch_semaphore_create(0);
@try {
__weak HippyBridge *weakSelf = self;
_moduleSetup = [[HippyModulesSetup alloc] initWithBridge:self extraProviderModulesBlock:_moduleProvider];
_javaScriptExecutor = [[HippyJSExecutor alloc] initWithEngineKey:_engineKey bridge:self];
_javaScriptExecutor = [[HippyJSExecutor alloc] initWithEngineKey:self.engineKey bridge:self];
_javaScriptExecutor.contextCreatedBlock = ^(id<HippyContextWrapper> ctxWrapper){
HippyBridge *strongSelf = weakSelf;
if (strongSelf) {
Expand All @@ -340,21 +348,19 @@ - (void)setUp {
_javaScriptExecutor.contextName = _contextName;
}
_displayLink = [[HippyDisplayLink alloc] init];
//The caller may attempt to look up a module immediately after creating the HippyBridge,
//therefore the initialization of all modules cannot be placed in a sub-thread
// dispatch_async(HippyBridgeQueue(), ^{
[self initWithModulesCompletion:^{

// Setup all extra and internal modules
[_moduleSetup setupModulesCompletion:^{
HippyBridge *strongSelf = weakSelf;
if (strongSelf) {
dispatch_semaphore_signal(strongSelf.moduleSemaphore);
footstone::TimePoint endTime = footstone::TimePoint::SystemNow();
auto enty =
strongSelf.javaScriptExecutor.pScope->GetPerformance()->PerformanceNavigation("hippyInit");
auto enty = strongSelf.javaScriptExecutor.pScope->GetPerformance()->PerformanceNavigation("hippyInit");
enty->SetHippyNativeInitStart(strongSelf->_startTime);
enty->SetHippyNativeInitEnd(endTime);
}
}];
// });

} @catch (NSException *exception) {
HippyBridgeHandleException(exception, self);
}
Expand All @@ -376,12 +382,6 @@ - (void)loadBundleURL:(NSURL *)bundleURL
});
}



- (void)initWithModulesCompletion:(dispatch_block_t)completion {
[_moduleSetup setupModulesCompletion:completion];
}

- (void)beginLoadingBundle:(NSURL *)bundleURL
completion:(void (^)(NSURL * _Nullable, NSError * _Nullable))completion {
dispatch_group_t group = dispatch_group_create();
Expand Down Expand Up @@ -1056,7 +1056,7 @@ - (void)setSnapShotData:(NSData *)data {

//FIXME: 调整优化
- (void)setRootView:(HippyRootView *)rootView {
auto engineResource = [[HippyJSEnginesMapper defaultInstance] JSEngineResourceForKey:[self engineKey]];
auto engineResource = [[HippyJSEnginesMapper defaultInstance] JSEngineResourceForKey:self.engineKey];
auto domManager = engineResource->GetDomManager();
NSNumber *rootTag = [rootView hippyTag];
//Create a RootNode instance with a root tag
Expand Down Expand Up @@ -1090,7 +1090,7 @@ - (void)setRootView:(HippyRootView *)rootView {
}

- (void)resetRootSize:(CGSize)size {
auto engineResource = [[HippyJSEnginesMapper defaultInstance] JSEngineResourceForKey:[self engineKey]];
auto engineResource = [[HippyJSEnginesMapper defaultInstance] JSEngineResourceForKey:self.engineKey];
std::weak_ptr<hippy::RootNode> rootNode = _rootNode;
auto domManager = engineResource->GetDomManager();
std::weak_ptr<hippy::DomManager> weakDomManager = domManager;
Expand Down
10 changes: 2 additions & 8 deletions framework/ios/base/executors/HippyJSExecutor.mm
Original file line number Diff line number Diff line change
Expand Up @@ -233,11 +233,9 @@ - (void)setup {
}

- (instancetype)initWithEngineKey:(NSString *)engineKey bridge:(HippyBridge *)bridge {
NSParameterAssert(engineKey.length > 0);
if (self = [super init]) {
_valid = YES;
// maybe bug in JavaScriptCore:
// JSContextRef held by JSContextGroupRef cannot be deallocated,
// unless JSContextGroupRef is deallocated
self.enginekey = engineKey;
self.bridge = bridge;

Expand Down Expand Up @@ -351,7 +349,7 @@ - (void)invalidate {
#endif //JS_JSC
self.pScope->WillExit();
self.pScope = nullptr;
NSString *enginekey = [self enginekey];
NSString *enginekey = self.enginekey;
if (!enginekey) {
return;
}
Expand All @@ -360,10 +358,6 @@ - (void)invalidate {
});
}

- (NSString *)enginekey {
return _enginekey ?: [NSString stringWithFormat:@"%p", self];
}

- (void)setContextName:(NSString *)contextName {
#ifdef JS_JSC
if (!contextName) {
Expand Down
6 changes: 3 additions & 3 deletions framework/ios/base/modules/HippyModulesSetup.mm
Original file line number Diff line number Diff line change
Expand Up @@ -169,8 +169,8 @@ - (void)setupModulesCompletion:(dispatch_block_t)completion {
NSMutableArray<HippyModuleData *> *moduleDataByID = [NSMutableArray new];
NSMutableDictionary<NSString *, HippyModuleData *> *moduleDataByName = [NSMutableDictionary new];

for (id<HippyBridgeModule> module in extraModules) {
Class moduleClass = [module class];
for (id<HippyBridgeModule> extraModule in extraModules) {
Class moduleClass = [extraModule class];
NSString *moduleName = HippyBridgeModuleNameForClass(moduleClass);
if (HIPPY_DEBUG) {
// Check for name collisions between preregistered modules
Expand All @@ -183,7 +183,7 @@ - (void)setupModulesCompletion:(dispatch_block_t)completion {
}
}
// Instantiate moduleData container
HippyModuleData *moduleData = [[HippyModuleData alloc] initWithModuleInstance:module bridge:_bridge];
HippyModuleData *moduleData = [[HippyModuleData alloc] initWithModuleInstance:extraModule bridge:_bridge];
moduleDataByName[moduleName] = moduleData;
[moduleClassesByID addObject:moduleClass];
[moduleDataByID addObject:moduleData];
Expand Down
Loading

0 comments on commit d9f5695

Please sign in to comment.