Skip to content

Commit

Permalink
Allow to add interface state delegate in background. (TextureGroup#1090)
Browse files Browse the repository at this point in the history
* fix SIMULATE_WEB_RESPONSE not imported TextureGroup#449

* Fix to make rangeMode update in right time

* remove uncessary assert

* Allow to add interface state delegate in background threads.

* Allow to add interface state delegate in background threads.

* lock around _interfaceStateDelegates

* lock _interfaceStateDelegates to local variable

* Fix comments

* remove extra spaces
  • Loading branch information
wsdwsd0829 authored and mikezucc committed Oct 2, 2018
1 parent 8f890a1 commit 90e97d9
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 52 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
## master
* Add your own contributions to the next release on the line below this with your name.
- [ASDisplayNode] Allow add/remove interface state delegates on background thread. [Max Wang](https://github.com/wsdwsd0829). [#1090](https://github.com/TextureGroup/Texture/pull/1090)
- [License] Simplify the Texture license to be pure Apache 2 (removing ASDK-Licenses).[Scott Goodson](https://github.com/appleguy) [#1077](https://github.com/TextureGroup/Texture/pull/1077)
- [ASNetworkImageNode] Allow delegate methods to be called on background thread. [Max Wang](https://github.com/wsdwsd0829). [#1007](https://github.com/TextureGroup/Texture/pull/1007)
- [ASLayoutTransition] Add support for preserving order after node moves during transitions. (This order defines the z-order as well.) [Kevin Smith](https://github.com/wiseoldduck) [#1006]
Expand Down
77 changes: 25 additions & 52 deletions Source/ASDisplayNode.mm
Original file line number Diff line number Diff line change
Expand Up @@ -452,6 +452,16 @@ - (void)dealloc

#pragma mark - Loading

#define ASDisplayNodeCallInterfaceStateDelegates(method) \
__instanceLock__.lock(); \
NSHashTable *delegates = [_interfaceStateDelegates copy]; \
__instanceLock__.unlock(); \
for (id <ASInterfaceStateDelegate> delegate in delegates) { \
if ([delegate respondsToSelector:@selector(method)]) { \
[delegate method]; \
} \
}

- (BOOL)_locked_shouldLoadViewOrLayer
{
ASAssertLocked(__instanceLock__);
Expand Down Expand Up @@ -564,12 +574,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);
}

- (void)didLoad
Expand Down Expand Up @@ -1274,11 +1279,7 @@ - (void)layout
ASDisplayNodeAssertMainThread();
ASAssertUnlocked(__instanceLock__);
ASDisplayNodeAssertTrue(self.isNodeLoaded);
for (id <ASInterfaceStateDelegate> delegate in _interfaceStateDelegates) {
if ([delegate respondsToSelector:@selector(nodeDidLayout)]) {
[delegate nodeDidLayout];
}
}
ASDisplayNodeCallInterfaceStateDelegates(nodeDidLayout);
}

#pragma mark Layout Transition
Expand Down Expand Up @@ -1501,12 +1502,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);

BOOL placeholderShouldPersist = [self placeholderShouldPersist];

Expand Down Expand Up @@ -3217,7 +3213,10 @@ - (void)interfaceStateDidChange:(ASInterfaceState)newState fromState:(ASInterfac
// Subclass hook
ASAssertUnlocked(__instanceLock__);
ASDisplayNodeAssertMainThread();
for (id <ASInterfaceStateDelegate> delegate in _interfaceStateDelegates) {
__instanceLock__.lock();
NSHashTable *delegates = [_interfaceStateDelegates copy];
__instanceLock__.unlock();
for (id <ASInterfaceStateDelegate> delegate in delegates) {
if ([delegate respondsToSelector:@selector(interfaceStateDidChange:fromState:)]) {
[delegate interfaceStateDidChange:newState fromState:oldState];
}
Expand All @@ -3233,8 +3232,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 @@ -3245,7 +3243,7 @@ - (void)addInterfaceStateDelegate:(id <ASInterfaceStateDelegate>)interfaceStateD

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

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

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

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

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

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

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

- (void)clearContents
Expand Down

0 comments on commit 90e97d9

Please sign in to comment.