Skip to content

Commit e8047bb

Browse files
committed
refactor(ios): clean and refactor the HippyBridge's code
1 parent 76ca98f commit e8047bb

File tree

2 files changed

+115
-117
lines changed

2 files changed

+115
-117
lines changed

framework/ios/base/bridge/HippyBridge.h

Lines changed: 108 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -147,14 +147,6 @@ HIPPY_EXTERN NSString *HippyBridgeModuleNameForClass(Class bridgeModuleClass);
147147
/// Async bridge used to communicate with the JavaScript application.
148148
@interface HippyBridge : NSObject <HippyInvalidating>
149149

150-
/// The bridge delegate
151-
@property (nonatomic, weak, readonly) id<HippyBridgeDelegate> delegate;
152-
153-
/// SDK launch config
154-
/// TODO: 优化 launchOptions 参数
155-
@property (nonatomic, copy, readonly) NSDictionary *launchOptions;
156-
157-
158150
/// Create A HippyBridge instance, without load/execute any js bundle.
159151
///
160152
/// @param delegate bridge delegate
@@ -188,32 +180,51 @@ HIPPY_EXTERN NSString *HippyBridgeModuleNameForClass(Class bridgeModuleClass);
188180
launchOptions:(nullable NSDictionary *)launchOptions
189181
executorKey:(nullable NSString *)executorKey;
190182

191-
/**
192-
* Context name for HippyBridge
193-
*
194-
* @discussion Context name will be shown on safari development menu.
195-
* only for JSC engine
196-
*/
197-
@property (nonatomic, copy) NSString *contextName;
183+
/// The delegate of bridge
184+
@property (nonatomic, weak, readonly) id<HippyBridgeDelegate> delegate;
198185

199-
/**
200-
* Set module name
201-
*
202-
*@discussion module name will show in error infomation
203-
*/
186+
/// SDK launch config
187+
/// TODO: optimizes the launchOptions parameter
188+
@property (nonatomic, copy, readonly) NSDictionary *launchOptions;
189+
190+
/// Module name
191+
///
192+
/// @discussion
193+
/// Module name is the only Key used to identify the bridge instance,
194+
/// It must be set and cannot be nil.
204195
@property (nonatomic, strong) NSString *moduleName;
205196

206-
/**
207-
* URLs of the script that was loaded into the bridge.
208-
*/
197+
/// Context name for HippyBridge
198+
///
199+
/// @discussion
200+
/// Context name will be shown on safari development menu. Only for JSC engine.
201+
/// By default, moduleName is the contextName.
202+
@property (nonatomic, copy) NSString *contextName;
203+
204+
/// Use this to check if the bridge has been invalidated.
205+
@property (nonatomic, readonly, getter=isValid) BOOL valid;
206+
207+
/// Reason for bridge invalidate state
208+
@property (nonatomic, assign) HippyInvalidateReason invalidateReason;
209+
210+
/// Whether the bridge is loading bundle
211+
@property (nonatomic, readonly, getter=isLoading) BOOL loading;
212+
213+
/// All loaded bundle urls
209214
@property (nonatomic, copy, readonly) NSArray<NSURL *> *bundleURLs;
210215

211-
/**
212-
* Set debug url for devtools
213-
*/
214-
@property (nonatomic, strong, readonly) NSURL *debugURL;
216+
/// Path of sandbox directory
217+
@property (nonatomic, strong) NSURL *sandboxDirectory;
215218

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

226+
/// Get Device Info
227+
- (NSDictionary *)deviceInfo;
217228

218229
#pragma mark - Image Related
219230

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

242253

243-
#pragma mark -
254+
#pragma mark - Lifecycle Related API
244255

245-
/**
246-
* Load instance for root view and show views
247-
* @param rootTag RootTag for specific root view
248-
* @param props Initial parameters for instance.
249-
*/
250-
- (void)loadInstanceForRootView:(NSNumber *)rootTag withProperties:(NSDictionary *)props;
256+
/// Register RootView
257+
///
258+
/// Internally, will create a dom root node and bind to the root view,
259+
/// and connect all parts together, prepare for `loadInstance`.
260+
/// - Parameter rootView: A view instance
261+
- (void)setRootView:(UIView *)rootView;
262+
263+
/// Load instance for hippy root view and show views
264+
/// This is the Entry of Hippy Application
265+
/// - Parameters:
266+
/// - rootTag: tag of rootView
267+
/// - props: props(appProperties) for hippy frontend application
268+
- (void)loadInstanceForRootView:(NSNumber *)rootTag withProperties:(nullable NSDictionary *)props;
251269

270+
/// Unload the instance
271+
/// - Parameter rootTag: tag of rootView
252272
- (void)unloadInstanceForRootView:(NSNumber *)rootTag;
253273

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

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

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

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

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

299+
/// Handle msg(buffer) from JS side
300+
/// - Parameters:
301+
/// - buffer: id
302+
/// - batchEnded: whether is batch end
276303
- (void)handleBuffer:(id _Nullable)buffer batchEnded:(BOOL)batchEnded;
277304

305+
/// Send native event to JS side
306+
/// - Parameters:
307+
/// - eventName: event name
308+
/// - params: event info
309+
- (void)sendEvent:(NSString *)eventName params:(NSDictionary *_Nullable)params;
278310

279-
#pragma mark - Inspector Related Functions
280-
281-
/// Sets whether the context is inspectable in Web Inspector.
282-
/// Default value is NO.
283-
///
284-
/// - Parameter isInspectable: BOOL
285-
- (void)setInspectable:(BOOL)isInspectable;
286311

312+
#pragma mark - Module Management
287313

288-
#pragma mark -
314+
/// Whether is turboModule enabled
315+
/// default is YES
316+
@property (nonatomic, assign) BOOL enableTurbo;
289317

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

325-
/** A red box will show when error occurs by default
326-
* only work on HIPPY_DEBUG mode
327-
*/
328-
- (void)setRedBoxShowEnabled:(BOOL)enabled;
329-
330-
/**
331-
* Use this to check if the bridge has been invalidated.
332-
*/
333-
@property (nonatomic, readonly, getter=isValid) BOOL valid;
334-
335-
@property (nonatomic, readonly, getter=isLoading) BOOL loading;
336-
337-
/**
338-
* Reload the bundle and reset executor & modules. Safe to call from any thread.
339-
*/
340-
- (void)reload;
341-
342-
/**
343-
* Inform the bridge, and anything subscribing to it, that it should reload.
344-
*/
345-
- (void)requestReload;
346-
347-
@property (nonatomic, assign) BOOL debugMode;
348-
349-
@property (nonatomic, strong) NSString *appVerson;
350-
351-
@property (nonatomic, assign) HippyInvalidateReason invalidateReason;
352-
353-
@property (nonatomic, weak) id<HippyMethodInterceptorProtocol> methodInterceptor;
354-
355-
@property (nonatomic, assign) BOOL enableTurbo;
356-
357-
/// Shared data between different rootViews on same bridge.
358-
/// Set by HippyRootView when runHippyApplication.
359-
/// Reserved for compatible with hippy2.
360-
///
361-
/// Note: Deprecated property.
362-
@property (nonatomic, strong) NSMutableDictionary<NSNumber *, NSDictionary *> *shareOptions;
363-
364353
/**
365354
* Get the turbo module for a given name.
366355
*/
367356
- (id)turboModuleWithName:(NSString *)name;
368357

369-
- (NSDictionary *)deviceInfo;
370-
371-
/**
372-
* property to path of sandbox directory
373-
*/
374-
@property (nonatomic, strong) NSURL *sandboxDirectory;
375358

376-
#pragma mark event dispatcher
377-
- (void)sendEvent:(NSString *)eventName params:(NSDictionary *_Nullable)params;
359+
#pragma mark - Snapshot
378360

379-
#pragma mark snapshot
380361
- (NSData *)snapShotData;
381362

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

384365

385-
386-
- (void)setRootView:(UIView *)rootView;
387-
388-
- (void)resetRootSize:(CGSize)size;
389-
390-
391366
#pragma mark - App UI State Related
392367

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

377+
/// Update the size of RootView
378+
/// - Parameter size: CGSize
379+
- (void)resetRootSize:(CGSize)size;
380+
381+
382+
#pragma mark - Debug Related
383+
384+
/// Whether is in debug mode
385+
/// debug mode will open DevMenu and make JSC inspectable
386+
@property (nonatomic, assign) BOOL debugMode;
387+
388+
/// Debug URL for devtools
389+
/// TODO: debugURL not working ?
390+
@property (nonatomic, strong, readonly) NSURL *debugURL;
391+
392+
/// Sets whether the context is inspectable in Web Inspector.
393+
/// Default value is NO.
394+
///
395+
/// - Parameter isInspectable: BOOL
396+
- (void)setInspectable:(BOOL)isInspectable;
397+
398+
/// A red box will show when error occurs by default
399+
/// only work on HIPPY_DEBUG mode
400+
///
401+
/// - Parameter enabled: BOOL
402+
- (void)setRedBoxShowEnabled:(BOOL)enabled;
402403

403404

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

412+
/// Interceptor for methods
413+
@property (nonatomic, weak) id<HippyMethodInterceptorProtocol> methodInterceptor;
414+
415+
411416
typedef NSUInteger HippyBridgeBundleType;
412417
typedef void (^HippyBridgeBundleLoadCompletionBlock)(NSURL * _Nullable bundleURL, NSError * _Nullable error);
413418

framework/ios/base/bridge/HippyBridge.mm

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -446,24 +446,17 @@ - (void)addImageProviderClass:(Class<HippyImageProviderProtocol>)cls {
446446
}
447447
}
448448

449-
#pragma mark - Debug Reload
449+
#pragma mark - Reload
450450

451-
- (void)reload {
451+
- (void)requestReload {
452+
[[NSNotificationCenter defaultCenter] postNotificationName:HippyReloadNotification object:nil];
452453
dispatch_async(dispatch_get_main_queue(), ^{
453454
self.invalidateReason = HippyInvalidateReasonReload;
454455
[self invalidate];
455456
[self setUp];
456457
});
457458
}
458459

459-
- (void)requestReload {
460-
if (_debugMode) {
461-
[[NSNotificationCenter defaultCenter] postNotificationName:HippyReloadNotification object:nil];
462-
[self reload];
463-
}
464-
}
465-
466-
467460
#pragma mark - Bridge SetUp
468461

469462
- (void)setUp {
@@ -680,8 +673,8 @@ - (void)unloadInstanceForRootView:(NSNumber *)rootTag {
680673
NSDictionary *param = @{@"id": rootTag};
681674
footstone::value::HippyValue value = [param toHippyValue];
682675
std::shared_ptr<footstone::value::HippyValue> domValue = std::make_shared<footstone::value::HippyValue>(value);
683-
if (self.javaScriptExecutor) {
684-
self.javaScriptExecutor.pScope->UnloadInstance(domValue);
676+
if (auto scope = self.javaScriptExecutor.pScope) {
677+
scope->UnloadInstance(domValue);
685678
}
686679
_renderManager->UnregisterRootView([rootTag intValue]);
687680
if (_rootNode) {
@@ -709,7 +702,7 @@ - (void)innerLoadInstanceForRootView:(NSNumber *)rootTag withProperties:(NSDicti
709702
HippyLogInfo(@"[HP PERF] End loading instance for HippyBridge(%p)", self);
710703
}
711704

712-
- (void)rootViewSizeChangedEvent:(NSNumber *)tag params:(NSDictionary *)params {
705+
- (void)sendRootSizeChangedEvent:(NSNumber *)tag params:(NSDictionary *)params {
713706
NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithDictionary:params];
714707
[dic setObject:tag forKey:@"rootViewId"];
715708
[self sendEvent:@"onSizeChanged" params:dic];
@@ -1351,7 +1344,7 @@ - (void)setRootView:(UIView *)rootView {
13511344
auto cb = [weakBridge](int32_t tag, NSDictionary *params){
13521345
HippyBridge *strongBridge = weakBridge;
13531346
if (strongBridge) {
1354-
[strongBridge rootViewSizeChangedEvent:@(tag) params:params];
1347+
[strongBridge sendRootSizeChangedEvent:@(tag) params:params];
13551348
}
13561349
};
13571350
_renderManager->SetRootViewSizeChangedEvent(cb);

0 commit comments

Comments
 (0)