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 5a25dc4
Show file tree
Hide file tree
Showing 12 changed files with 1,146 additions and 148 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;
executorKey: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
47 changes: 33 additions & 14 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 @@ -92,19 +90,40 @@ HIPPY_EXTERN NSString *HippyBridgeModuleNameForClass(Class bridgeModuleClass);

@property (nonatomic, copy, readonly) NSDictionary *launchOptions;

/**
* Create A HippyBridge instance
*
* @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
* @return A HippyBridge instance
*/


/// Create A HippyBridge instance, without load/execute any js bundle.
///
/// @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.
///
/// Note: 多个bridge使用相同的共享engineKey时,只有全部bridge实例销毁时engine资源才将释放,因此,请注意合理使用,避免出现意外的内存泄漏。
/// 传空时默认不共享,SDK内部默认分配一随机key。
- (instancetype)initWithDelegate:(id<HippyBridgeDelegate>)delegate
moduleProvider:(nullable HippyBridgeModuleProviderBlock)block
launchOptions:(nullable NSDictionary *)launchOptions
executorKey:(nullable NSString *)executorKey;


/// Create A HippyBridge instance with a common js bundle.
///
/// This method is compatible with the Hippy2 initializer function.
///
/// @param delegate bridge delegate
/// @param bundleURL the
/// @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.
///
/// Note: 多个bridge使用相同的共享engineKey时,只有全部bridge实例销毁时engine资源才将释放,因此,请注意合理使用,避免出现意外的内存泄漏。
/// 传空时默认不共享,SDK内部默认分配一随机key。
- (instancetype)initWithDelegate:(id<HippyBridgeDelegate>)delegate
bundleURL:(nullable NSURL *)bundleURL
moduleProvider:(nullable HippyBridgeModuleProviderBlock)block
launchOptions:(NSDictionary *)launchOptions
executorKey:(NSString *)executorKey;
launchOptions:(nullable NSDictionary *)launchOptions
executorKey:(nullable NSString *)executorKey;

/**
* Context name for HippyBridge
Expand Down Expand Up @@ -258,7 +277,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
89 changes: 61 additions & 28 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,15 @@ @interface HippyBridge() {

}

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

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

Expand Down Expand Up @@ -141,19 +149,33 @@ + (void)initialize {
- (instancetype)initWithDelegate:(id<HippyBridgeDelegate>)delegate
moduleProvider:(HippyBridgeModuleProviderBlock)block
launchOptions:(NSDictionary *)launchOptions
executorKey:(NSString *)executorKey {
executorKey:(nullable NSString *)executorKey {
return [self initWithDelegate:delegate
bundleURL:nil
moduleProvider:block
launchOptions:launchOptions
executorKey:executorKey];
}

- (instancetype)initWithDelegate:(id<HippyBridgeDelegate>)delegate
bundleURL:(NSURL *)bundleURL
moduleProvider:(HippyBridgeModuleProviderBlock)block
launchOptions:(NSDictionary *)launchOptions
executorKey:(nullable NSString *)executorKey {
if (self = [super init]) {
_delegate = delegate;
_moduleProvider = block;
_pendingLoadingVendorBundleURL = bundleURL;
_bundleURLs = [NSMutableArray array];
_debugMode = [launchOptions[@"DebugMode"] boolValue];
_enableTurbo = !!launchOptions[@"EnableTurbo"] ? [launchOptions[@"EnableTurbo"] boolValue] : YES;
_engineKey = executorKey;
_engineKey = executorKey.length > 0 ? executorKey : [NSString stringWithFormat:@"%p", self];
_invalidateReason = HippyInvalidateReasonDealloc;
_valid = YES;
_bundlesQueue = [[HippyBundleOperationQueue alloc] init];
_startTime = footstone::TimePoint::SystemNow();
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(rootViewContentDidAppear:) name:kRootViewDidAddContent object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(rootViewContentDidAppear:)
name:HippyContentDidAppearNotification object:nil];
[self setUp];
HippyExecuteOnMainThread(^{
[self bindKeys];
Expand All @@ -166,13 +188,15 @@ - (instancetype)initWithDelegate:(id<HippyBridgeDelegate>)delegate
[self setUpNativeRenderManager];

[HippyBridge setCurrentBridge:self];

[self loadPendingVendorBundleURLIfNeeded];
HippyLogInfo(@"[Hippy_OC_Log][Life_Circle],%@ Init %p", NSStringFromClass([self class]), self);
}
return self;
}

- (void)rootViewContentDidAppear:(NSNotification *)noti {
UIView *rootView = [[noti userInfo] objectForKey:kRootViewKey];
UIView *rootView = [noti object];
if (rootView) {
auto domManager = _javaScriptExecutor.pScope->GetDomManager().lock();
if (domManager) {
Expand Down Expand Up @@ -221,12 +245,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 +319,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 +339,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,26 +366,39 @@ - (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);
}
}


/// 加载初始化bridge时传入的Bundle URL
- (void)loadPendingVendorBundleURLIfNeeded {
if (self.pendingLoadingVendorBundleURL) {
[self loadBundleURL:self.pendingLoadingVendorBundleURL completion:^(NSURL * _Nullable url, NSError * _Nullable error) {
if (error) {
HippyLogError(@"[Hippy_OC_Log][HippyBridge], bundle loaded error:%@, %@", url, error.description);
} else {
HippyLogInfo(@"[Hippy_OC_Log][HippyBridge], bundle loaded success:%@", url);
}
}];
}
}


- (void)loadBundleURL:(NSURL *)bundleURL
completion:(void (^_Nullable)(NSURL * _Nullable, NSError * _Nullable))completion {
if (!bundleURL) {
Expand All @@ -376,12 +415,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 @@ -531,7 +564,7 @@ - (void)executeJSCode:(NSData *)script
dispatch_async(dispatch_get_main_queue(), ^{
NSDictionary *userInfo = @{@"bridge": self, sourceURL: sourceURL};
[[NSNotificationCenter defaultCenter] postNotificationName:HippyJavaScriptDidLoadNotification
object:nil
object:self
userInfo:userInfo];
});
}
Expand All @@ -555,7 +588,7 @@ - (void)stopLoadingWithError:(NSError *)error scriptSourceURL:(NSURL *)sourceURL
}];
NSDictionary *userInfo = @{@"bridge": self, @"error": error, @"sourceURL": sourceURL};
[[NSNotificationCenter defaultCenter] postNotificationName:HippyJavaScriptDidFailToLoadNotification
object:nil
object:self
userInfo:userInfo];
if ([error userInfo][HippyJSStackTraceKey]) {
[self.redBox showErrorMessage:[error localizedDescription] withStack:[error userInfo][HippyJSStackTraceKey]];
Expand Down Expand Up @@ -1056,7 +1089,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 +1123,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
Loading

0 comments on commit 5a25dc4

Please sign in to comment.