Async core refactor
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
using AsyncEvent;
|
||||
using FileTime.Core.Models;
|
||||
using FileTime.Core.Providers;
|
||||
using SMBLibrary;
|
||||
@@ -10,43 +11,9 @@ namespace FileTime.Providers.Smb
|
||||
private IReadOnlyList<IItem>? _items;
|
||||
private IReadOnlyList<IContainer>? _containers;
|
||||
private IReadOnlyList<IElement>? _elements;
|
||||
private Func<ISMBClient> _getSmbClient;
|
||||
private readonly SmbShare _smbShare;
|
||||
private readonly IContainer? _parent;
|
||||
|
||||
public IReadOnlyList<IItem> Items
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_items == null) Refresh();
|
||||
return _items!;
|
||||
}
|
||||
|
||||
private set => _items = value;
|
||||
}
|
||||
|
||||
public IReadOnlyList<IContainer> Containers
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_containers == null) Refresh();
|
||||
return _containers!;
|
||||
}
|
||||
|
||||
private set => _containers = value;
|
||||
}
|
||||
|
||||
public IReadOnlyList<IElement> Elements
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_elements == null) Refresh();
|
||||
return _elements!;
|
||||
}
|
||||
|
||||
private set => _elements = value;
|
||||
}
|
||||
|
||||
public string Name { get; }
|
||||
|
||||
public string? FullName { get; }
|
||||
@@ -56,39 +23,33 @@ namespace FileTime.Providers.Smb
|
||||
public SmbContentProvider Provider { get; }
|
||||
IContentProvider IItem.Provider => Provider;
|
||||
|
||||
public event EventHandler? Refreshed;
|
||||
public AsyncEventHandler Refreshed { get; } = new();
|
||||
|
||||
public SmbFolder(string name, SmbContentProvider contentProvider, SmbShare smbShare, IContainer parent, Func<ISMBClient> getSmbClient)
|
||||
public SmbFolder(string name, SmbContentProvider contentProvider, SmbShare smbShare, IContainer parent)
|
||||
{
|
||||
_parent = parent;
|
||||
_getSmbClient = getSmbClient;
|
||||
_smbShare = smbShare;
|
||||
|
||||
Name = name;
|
||||
FullName = parent?.FullName == null ? Name : parent.FullName + Constants.SeparatorChar + Name;
|
||||
Provider = contentProvider;
|
||||
_smbShare = smbShare;
|
||||
}
|
||||
|
||||
public IContainer CreateContainer(string name)
|
||||
public Task<IContainer> CreateContainer(string name)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public IElement CreateElement(string name)
|
||||
public Task<IElement> CreateElement(string name)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public void Delete()
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public IItem? GetByPath(string path)
|
||||
public async Task<IItem?> GetByPath(string path)
|
||||
{
|
||||
var paths = path.Split(Constants.SeparatorChar);
|
||||
|
||||
var item = Items.FirstOrDefault(i => i.Name == paths[0]);
|
||||
var item = (await GetItems())?.FirstOrDefault(i => i.Name == paths[0]);
|
||||
|
||||
if (paths.Length == 1)
|
||||
{
|
||||
@@ -97,7 +58,7 @@ namespace FileTime.Providers.Smb
|
||||
|
||||
if (item is IContainer container)
|
||||
{
|
||||
return container.GetByPath(string.Join(Constants.SeparatorChar, paths.Skip(1)));
|
||||
return await container.GetByPath(string.Join(Constants.SeparatorChar, paths.Skip(1)));
|
||||
}
|
||||
|
||||
return null;
|
||||
@@ -105,12 +66,17 @@ namespace FileTime.Providers.Smb
|
||||
|
||||
public IContainer? GetParent() => _parent;
|
||||
|
||||
public bool IsExists(string name)
|
||||
public Task<bool> IsExists(string name)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public void Refresh()
|
||||
public Task Delete()
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public async Task Refresh()
|
||||
{
|
||||
var containers = new List<IContainer>();
|
||||
var elements = new List<IElement>();
|
||||
@@ -118,7 +84,7 @@ namespace FileTime.Providers.Smb
|
||||
try
|
||||
{
|
||||
var path = FullName![(_smbShare.FullName!.Length + 1)..];
|
||||
(containers, elements) = _smbShare.ListFolder(this, _smbShare.Name, path);
|
||||
(containers, elements) = await _smbShare.ListFolder(this, _smbShare.Name, path);
|
||||
}
|
||||
catch { }
|
||||
|
||||
@@ -126,7 +92,23 @@ namespace FileTime.Providers.Smb
|
||||
_elements = elements.AsReadOnly();
|
||||
|
||||
_items = _containers.Cast<IItem>().Concat(_elements).ToList().AsReadOnly();
|
||||
Refreshed?.Invoke(this, EventArgs.Empty);
|
||||
await Refreshed?.InvokeAsync(this, AsyncEventArgs.Empty);
|
||||
}
|
||||
|
||||
public async Task<IReadOnlyList<IItem>?> GetItems(CancellationToken token = default)
|
||||
{
|
||||
if (_items == null) await Refresh();
|
||||
return _items;
|
||||
}
|
||||
public async Task<IReadOnlyList<IContainer>?> GetContainers(CancellationToken token = default)
|
||||
{
|
||||
if (_containers == null) await Refresh();
|
||||
return _containers;
|
||||
}
|
||||
public async Task<IReadOnlyList<IElement>?> GetElements(CancellationToken token = default)
|
||||
{
|
||||
if (_elements == null) await Refresh();
|
||||
return _elements;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user