Using CancellationToken

This commit is contained in:
2022-02-04 00:17:03 +01:00
parent 3f8309dc0f
commit 2e832f72d2
23 changed files with 1509 additions and 147 deletions

View File

@@ -69,7 +69,7 @@ namespace FileTime.Providers.Local
return remainingPath.Length == 0 ? rootContainer : await rootContainer.GetByPath(remainingPath, acceptDeepestMatch);
}
public async Task Refresh() => await Refreshed.InvokeAsync(this, AsyncEventArgs.Empty);
public async Task RefreshAsync(CancellationToken token = default) => await Refreshed.InvokeAsync(this, AsyncEventArgs.Empty, token);
public Task<IContainer> Clone() => Task.FromResult((IContainer)this);

View File

@@ -59,7 +59,7 @@ namespace FileTime.Providers.Local
if (_parent is LocalFolder parentFolder)
{
System.IO.File.Move(File.FullName, Path.Combine(parentFolder.Directory.FullName, newName));
await _parent.Refresh();
await _parent.RefreshAsync();
}
}

View File

@@ -59,16 +59,22 @@ namespace FileTime.Providers.Local
public Task<IContainer> Clone() => Task.FromResult((IContainer)new LocalFolder(Directory, Provider, _parent));
public Task Refresh()
public Task RefreshAsync(CancellationToken token = default)
{
_containers = new List<IContainer>();
_elements = new List<IElement>();
_items = new List<IItem>();
_exceptions.Clear();
try
{
if (token.IsCancellationRequested) return Task.CompletedTask;
_containers = Directory.GetDirectories().Select(d => new LocalFolder(d, Provider, this)).OrderBy(d => d.Name).ToList().AsReadOnly();
if (token.IsCancellationRequested) return Task.CompletedTask;
_elements = Directory.GetFiles().Select(f => new LocalFile(f, this, Provider)).OrderBy(f => f.Name).ToList().AsReadOnly();
if (token.IsCancellationRequested) return Task.CompletedTask;
}
catch (Exception e)
{
@@ -76,24 +82,24 @@ namespace FileTime.Providers.Local
}
_items = _containers.Cast<IItem>().Concat(_elements).ToList().AsReadOnly();
Refreshed?.InvokeAsync(this, AsyncEventArgs.Empty);
Refreshed?.InvokeAsync(this, AsyncEventArgs.Empty, token);
return Task.CompletedTask;
}
public async Task<IReadOnlyList<IItem>?> GetItems(CancellationToken token = default)
{
if (_items == null) await Refresh();
if (_items == null) await RefreshAsync(token);
return _items;
}
public async Task<IReadOnlyList<IContainer>?> GetContainers(CancellationToken token = default)
{
if (_containers == null) await Refresh();
if (_containers == null) await RefreshAsync(token);
return _containers;
}
public async Task<IReadOnlyList<IElement>?> GetElements(CancellationToken token = default)
{
if (_elements == null) await Refresh();
if (_elements == null) await RefreshAsync(token);
return _elements;
}
@@ -118,7 +124,7 @@ namespace FileTime.Providers.Local
public async Task<IContainer> CreateContainer(string name)
{
Directory.CreateSubdirectory(name);
await Refresh();
await RefreshAsync();
return _containers!.FirstOrDefault(c => Provider.NormalizePath(c.Name) == Provider.NormalizePath(name))!;
}
@@ -126,7 +132,7 @@ namespace FileTime.Providers.Local
public async Task<IElement> CreateElement(string name)
{
using (File.Create(Path.Combine(Directory.FullName, name))) { }
await Refresh();
await RefreshAsync();
return _elements!.FirstOrDefault(e => Provider.NormalizePath(e.Name) == Provider.NormalizePath(name))!;
}
@@ -150,7 +156,7 @@ namespace FileTime.Providers.Local
if (_parent is LocalFolder parentFolder)
{
System.IO.Directory.Move(Directory.FullName, Path.Combine(parentFolder.Directory.FullName, newName));
await _parent.Refresh();
await _parent.RefreshAsync();
}
}

View File

@@ -51,7 +51,7 @@ namespace FileTime.Providers.Smb
_items = _rootContainers.OrderBy(c => c.Name).ToList().AsReadOnly();
}
await Refresh();
await RefreshAsync();
return container;
}
@@ -89,7 +89,7 @@ namespace FileTime.Providers.Smb
public async Task<bool> IsExists(string name) => (await GetItems())?.Any(i => i.Name == name) ?? false;
public async Task Refresh() => await Refreshed.InvokeAsync(this, AsyncEventArgs.Empty);
public async Task RefreshAsync(CancellationToken token = default) => await Refreshed.InvokeAsync(this, AsyncEventArgs.Empty, token);
public bool CanHandlePath(string path) => path.StartsWith("smb://") || path.StartsWith(@"\\");

View File

@@ -88,7 +88,7 @@ namespace FileTime.Providers.Smb
throw new NotImplementedException();
}
public async Task Refresh()
public async Task RefreshAsync(CancellationToken token = default)
{
var containers = new List<IContainer>();
var elements = new List<IElement>();
@@ -96,7 +96,7 @@ namespace FileTime.Providers.Smb
try
{
var path = FullName![(_smbShare.FullName!.Length + 1)..];
(containers, elements) = await _smbShare.ListFolder(this, _smbShare.Name, path);
(containers, elements) = await _smbShare.ListFolder(this, _smbShare.Name, path, token);
}
catch { }
@@ -104,22 +104,22 @@ namespace FileTime.Providers.Smb
_elements = elements.AsReadOnly();
_items = _containers.Cast<IItem>().Concat(_elements).ToList().AsReadOnly();
await Refreshed.InvokeAsync(this, AsyncEventArgs.Empty);
await Refreshed.InvokeAsync(this, AsyncEventArgs.Empty, token);
}
public async Task<IReadOnlyList<IItem>?> GetItems(CancellationToken token = default)
{
if (_items == null) await Refresh();
if (_items == null) await RefreshAsync();
return _items;
}
public async Task<IReadOnlyList<IContainer>?> GetContainers(CancellationToken token = default)
{
if (_containers == null) await Refresh();
if (_containers == null) await RefreshAsync();
return _containers;
}
public async Task<IReadOnlyList<IElement>?> GetElements(CancellationToken token = default)
{
if (_elements == null) await Refresh();
if (_elements == null) await RefreshAsync();
return _elements;
}
public Task<bool> CanOpen() => Task.FromResult(true);

View File

@@ -51,12 +51,12 @@ namespace FileTime.Providers.Smb
public async Task<IReadOnlyList<IItem>?> GetItems(CancellationToken token = default)
{
if (_shares == null) await Refresh();
if (_shares == null) await RefreshAsync();
return _shares;
}
public async Task<IReadOnlyList<IContainer>?> GetContainers(CancellationToken token = default)
{
if (_shares == null) await Refresh();
if (_shares == null) await RefreshAsync();
return _shares;
}
public Task<IReadOnlyList<IElement>?> GetElements(CancellationToken token = default)
@@ -91,7 +91,7 @@ namespace FileTime.Providers.Smb
throw new NotImplementedException();
}
public async Task Refresh()
public async Task RefreshAsync(CancellationToken token = default)
{
List<string> shares = await _smbClientContext.RunWithSmbClientAsync((client) => client.ListShares(out var status));

View File

@@ -43,17 +43,17 @@ namespace FileTime.Providers.Smb
public async Task<IReadOnlyList<IItem>?> GetItems(CancellationToken token = default)
{
if (_items == null) await Refresh();
if (_items == null) await RefreshAsync();
return _items;
}
public async Task<IReadOnlyList<IContainer>?> GetContainers(CancellationToken token = default)
{
if (_containers == null) await Refresh();
if (_containers == null) await RefreshAsync();
return _containers;
}
public async Task<IReadOnlyList<IElement>?> GetElements(CancellationToken token = default)
{
if (_elements == null) await Refresh();
if (_elements == null) await RefreshAsync();
return _elements;
}
@@ -100,14 +100,14 @@ namespace FileTime.Providers.Smb
throw new NotImplementedException();
}
public async Task Refresh()
public async Task RefreshAsync(CancellationToken token = default)
{
var containers = new List<IContainer>();
var elements = new List<IElement>();
try
{
(containers, elements) = await ListFolder(this, Name, string.Empty);
(containers, elements) = await ListFolder(this, Name, string.Empty, token);
}
catch { }
@@ -115,10 +115,10 @@ namespace FileTime.Providers.Smb
_elements = elements.AsReadOnly();
_items = _containers.Cast<IItem>().Concat(_elements).ToList().AsReadOnly();
await Refreshed.InvokeAsync(this, AsyncEventArgs.Empty);
await Refreshed.InvokeAsync(this, AsyncEventArgs.Empty, token);
}
public async Task<(List<IContainer> containers, List<IElement> elements)> ListFolder(IContainer parent, string shareName, string folderName)
public async Task<(List<IContainer> containers, List<IElement> elements)> ListFolder(IContainer parent, string shareName, string folderName, CancellationToken token = default)
{
return await _smbClientContext.RunWithSmbClientAsync(client =>
{