-
Notifications
You must be signed in to change notification settings - Fork 580
Redo how debug "reflow" works #691
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -12,6 +12,8 @@ | |
|
|
||
| #import <UIKit/UIKit.h> | ||
|
|
||
| #import <mutex> | ||
|
|
||
| #import "CKComponent.h" | ||
| #import "CKComponentAnimation.h" | ||
| #import "CKComponentHostingView.h" | ||
|
|
@@ -109,9 +111,6 @@ + (BOOL)debugMode | |
| return context; // no need for a debug view if the component has a view. | ||
| } | ||
|
|
||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. unnecessary; mount is only performed on the main thread.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we want to assert this is called on the main thread? Or do you think that's overkill in this case?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it's overkill in this particular case. |
||
| static CK::StaticMutex l = CK_MUTEX_INITIALIZER; | ||
| CK::StaticMutexLocker lock(l); | ||
|
|
||
| // Avoid the static destructor fiasco, use a pointer: | ||
| static std::unordered_map<Class, CKComponentViewConfiguration> *debugViewConfigurations = | ||
| new std::unordered_map<Class, CKComponentViewConfiguration>(); | ||
|
|
@@ -133,46 +132,35 @@ + (BOOL)debugMode | |
|
|
||
| #pragma mark - Synchronous Reflow | ||
|
|
||
| static NSHashTable<id<CKComponentDebugReflowListener>> *reflowListeners; | ||
| static std::mutex reflowMutex; | ||
|
|
||
| + (void)reflowComponents | ||
| { | ||
| if (![NSThread isMainThread]) { | ||
| dispatch_async(dispatch_get_main_queue(), ^{ [self reflowComponents]; }); | ||
| } else { | ||
| UIWindow *window = [[UIApplication sharedApplication] keyWindow]; | ||
| CKRecursiveComponentReflow(window); | ||
| return; | ||
| } | ||
| } | ||
|
|
||
| + (void)reflowComponentsForView:(UIView *)view searchUpwards:(BOOL)upwards | ||
| { | ||
| if (upwards) { | ||
| while (view && ![view isKindOfClass:[CKComponentRootView class]]) { | ||
| view = view.superview; | ||
| } | ||
| NSArray<id<CKComponentDebugReflowListener>> *copiedListeners; | ||
| { | ||
| std::lock_guard<std::mutex> l(reflowMutex); | ||
| copiedListeners = [reflowListeners allObjects]; | ||
| } | ||
| if (view) { | ||
| CKRecursiveComponentReflow(view); | ||
| for (id<CKComponentDebugReflowListener> listener in copiedListeners) { | ||
| [listener didReceiveReflowComponentsRequest]; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is so much better than traversing the view hierarchy to find the component lifecycle manager or explicitly telling component hosting views to layout. |
||
| } | ||
| } | ||
|
|
||
| static void CKRecursiveComponentReflow(UIView *view) | ||
| + (void)registerReflowListener:(id<CKComponentDebugReflowListener>)listener | ||
| { | ||
| if (view.ck_componentLifecycleManager) { | ||
| CKComponentLifecycleManager *lifecycleManager = view.ck_componentLifecycleManager; | ||
| CKComponentLifecycleManagerState oldState = [lifecycleManager state]; | ||
| CKComponentLifecycleManagerState state = | ||
| [lifecycleManager prepareForUpdateWithModel:oldState.model | ||
| constrainedSize:oldState.constrainedSize | ||
| context:oldState.context]; | ||
| [lifecycleManager updateWithState:state]; | ||
| } else if ([view.superview isKindOfClass:[CKComponentHostingView class]]) { | ||
| CKComponentHostingView *hostingView = (CKComponentHostingView *)view.superview; | ||
| [hostingView setNeedsLayout]; | ||
| } else { | ||
| for (UIView *subview in view.subviews) { | ||
| CKRecursiveComponentReflow(subview); | ||
| } | ||
| if (listener == nil) { | ||
| return; | ||
| } | ||
| std::lock_guard<std::mutex> l(reflowMutex); | ||
| if (reflowListeners == nil) { | ||
| reflowListeners = [NSHashTable weakObjectsHashTable]; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice. So we weakly hold the reflow listeners to naturally keep the collection bounded. |
||
| } | ||
| [reflowListeners addObject:listener]; | ||
| } | ||
|
|
||
| @end | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Though it's a bit of a shame to have
CKComponentLifecycleManageraware of debug code I think it's totally worthwhile considering how much it simplifiesCKComponentDebugController.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is a bit weird, definitely. How about having a separate registry of lifecycle managers (exposed in an internal header) that the debug controller can then access? At least that would get the debug controller code out of
CKComponentLifecycleManager.