Skip to content

Commit

Permalink
refactor(ios): clean and refactor the HippyBridge's code
Browse files Browse the repository at this point in the history
  • Loading branch information
wwwcg committed Jun 13, 2024
1 parent 76ca98f commit e8047bb
Show file tree
Hide file tree
Showing 2 changed files with 115 additions and 117 deletions.
211 changes: 108 additions & 103 deletions framework/ios/base/bridge/HippyBridge.h
Original file line number Diff line number Diff line change
Expand Up @@ -147,14 +147,6 @@ HIPPY_EXTERN NSString *HippyBridgeModuleNameForClass(Class bridgeModuleClass);
/// Async bridge used to communicate with the JavaScript application.
@interface HippyBridge : NSObject <HippyInvalidating>

/// The bridge delegate
@property (nonatomic, weak, readonly) id<HippyBridgeDelegate> delegate;

/// SDK launch config
/// TODO: 优化 launchOptions 参数
@property (nonatomic, copy, readonly) NSDictionary *launchOptions;


/// Create A HippyBridge instance, without load/execute any js bundle.
///
/// @param delegate bridge delegate
Expand Down Expand Up @@ -188,32 +180,51 @@ HIPPY_EXTERN NSString *HippyBridgeModuleNameForClass(Class bridgeModuleClass);
launchOptions:(nullable NSDictionary *)launchOptions
executorKey:(nullable NSString *)executorKey;

/**
* Context name for HippyBridge
*
* @discussion Context name will be shown on safari development menu.
* only for JSC engine
*/
@property (nonatomic, copy) NSString *contextName;
/// The delegate of bridge
@property (nonatomic, weak, readonly) id<HippyBridgeDelegate> delegate;

/**
* Set module name
*
*@discussion module name will show in error infomation
*/
/// SDK launch config
/// TODO: optimizes the launchOptions parameter
@property (nonatomic, copy, readonly) NSDictionary *launchOptions;

/// Module name
///
/// @discussion
/// Module name is the only Key used to identify the bridge instance,
/// It must be set and cannot be nil.
@property (nonatomic, strong) NSString *moduleName;

/**
* URLs of the script that was loaded into the bridge.
*/
/// Context name for HippyBridge
///
/// @discussion
/// Context name will be shown on safari development menu. Only for JSC engine.
/// By default, moduleName is the contextName.
@property (nonatomic, copy) NSString *contextName;

/// Use this to check if the bridge has been invalidated.
@property (nonatomic, readonly, getter=isValid) BOOL valid;

/// Reason for bridge invalidate state
@property (nonatomic, assign) HippyInvalidateReason invalidateReason;

/// Whether the bridge is loading bundle
@property (nonatomic, readonly, getter=isLoading) BOOL loading;

/// All loaded bundle urls
@property (nonatomic, copy, readonly) NSArray<NSURL *> *bundleURLs;

/**
* Set debug url for devtools
*/
@property (nonatomic, strong, readonly) NSURL *debugURL;
/// Path of sandbox directory
@property (nonatomic, strong) NSURL *sandboxDirectory;

/// Shared data between different rootViews on same bridge.
/// Set by HippyRootView when runHippyApplication.
/// Reserved for compatible with hippy2.
///
/// Note: Deprecated property.
@property (nonatomic, strong) NSMutableDictionary<NSNumber *, NSDictionary *> *shareOptions;

/// Get Device Info
- (NSDictionary *)deviceInfo;

#pragma mark - Image Related

Expand All @@ -240,26 +251,38 @@ HIPPY_EXTERN NSString *HippyBridgeModuleNameForClass(Class bridgeModuleClass);
- (void)addImageProviderClass:(Class<HippyImageProviderProtocol>)cls;


#pragma mark -
#pragma mark - Lifecycle Related API

/**
* Load instance for root view and show views
* @param rootTag RootTag for specific root view
* @param props Initial parameters for instance.
*/
- (void)loadInstanceForRootView:(NSNumber *)rootTag withProperties:(NSDictionary *)props;
/// Register RootView
///
/// Internally, will create a dom root node and bind to the root view,
/// and connect all parts together, prepare for `loadInstance`.
/// - Parameter rootView: A view instance
- (void)setRootView:(UIView *)rootView;

/// Load instance for hippy root view and show views
/// This is the Entry of Hippy Application
/// - Parameters:
/// - rootTag: tag of rootView
/// - props: props(appProperties) for hippy frontend application
- (void)loadInstanceForRootView:(NSNumber *)rootTag withProperties:(nullable NSDictionary *)props;

/// Unload the instance
/// - Parameter rootTag: tag of rootView
- (void)unloadInstanceForRootView:(NSNumber *)rootTag;

- (void)rootViewSizeChangedEvent:(NSNumber *)tag params:(NSDictionary *)params;
/// Reload the bundle and reset executor & modules.
/// Safe to call from any thread.
/// Internally sends `HippyReloadNotification` Notification.
- (void)requestReload;


/**
* Access the underlying JavaScript executor. You can use this in unit tests to detect
* when the executor has been invalidated, or when you want to schedule calls on the
* JS VM outside of Hippy Native. Use with care!
*/
@property (nonatomic, readonly) HippyJSExecutor *javaScriptExecutor;
#pragma mark -

/// Access the underlying JavaScript executor.
/// You can use this in unit tests to detect when the executor has been invalidated,
/// or when you want to schedule calls on the JS VM outside of Hippy Native. Use with care!
@property (nonatomic, readonly) HippyJSExecutor *javaScriptExecutor;

/**
* JS invocation methods
Expand All @@ -273,19 +296,24 @@ HIPPY_EXTERN NSString *HippyBridgeModuleNameForClass(Class bridgeModuleClass);

- (void)registerModuleForFrameUpdates:(id<HippyBridgeModule>)module withModuleData:(HippyModuleData *)moduleData;

/// Handle msg(buffer) from JS side
/// - Parameters:
/// - buffer: id
/// - batchEnded: whether is batch end
- (void)handleBuffer:(id _Nullable)buffer batchEnded:(BOOL)batchEnded;

/// Send native event to JS side
/// - Parameters:
/// - eventName: event name
/// - params: event info
- (void)sendEvent:(NSString *)eventName params:(NSDictionary *_Nullable)params;

#pragma mark - Inspector Related Functions

/// Sets whether the context is inspectable in Web Inspector.
/// Default value is NO.
///
/// - Parameter isInspectable: BOOL
- (void)setInspectable:(BOOL)isInspectable;

#pragma mark - Module Management

#pragma mark -
/// Whether is turboModule enabled
/// default is YES
@property (nonatomic, assign) BOOL enableTurbo;

/// All registered bridge module classes.
@property (nonatomic, copy, readonly) NSArray<Class> *moduleClasses;
Expand Down Expand Up @@ -322,72 +350,19 @@ HIPPY_EXTERN NSString *HippyBridgeModuleNameForClass(Class bridgeModuleClass);
*/
- (BOOL)moduleIsInitialized:(Class)moduleClass;

/** A red box will show when error occurs by default
* only work on HIPPY_DEBUG mode
*/
- (void)setRedBoxShowEnabled:(BOOL)enabled;

/**
* Use this to check if the bridge has been invalidated.
*/
@property (nonatomic, readonly, getter=isValid) BOOL valid;

@property (nonatomic, readonly, getter=isLoading) BOOL loading;

/**
* Reload the bundle and reset executor & modules. Safe to call from any thread.
*/
- (void)reload;

/**
* Inform the bridge, and anything subscribing to it, that it should reload.
*/
- (void)requestReload;

@property (nonatomic, assign) BOOL debugMode;

@property (nonatomic, strong) NSString *appVerson;

@property (nonatomic, assign) HippyInvalidateReason invalidateReason;

@property (nonatomic, weak) id<HippyMethodInterceptorProtocol> methodInterceptor;

@property (nonatomic, assign) BOOL enableTurbo;

/// Shared data between different rootViews on same bridge.
/// Set by HippyRootView when runHippyApplication.
/// Reserved for compatible with hippy2.
///
/// Note: Deprecated property.
@property (nonatomic, strong) NSMutableDictionary<NSNumber *, NSDictionary *> *shareOptions;

/**
* Get the turbo module for a given name.
*/
- (id)turboModuleWithName:(NSString *)name;

- (NSDictionary *)deviceInfo;

/**
* property to path of sandbox directory
*/
@property (nonatomic, strong) NSURL *sandboxDirectory;

#pragma mark event dispatcher
- (void)sendEvent:(NSString *)eventName params:(NSDictionary *_Nullable)params;
#pragma mark - Snapshot

#pragma mark snapshot
- (NSData *)snapShotData;

- (void)setSnapShotData:(NSData *)data;



- (void)setRootView:(UIView *)rootView;

- (void)resetRootSize:(CGSize)size;


#pragma mark - App UI State Related

/// NightMode or not, default is NO.
Expand All @@ -399,6 +374,32 @@ HIPPY_EXTERN NSString *HippyBridgeModuleNameForClass(Class bridgeModuleClass);
/// - Parameter rootViewTag: rootView's hippyTag
- (void)setOSNightMode:(BOOL)isOSNightMode withRootViewTag:(NSNumber *)rootViewTag;

/// Update the size of RootView
/// - Parameter size: CGSize
- (void)resetRootSize:(CGSize)size;


#pragma mark - Debug Related

/// Whether is in debug mode
/// debug mode will open DevMenu and make JSC inspectable
@property (nonatomic, assign) BOOL debugMode;

/// Debug URL for devtools
/// TODO: debugURL not working ?
@property (nonatomic, strong, readonly) NSURL *debugURL;

/// Sets whether the context is inspectable in Web Inspector.
/// Default value is NO.
///
/// - Parameter isInspectable: BOOL
- (void)setInspectable:(BOOL)isInspectable;

/// A red box will show when error occurs by default
/// only work on HIPPY_DEBUG mode
///
/// - Parameter enabled: BOOL
- (void)setRedBoxShowEnabled:(BOOL)enabled;


#pragma mark - Advanced Usages
Expand All @@ -408,6 +409,10 @@ HIPPY_EXTERN NSString *HippyBridgeModuleNameForClass(Class bridgeModuleClass);
* Following methods are only used for advanced customization, no need to be invoked in general.
*/

/// Interceptor for methods
@property (nonatomic, weak) id<HippyMethodInterceptorProtocol> methodInterceptor;


typedef NSUInteger HippyBridgeBundleType;
typedef void (^HippyBridgeBundleLoadCompletionBlock)(NSURL * _Nullable bundleURL, NSError * _Nullable error);

Expand Down
21 changes: 7 additions & 14 deletions framework/ios/base/bridge/HippyBridge.mm
Original file line number Diff line number Diff line change
Expand Up @@ -446,24 +446,17 @@ - (void)addImageProviderClass:(Class<HippyImageProviderProtocol>)cls {
}
}

#pragma mark - Debug Reload
#pragma mark - Reload

- (void)reload {
- (void)requestReload {
[[NSNotificationCenter defaultCenter] postNotificationName:HippyReloadNotification object:nil];
dispatch_async(dispatch_get_main_queue(), ^{
self.invalidateReason = HippyInvalidateReasonReload;
[self invalidate];
[self setUp];
});
}

- (void)requestReload {
if (_debugMode) {
[[NSNotificationCenter defaultCenter] postNotificationName:HippyReloadNotification object:nil];
[self reload];
}
}


#pragma mark - Bridge SetUp

- (void)setUp {
Expand Down Expand Up @@ -680,8 +673,8 @@ - (void)unloadInstanceForRootView:(NSNumber *)rootTag {
NSDictionary *param = @{@"id": rootTag};
footstone::value::HippyValue value = [param toHippyValue];
std::shared_ptr<footstone::value::HippyValue> domValue = std::make_shared<footstone::value::HippyValue>(value);
if (self.javaScriptExecutor) {
self.javaScriptExecutor.pScope->UnloadInstance(domValue);
if (auto scope = self.javaScriptExecutor.pScope) {
scope->UnloadInstance(domValue);
}
_renderManager->UnregisterRootView([rootTag intValue]);
if (_rootNode) {
Expand Down Expand Up @@ -709,7 +702,7 @@ - (void)innerLoadInstanceForRootView:(NSNumber *)rootTag withProperties:(NSDicti
HippyLogInfo(@"[HP PERF] End loading instance for HippyBridge(%p)", self);
}

- (void)rootViewSizeChangedEvent:(NSNumber *)tag params:(NSDictionary *)params {
- (void)sendRootSizeChangedEvent:(NSNumber *)tag params:(NSDictionary *)params {
NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithDictionary:params];
[dic setObject:tag forKey:@"rootViewId"];
[self sendEvent:@"onSizeChanged" params:dic];
Expand Down Expand Up @@ -1351,7 +1344,7 @@ - (void)setRootView:(UIView *)rootView {
auto cb = [weakBridge](int32_t tag, NSDictionary *params){
HippyBridge *strongBridge = weakBridge;
if (strongBridge) {
[strongBridge rootViewSizeChangedEvent:@(tag) params:params];
[strongBridge sendRootSizeChangedEvent:@(tag) params:params];
}
};
_renderManager->SetRootViewSizeChangedEvent(cb);
Expand Down

0 comments on commit e8047bb

Please sign in to comment.