Skip to content
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

Allow to add interface state delegate in background. #1090

Merged
Changes from 26 commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
929d118
fix SIMULATE_WEB_RESPONSE not imported #449
wsdwsd0829 Jul 16, 2017
dd24d8f
Merge branch 'master' of github.com:TextureGroup/Texture
wsdwsd0829 Sep 6, 2017
b8eaffa
Merge branch 'master' of github.com:TextureGroup/Texture
wsdwsd0829 Oct 4, 2017
2918ea0
Merge branch 'master' of github.com:TextureGroup/Texture
wsdwsd0829 Oct 11, 2017
9c42266
Merge branch 'master' of github.com:TextureGroup/Texture
wsdwsd0829 Oct 12, 2017
329f35f
Fix to make rangeMode update in right time
wsdwsd0829 Feb 5, 2018
5f8b7ec
Merge branch 'master' of github.com:TextureGroup/Texture
wsdwsd0829 Feb 7, 2018
d87bb11
merge master from upstream
wsdwsd0829 Feb 15, 2018
269c2ab
Merge branch 'master' of github.com:TextureGroup/Texture
wsdwsd0829 Mar 10, 2018
24c1ce8
Merge branch 'master' of github.com:TextureGroup/Texture
wsdwsd0829 Mar 12, 2018
233169e
Merge branch 'master' of github.com:TextureGroup/Texture
wsdwsd0829 Mar 26, 2018
b19f90d
Merge branch 'master' of github.com:TextureGroup/Texture
wsdwsd0829 Mar 28, 2018
b50cec4
Merge branch 'master' of github.com:TextureGroup/Texture
wsdwsd0829 Apr 5, 2018
b75a5f3
Merge branch 'master' of github.com:TextureGroup/Texture
wsdwsd0829 May 3, 2018
64b46e0
Merge branch 'master' of github.com:TextureGroup/Texture
wsdwsd0829 May 16, 2018
5fabc1e
remove uncessary assert
wsdwsd0829 May 17, 2018
3d5b84b
Merge branch 'master' of github.com:TextureGroup/Texture
wsdwsd0829 May 21, 2018
098b978
Merge branch 'master' of github.com:TextureGroup/Texture
wsdwsd0829 Jun 8, 2018
77eefd6
Merge branch 'master' of github.com:TextureGroup/Texture
wsdwsd0829 Jun 13, 2018
0310ed7
Merge branch 'master' of github.com:TextureGroup/Texture
wsdwsd0829 Jul 3, 2018
0b886de
Merge branch 'master' of github.com:TextureGroup/Texture
wsdwsd0829 Jul 25, 2018
a63d438
Merge branch 'master' of github.com:TextureGroup/Texture
wsdwsd0829 Aug 21, 2018
24af857
Allow to add interface state delegate in background threads.
wsdwsd0829 Aug 29, 2018
49e1337
Allow to add interface state delegate in background threads.
wsdwsd0829 Aug 30, 2018
ee88e85
lock around _interfaceStateDelegates
wsdwsd0829 Aug 30, 2018
1b605f4
lock _interfaceStateDelegates to local variable
wsdwsd0829 Aug 31, 2018
dfbd03b
Fix comments
wsdwsd0829 Aug 31, 2018
ec3542a
remove extra spaces
wsdwsd0829 Aug 31, 2018
7721b95
Merge branch 'master' into add-interfacestate-delegate-in-background
wsdwsd0829 Aug 31, 2018
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 29 additions & 52 deletions Source/ASDisplayNode.mm
Original file line number Diff line number Diff line change
Expand Up @@ -460,6 +460,16 @@ - (void)dealloc

#pragma mark - Loading

#define ASDisplayNodeCallInterfaceStateDelegates(method, __lock) \
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since __lock is always the same (i.e __instanceLock__), should we just remove the param entirely?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

__lock.lock(); \
NSHashTable *delegates = _interfaceStateDelegates; \
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't we need to copy the hash table? Same for a couple of other places.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good Point. Thanks.

__lock.unlock(); \
for (id <ASInterfaceStateDelegate> delegate in delegates) { \
if ([delegate respondsToSelector:@selector(method)]) { \
[delegate method]; \
} \
}

- (BOOL)_locked_shouldLoadViewOrLayer
{
ASAssertLocked(__instanceLock__);
Expand Down Expand Up @@ -572,12 +582,7 @@ - (void)_didLoad
for (ASDisplayNodeDidLoadBlock block in onDidLoadBlocks) {
block(self);
}

for (id <ASInterfaceStateDelegate> delegate in _interfaceStateDelegates) {
if ([delegate respondsToSelector:@selector(nodeDidLoad)]) {
[delegate nodeDidLoad];
}
}
ASDisplayNodeCallInterfaceStateDelegates(nodeDidLoad, __instanceLock__);
}

- (void)didLoad
Expand Down Expand Up @@ -1278,15 +1283,12 @@ - (ASLayoutSpec *)layoutSpecThatFits:(ASSizeRange)constrainedSize

- (void)layout
{

// Hook for subclasses
ASDisplayNodeAssertMainThread();
ASAssertUnlocked(__instanceLock__);
ASDisplayNodeAssertTrue(self.isNodeLoaded);
for (id <ASInterfaceStateDelegate> delegate in _interfaceStateDelegates) {
if ([delegate respondsToSelector:@selector(nodeDidLayout)]) {
[delegate nodeDidLayout];
}
}
ASDisplayNodeCallInterfaceStateDelegates(nodeDidLayout, __instanceLock__);
}

#pragma mark Layout Transition
Expand Down Expand Up @@ -1509,12 +1511,7 @@ - (void)_pendingNodeDidDisplay:(ASDisplayNode *)node
if (_pendingDisplayNodes.isEmpty) {

[self hierarchyDisplayDidFinish];

for (id <ASInterfaceStateDelegate> delegate in _interfaceStateDelegates) {
if ([delegate respondsToSelector:@selector(hierarchyDisplayDidFinish)]) {
[delegate hierarchyDisplayDidFinish];
}
}
ASDisplayNodeCallInterfaceStateDelegates(hierarchyDisplayDidFinish, __instanceLock__);

BOOL placeholderShouldPersist = [self placeholderShouldPersist];

Expand Down Expand Up @@ -3017,6 +3014,8 @@ - (void)didExitHierarchy

#pragma mark - Interface State



/**
* We currently only set interface state on nodes in table/collection views. For other nodes, if they are
* in the hierarchy we enable all ASInterfaceState types with `ASInterfaceStateInHierarchy`, otherwise `None`.
Expand Down Expand Up @@ -3225,7 +3224,10 @@ - (void)interfaceStateDidChange:(ASInterfaceState)newState fromState:(ASInterfac
// Subclass hook
ASAssertUnlocked(__instanceLock__);
ASDisplayNodeAssertMainThread();
for (id <ASInterfaceStateDelegate> delegate in _interfaceStateDelegates) {
__instanceLock__.lock();
NSHashTable *delegates = _interfaceStateDelegates;
__instanceLock__.unlock();
for (id <ASInterfaceStateDelegate> delegate in delegates) {
if ([delegate respondsToSelector:@selector(interfaceStateDidChange:fromState:)]) {
[delegate interfaceStateDidChange:newState fromState:oldState];
}
Expand All @@ -3241,8 +3243,7 @@ - (BOOL)shouldScheduleDisplayWithNewInterfaceState:(ASInterfaceState)newInterfac

- (void)addInterfaceStateDelegate:(id <ASInterfaceStateDelegate>)interfaceStateDelegate
{
ASDisplayNodeAssertMainThread();

ASDN::MutexLocker l(__instanceLock__);
// Not a fan of lazy loading, but this method won't get called very often and avoiding
// the overhead of creating this is probably worth it.
if (_interfaceStateDelegates == nil) {
Expand All @@ -3253,7 +3254,7 @@ - (void)addInterfaceStateDelegate:(id <ASInterfaceStateDelegate>)interfaceStateD

- (void)removeInterfaceStateDelegate:(id <ASInterfaceStateDelegate>)interfaceStateDelegate
{
ASDisplayNodeAssertMainThread();
ASDN::MutexLocker l(__instanceLock__);
[_interfaceStateDelegates removeObject:interfaceStateDelegate];
}

Expand All @@ -3268,11 +3269,7 @@ - (void)didEnterVisibleState
// subclass override
ASDisplayNodeAssertMainThread();
ASAssertUnlocked(__instanceLock__);
for (id <ASInterfaceStateDelegate> delegate in _interfaceStateDelegates) {
if ([delegate respondsToSelector:@selector(didEnterVisibleState)]) {
[delegate didEnterVisibleState];
}
}
ASDisplayNodeCallInterfaceStateDelegates(didEnterVisibleState, __instanceLock__);
#if AS_ENABLE_TIPS
[ASTipsController.shared nodeDidAppear:self];
#endif
Expand All @@ -3283,11 +3280,7 @@ - (void)didExitVisibleState
// subclass override
ASDisplayNodeAssertMainThread();
ASAssertUnlocked(__instanceLock__);
for (id <ASInterfaceStateDelegate> delegate in _interfaceStateDelegates) {
if ([delegate respondsToSelector:@selector(didExitVisibleState)]) {
[delegate didExitVisibleState];
}
}
ASDisplayNodeCallInterfaceStateDelegates(didExitVisibleState, __instanceLock__);
}

- (BOOL)isInDisplayState
Expand All @@ -3301,23 +3294,15 @@ - (void)didEnterDisplayState
// subclass override
ASDisplayNodeAssertMainThread();
ASAssertUnlocked(__instanceLock__);
for (id <ASInterfaceStateDelegate> delegate in _interfaceStateDelegates) {
if ([delegate respondsToSelector:@selector(didEnterDisplayState)]) {
[delegate didEnterDisplayState];
}
}
ASDisplayNodeCallInterfaceStateDelegates(didEnterDisplayState, __instanceLock__);
}

- (void)didExitDisplayState
{
// subclass override
ASDisplayNodeAssertMainThread();
ASAssertUnlocked(__instanceLock__);
for (id <ASInterfaceStateDelegate> delegate in _interfaceStateDelegates) {
if ([delegate respondsToSelector:@selector(didExitDisplayState)]) {
[delegate didExitDisplayState];
}
}
ASDisplayNodeCallInterfaceStateDelegates(didExitDisplayState, __instanceLock__);
}

- (BOOL)isInPreloadState
Expand Down Expand Up @@ -3367,23 +3352,15 @@ - (void)didEnterPreloadState
if (self.automaticallyManagesSubnodes) {
[self layoutIfNeeded];
}

for (id <ASInterfaceStateDelegate> delegate in _interfaceStateDelegates) {
if ([delegate respondsToSelector:@selector(didEnterPreloadState)]) {
[delegate didEnterPreloadState];
}
}
ASDisplayNodeCallInterfaceStateDelegates(didEnterPreloadState, __instanceLock__);
}

- (void)didExitPreloadState
{
ASDisplayNodeAssertMainThread();
ASAssertUnlocked(__instanceLock__);
for (id <ASInterfaceStateDelegate> delegate in _interfaceStateDelegates) {
if ([delegate respondsToSelector:@selector(didExitPreloadState)]) {
[delegate didExitPreloadState];
}
}
ASDisplayNodeCallInterfaceStateDelegates(didExitPreloadState, __instanceLock__);

}

- (void)clearContents
Expand Down