Skip to content

Commit

Permalink
Added more ThrowIfCancellationRequested calls
Browse files Browse the repository at this point in the history
  • Loading branch information
daniel-luberda committed Nov 11, 2016
1 parent 75b9ba7 commit 5625977
Show file tree
Hide file tree
Showing 9 changed files with 28 additions and 0 deletions.
5 changes: 5 additions & 0 deletions source/FFImageLoading.Common/Cache/DownloadCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,14 @@ public virtual async Task<CacheStream> DownloadAndCacheIfNeededAsync(string url,
var diskStream = await configuration.DiskCache.TryGetStreamAsync(filename).ConfigureAwait(false);
if (diskStream != null)
{
token.ThrowIfCancellationRequested();
filePath = await configuration.DiskCache.GetFilePathAsync(filename).ConfigureAwait(false);
return new CacheStream(diskStream, true, filePath);
}
}

token.ThrowIfCancellationRequested();

var downloadInfo = new DownloadInformation(url, parameters.CustomCacheKey, filename, allowDiskCaching, duration);
parameters.OnDownloadStarted?.Invoke(downloadInfo);

Expand All @@ -62,7 +65,9 @@ public virtual async Task<CacheStream> DownloadAndCacheIfNeededAsync(string url,
await configuration.DiskCache.AddToSavingQueueIfNotExistsAsync(filename, responseBytes, duration).ConfigureAwait(false);
}

token.ThrowIfCancellationRequested();
filePath = await configuration.DiskCache.GetFilePathAsync(filename).ConfigureAwait(false);
token.ThrowIfCancellationRequested();
var memoryStream = new MemoryStream(responseBytes, false);
return new CacheStream(memoryStream, false, filePath);
}
Expand Down
1 change: 1 addition & 0 deletions source/FFImageLoading.Common/Work/ImageLoaderTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,7 @@ await MainThreadDispatcher.PostAsync(() =>
}
else
{
ThrowIfCancellationRequested();
// Loading placeholder if enabled
if (!isLoadingPlaceholderLoaded && !string.IsNullOrWhiteSpace(Parameters.LoadingPlaceholderPath))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public class BundleDataResolver : IDataResolver
{
public virtual Task<Tuple<Stream, LoadingResult, ImageInformation>> Resolve(string identifier, TaskParameter parameters, CancellationToken token)
{
token.ThrowIfCancellationRequested();
var stream = Context.Assets.Open(identifier, Access.Streaming);

if (stream == null)
Expand Down
2 changes: 2 additions & 0 deletions source/FFImageLoading.Droid/DataResolvers/FileDataResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ public virtual Task<Tuple<Stream, LoadingResult, ImageInformation>> Resolve(stri
throw new FileNotFoundException(identifier);
}

token.ThrowIfCancellationRequested();

var stream = FileStore.GetInputStream(identifier, true);

var imageInformation = new ImageInformation();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,15 @@ public virtual Task<Tuple<Stream, LoadingResult, ImageInformation>> Resolve(stri
int resourceId = 0;
if (!_resourceIdentifiersCache.TryGetValue(resourceName, out resourceId))
{
token.ThrowIfCancellationRequested();
resourceId = Context.Resources.GetIdentifier(resourceName.ToLower(), "drawable", Context.PackageName);
_resourceIdentifiersCache.TryAdd(resourceName.ToLower(), resourceId);
}

if (resourceId == 0)
throw new FileNotFoundException(identifier);

token.ThrowIfCancellationRequested();
Stream stream = Context.Resources.OpenRawResource(resourceId);

if (stream == null)
Expand Down
11 changes: 11 additions & 0 deletions source/FFImageLoading.Touch/DataResolvers/BundleDataResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,15 @@ public virtual async Task<Tuple<Stream, LoadingResult, ImageInformation>> Resolv

foreach (var fileType in fileTypes)
{
token.ThrowIfCancellationRequested();

int scale = (int)ScaleHelper.Scale;
if (scale > 1)
{
while (scale > 1)
{
token.ThrowIfCancellationRequested();

var tmpFile = string.Format(pattern, filename, scale, extension);
bundle = NSBundle._AllBundles.FirstOrDefault(bu =>
{
Expand All @@ -46,6 +50,8 @@ public virtual async Task<Tuple<Stream, LoadingResult, ImageInformation>> Resolv
}
}

token.ThrowIfCancellationRequested();

if (file == null)
{
file = identifier;
Expand All @@ -56,6 +62,8 @@ public virtual async Task<Tuple<Stream, LoadingResult, ImageInformation>> Resolv
});
}

token.ThrowIfCancellationRequested();

if (bundle != null)
{
var path = bundle.PathForResource(file, fileType);
Expand All @@ -71,6 +79,7 @@ public virtual async Task<Tuple<Stream, LoadingResult, ImageInformation>> Resolv
}

//Asset catalog
token.ThrowIfCancellationRequested();

if (UIDevice.CurrentDevice.CheckSystemVersion(9, 0))
{
Expand All @@ -84,6 +93,7 @@ public virtual async Task<Tuple<Stream, LoadingResult, ImageInformation>> Resolv

if (asset != null)
{
token.ThrowIfCancellationRequested();
var stream = asset.Data?.AsStream();
var imageInformation = new ImageInformation();
imageInformation.SetPath(identifier);
Expand All @@ -105,6 +115,7 @@ public virtual async Task<Tuple<Stream, LoadingResult, ImageInformation>> Resolv

if (image != null)
{
token.ThrowIfCancellationRequested();
var stream = image.AsPNG()?.AsStream();
var imageInformation = new ImageInformation();
imageInformation.SetPath(identifier);
Expand Down
4 changes: 4 additions & 0 deletions source/FFImageLoading.Touch/DataResolvers/FileDataResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ public virtual Task<Tuple<Stream, LoadingResult, ImageInformation>> Resolve(stri

while (scale > 1)
{
token.ThrowIfCancellationRequested();

var tmpFile = string.Format(pattern, filename, scale, extension);
if (FileStore.Exists(tmpFile))
{
Expand All @@ -38,6 +40,8 @@ public virtual Task<Tuple<Stream, LoadingResult, ImageInformation>> Resolve(stri
file = identifier;
}

token.ThrowIfCancellationRequested();

if (!string.IsNullOrEmpty(file))
{
var stream = FileStore.GetInputStream(file, true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public async virtual Task<Tuple<Stream, LoadingResult, ImageInformation>> Resolv
imageInformation.SetPath(identifier);
imageInformation.SetFilePath(identifier);

token.ThrowIfCancellationRequested();
var stream = await file.OpenStreamForReadAsync();

return new Tuple<Stream, LoadingResult, ImageInformation>(stream, LoadingResult.Disk, imageInformation);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public async virtual Task<Tuple<Stream, LoadingResult, ImageInformation>> Resolv
imageInformation.SetPath(identifier);
imageInformation.SetFilePath(file.Path);

token.ThrowIfCancellationRequested();
var stream = await file.OpenStreamForReadAsync();

return new Tuple<Stream, LoadingResult, ImageInformation>(stream, LoadingResult.CompiledResource, imageInformation);
Expand Down

0 comments on commit 5625977

Please sign in to comment.