Skip to content

Commit

Permalink
Encapsulate common logics into methods
Browse files Browse the repository at this point in the history
  • Loading branch information
nguyenhuy committed Mar 7, 2019
1 parent 3848174 commit 6a1f368
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 136 deletions.
99 changes: 28 additions & 71 deletions Source/ASMultiplexImageNode.mm
Original file line number Diff line number Diff line change
Expand Up @@ -279,92 +279,31 @@ - (BOOL)placeholderShouldPersist
- (void)displayWillStartAsynchronously:(BOOL)asynchronously
{
[super displayWillStartAsynchronously:asynchronously];

[self didEnterPreloadState];

if (_downloaderFlags.downloaderImplementsSetPriority) {
id downloadIdentifier;
{
ASDN::MutexLocker l(_downloadIdentifierLock);
downloadIdentifier = _downloadIdentifier;
}

if (downloadIdentifier != nil) {
ASImageDownloaderPriority priority = ASImageDownloaderPriorityImminent;
if (_isInImageDownloaderPriorityExperiment) {
priority = ASImageDownloaderPriorityWithInterfaceState(self.interfaceState);
}

[_downloader setPriority:priority withDownloadIdentifier:downloadIdentifier];
}
}
[self _updatePriorityOnDownloaderIfNeededWithDefaultPriority:ASImageDownloaderPriorityImminent];
}

/* didEnterVisibleState / didExitVisibleState in ASNetworkImageNode has a very similar implementation. Changes here are likely necessary
in ASNetworkImageNode as well. */
- (void)didEnterVisibleState
{
[super didEnterVisibleState];

if (_downloaderFlags.downloaderImplementsSetPriority) {
id downloadIdentifier;
{
ASDN::MutexLocker l(_downloadIdentifierLock);
downloadIdentifier = _downloadIdentifier;
}

if (downloadIdentifier != nil) {
ASImageDownloaderPriority priority = ASImageDownloaderPriorityVisible;
if (_isInImageDownloaderPriorityExperiment) {
priority = ASImageDownloaderPriorityWithInterfaceState(self.interfaceState);
}

[_downloader setPriority:priority withDownloadIdentifier:downloadIdentifier];
}
}

[self _updatePriorityOnDownloaderIfNeededWithDefaultPriority:ASImageDownloaderPriorityVisible];
[self _updateProgressImageBlockOnDownloaderIfNeeded];
}

- (void)didExitVisibleState
{
[super didExitVisibleState];

if (_downloaderFlags.downloaderImplementsSetPriority) {
id downloadIdentifier;
{
ASDN::MutexLocker l(_downloadIdentifierLock);
downloadIdentifier = _downloadIdentifier;
}

if (downloadIdentifier != nil) {
ASImageDownloaderPriority priority = ASImageDownloaderPriorityPreload;
if (_isInImageDownloaderPriorityExperiment) {
priority = ASImageDownloaderPriorityWithInterfaceState(self.interfaceState);
}

[_downloader setPriority:priority withDownloadIdentifier:downloadIdentifier];
}
}

[self _updatePriorityOnDownloaderIfNeededWithDefaultPriority:ASImageDownloaderPriorityPreload];
[self _updateProgressImageBlockOnDownloaderIfNeeded];
}

- (void)didExitDisplayState
{
[super didExitDisplayState];

if (_downloaderFlags.downloaderImplementsSetPriority && _isInImageDownloaderPriorityExperiment) {
id downloadIdentifier;
{
ASDN::MutexLocker l(_downloadIdentifierLock);
downloadIdentifier = _downloadIdentifier;
}

if (downloadIdentifier != nil) {
ASImageDownloaderPriority priority = ASImageDownloaderPriorityWithInterfaceState(self.interfaceState);
[_downloader setPriority:priority withDownloadIdentifier:downloadIdentifier];
}
if (_isInImageDownloaderPriorityExperiment) {
[self _updatePriorityOnDownloaderIfNeededWithDefaultPriority:ASImageDownloaderPriorityPreload];
}
}

Expand Down Expand Up @@ -503,7 +442,6 @@ - (void)_setDownloadIdentifier:(id)downloadIdentifier
_downloadIdentifier = downloadIdentifier;
}


#pragma mark - Image Loading Machinery

- (void)_loadImageIdentifiers
Expand Down Expand Up @@ -547,12 +485,31 @@ - (UIImage *)_bestImmediatelyAvailableImageFromDataSource:(id *)imageIdentifierO

#pragma mark -

/**
@note: This should be called without _downloadIdentifierLock held. We will lock
super to read our interface state and it's best to avoid acquiring both locks.
*/
- (void)_updatePriorityOnDownloaderIfNeededWithDefaultPriority:(ASImageDownloaderPriority)defaultPriority
{
ASAssertUnlocked(_downloadIdentifierLock);
if (_downloaderFlags.downloaderImplementsSetPriority) {
id downloadIdentifier;
{
ASDN::MutexLocker l(_downloadIdentifierLock);
downloadIdentifier = _downloadIdentifier;
}

if (downloadIdentifier != nil) {
ASImageDownloaderPriority priority = defaultPriority;
if (_isInImageDownloaderPriorityExperiment) {
priority = ASImageDownloaderPriorityWithInterfaceState(self.interfaceState);
}

[_downloader setPriority:priority withDownloadIdentifier:downloadIdentifier];
}
}
}

- (void)_updateProgressImageBlockOnDownloaderIfNeeded
{
ASAssertUnlocked(_downloadIdentifierLock);

BOOL shouldRenderProgressImages = self.shouldRenderProgressImages;

// Read our interface state before locking so that we don't lock super while holding our lock.
Expand Down
93 changes: 28 additions & 65 deletions Source/ASNetworkImageNode.mm
Original file line number Diff line number Diff line change
Expand Up @@ -340,14 +340,10 @@ - (void)displayWillStartAsynchronously:(BOOL)asynchronously

id<ASNetworkImageNodeDelegate> delegate;
BOOL notifyDelegate;
ASInterfaceState interfaceState;
id downloadIdentifier;
{
ASLockScopeSelf();
notifyDelegate = _delegateFlags.delegateWillStartDisplayAsynchronously;
delegate = _delegate;
downloadIdentifier = _downloaderFlags.downloaderImplementsSetPriority ? _downloadIdentifier : nil;
interfaceState = _interfaceState;
}
if (notifyDelegate) {
[delegate imageNodeWillStartDisplayAsynchronously:self];
Expand Down Expand Up @@ -377,13 +373,8 @@ - (void)displayWillStartAsynchronously:(BOOL)asynchronously
}
}

if (self.image == nil && downloadIdentifier != nil) {
ASImageDownloaderPriority priority = ASImageDownloaderPriorityImminent;
if (_isInImageDownloaderPriorityExperiment) {
priority = ASImageDownloaderPriorityWithInterfaceState(interfaceState);
}

[_downloader setPriority:priority withDownloadIdentifier:downloadIdentifier];
if (self.image == nil) {
[self _updatePriorityOnDownloaderIfNeededWithDefaultPriority:ASImageDownloaderPriorityImminent];
}
}

Expand All @@ -392,72 +383,22 @@ - (void)displayWillStartAsynchronously:(BOOL)asynchronously
- (void)didEnterVisibleState
{
[super didEnterVisibleState];

if (_downloaderFlags.downloaderImplementsSetPriority) {
id downloadIdentifier;
ASInterfaceState interfaceState;
{
ASLockScopeSelf();
downloadIdentifier = _downloadIdentifier;
interfaceState = _interfaceState;
}

if (downloadIdentifier != nil) {
ASImageDownloaderPriority priority = ASImageDownloaderPriorityVisible;
if (_isInImageDownloaderPriorityExperiment) {
priority = ASImageDownloaderPriorityWithInterfaceState(interfaceState);
}

[_downloader setPriority:priority withDownloadIdentifier:downloadIdentifier];
}
}

[self _updatePriorityOnDownloaderIfNeededWithDefaultPriority:ASImageDownloaderPriorityVisible];
[self _updateProgressImageBlockOnDownloaderIfNeeded];
}

- (void)didExitVisibleState
{
[super didExitVisibleState];

if (_downloaderFlags.downloaderImplementsSetPriority) {
id downloadIdentifier;
ASInterfaceState interfaceState;
{
ASLockScopeSelf();
downloadIdentifier = _downloadIdentifier;
interfaceState = _interfaceState;
}

if (downloadIdentifier != nil) {
ASImageDownloaderPriority priority = ASImageDownloaderPriorityPreload;
if (_isInImageDownloaderPriorityExperiment) {
priority = ASImageDownloaderPriorityWithInterfaceState(interfaceState);
}

[_downloader setPriority:priority withDownloadIdentifier:downloadIdentifier];
}
}

[self _updatePriorityOnDownloaderIfNeededWithDefaultPriority:ASImageDownloaderPriorityPreload];
[self _updateProgressImageBlockOnDownloaderIfNeeded];
}

- (void)didExitDisplayState
{
[super didExitDisplayState];

if (_downloaderFlags.downloaderImplementsSetPriority && _isInImageDownloaderPriorityExperiment) {
id downloadIdentifier;
ASInterfaceState interfaceState;
{
ASLockScopeSelf();
downloadIdentifier = _downloadIdentifier;
interfaceState = _interfaceState;
}

if (downloadIdentifier != nil) {
ASImageDownloaderPriority priority = ASImageDownloaderPriorityWithInterfaceState(interfaceState);
[_downloader setPriority:priority withDownloadIdentifier:downloadIdentifier];
}
if (_isInImageDownloaderPriorityExperiment) {
[self _updatePriorityOnDownloaderIfNeededWithDefaultPriority:ASImageDownloaderPriorityPreload];
}
}

Expand Down Expand Up @@ -507,6 +448,28 @@ - (void)handleProgressImage:(UIImage *)progressImage progress:(CGFloat)progress
[self _locked__setImage:progressImage];
}

- (void)_updatePriorityOnDownloaderIfNeededWithDefaultPriority:(ASImageDownloaderPriority)defaultPriority
{
if (_downloaderFlags.downloaderImplementsSetPriority) {
id downloadIdentifier;
ASInterfaceState interfaceState;
{
ASLockScopeSelf();
downloadIdentifier = _downloadIdentifier;
interfaceState = _interfaceState;
}

if (downloadIdentifier != nil) {
ASImageDownloaderPriority priority = defaultPriority;
if (_isInImageDownloaderPriorityExperiment) {
priority = ASImageDownloaderPriorityWithInterfaceState(interfaceState);
}

[_downloader setPriority:priority withDownloadIdentifier:downloadIdentifier];
}
}
}

- (void)_updateProgressImageBlockOnDownloaderIfNeeded
{
// If the downloader doesn't do progress, we are done.
Expand Down

0 comments on commit 6a1f368

Please sign in to comment.