diff --git a/framework/ios/base/bridge/HippyBridge.h b/framework/ios/base/bridge/HippyBridge.h index d5697ad44db..36402a6168b 100644 --- a/framework/ios/base/bridge/HippyBridge.h +++ b/framework/ios/base/bridge/HippyBridge.h @@ -147,14 +147,6 @@ HIPPY_EXTERN NSString *HippyBridgeModuleNameForClass(Class bridgeModuleClass); /// Async bridge used to communicate with the JavaScript application. @interface HippyBridge : NSObject -/// The bridge delegate -@property (nonatomic, weak, readonly) id 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 @@ -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 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 *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 *shareOptions; +/// Get Device Info +- (NSDictionary *)deviceInfo; #pragma mark - Image Related @@ -240,26 +251,38 @@ HIPPY_EXTERN NSString *HippyBridgeModuleNameForClass(Class bridgeModuleClass); - (void)addImageProviderClass:(Class)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 @@ -273,19 +296,24 @@ HIPPY_EXTERN NSString *HippyBridgeModuleNameForClass(Class bridgeModuleClass); - (void)registerModuleForFrameUpdates:(id)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 *moduleClasses; @@ -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 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 *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. @@ -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 @@ -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 methodInterceptor; + + typedef NSUInteger HippyBridgeBundleType; typedef void (^HippyBridgeBundleLoadCompletionBlock)(NSURL * _Nullable bundleURL, NSError * _Nullable error); diff --git a/framework/ios/base/bridge/HippyBridge.mm b/framework/ios/base/bridge/HippyBridge.mm index 78fdf1948bd..cc8453289cb 100644 --- a/framework/ios/base/bridge/HippyBridge.mm +++ b/framework/ios/base/bridge/HippyBridge.mm @@ -446,9 +446,10 @@ - (void)addImageProviderClass:(Class)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]; @@ -456,14 +457,6 @@ - (void)reload { }); } -- (void)requestReload { - if (_debugMode) { - [[NSNotificationCenter defaultCenter] postNotificationName:HippyReloadNotification object:nil]; - [self reload]; - } -} - - #pragma mark - Bridge SetUp - (void)setUp { @@ -680,8 +673,8 @@ - (void)unloadInstanceForRootView:(NSNumber *)rootTag { NSDictionary *param = @{@"id": rootTag}; footstone::value::HippyValue value = [param toHippyValue]; std::shared_ptr domValue = std::make_shared(value); - if (self.javaScriptExecutor) { - self.javaScriptExecutor.pScope->UnloadInstance(domValue); + if (auto scope = self.javaScriptExecutor.pScope) { + scope->UnloadInstance(domValue); } _renderManager->UnregisterRootView([rootTag intValue]); if (_rootNode) { @@ -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]; @@ -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);