-
Notifications
You must be signed in to change notification settings - Fork 730
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(storage): Implement the StreamedCustomDataLoader
- Loading branch information
Showing
3 changed files
with
87 additions
and
85 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,80 +1,8 @@ | ||
#nullable enable | ||
|
||
using System; | ||
using System.IO; | ||
using System.Net.Http; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Microsoft.Extensions.Logging; | ||
using Uno; | ||
using Uno.Extensions; | ||
|
||
namespace Windows.Storage | ||
{ | ||
public delegate void StreamedFileDataRequestedHandler(StreamedFileDataRequest stream); | ||
|
||
//internal static class StreamedFileDataRequestedHandlerHelper | ||
//{ | ||
// public static StreamedFileDataRequestedHandler CreateFromUri( | ||
// Uri uri, | ||
// HttpMethod? method = null, | ||
// HttpClient? client = null, | ||
// ActionAsync<HttpResponseMessage?, Exception?>? onReady = null) | ||
// { | ||
// method ??= HttpMethod.Get; | ||
// client ??= new HttpClient(); | ||
|
||
// return req => Task.Run(() => FetchAsync(req, uri, method, client, onReady, req.CancellationToken)); | ||
// } | ||
|
||
// private static async Task FetchAsync( | ||
// StreamedFileDataRequest req, | ||
// Uri uri, | ||
// HttpMethod method, | ||
// HttpClient client, | ||
// ActionAsync<HttpResponseMessage?, Exception?>? onReady, | ||
// CancellationToken ct) | ||
// { | ||
// try | ||
// { | ||
// HttpResponseMessage response; | ||
// try | ||
// { | ||
// var request = new HttpRequestMessage(method, uri); | ||
// response = await client.SendAsync(request, HttpCompletionOption.ResponseHeadersRead, ct); | ||
|
||
// response.EnsureSuccessStatusCode(); | ||
|
||
// if (onReady is {}) | ||
// { | ||
// await onReady(ct, response, null); | ||
// } | ||
// } | ||
// catch (Exception e) when (onReady is {}) | ||
// { | ||
// await onReady(ct, null, e); | ||
// throw; | ||
// } | ||
|
||
// if (response.Content is { } content) | ||
// { | ||
// var responseStream = await content.ReadAsStreamAsync(); | ||
// await responseStream.CopyToAsync(req.AsStreamForWrite(), global::Windows.Storage.Streams.Buffer.DefaultCapacity, ct); | ||
// } | ||
// } | ||
// catch (Exception e) | ||
// { | ||
// if (req.Log().IsEnabled(LogLevel.Warning)) | ||
// { | ||
// req.Log().LogWarning("Failed to load content", e); | ||
// } | ||
|
||
// req.FailAndClose(StreamedFileFailureMode.Failed); | ||
// } | ||
// finally | ||
// { | ||
// req.Dispose(); | ||
// } | ||
// } | ||
//} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
#nullable enable | ||
|
||
using System; | ||
using System.Linq; | ||
using Windows.Foundation; | ||
|
||
namespace Windows.Storage.Streams | ||
{ | ||
internal class StreamedCustomDataLoader : IStreamedDataLoader | ||
{ | ||
private uint _loaded; | ||
private bool _isCompleted; | ||
private StreamedFileFailureMode? _failure; | ||
|
||
public StreamedCustomDataLoader(StreamedFileDataRequestedHandler handler, TemporaryFile? tempFile = null) | ||
{ | ||
File = tempFile ?? new TemporaryFile(); | ||
|
||
handler(new StreamedFileDataRequest(this)); | ||
} | ||
|
||
/// <inheritdoc /> | ||
public event TypedEventHandler<IStreamedDataLoader, object?>? DataUpdated; | ||
|
||
/// <inheritdoc /> | ||
public TemporaryFile File { get; } | ||
|
||
/// <inheritdoc /> | ||
public string? ContentType { get; } = RandomAccessStreamWithContentType.DefaultContentType; | ||
|
||
/// <inheritdoc /> | ||
public void CheckState() | ||
{ | ||
if (_failure.HasValue) | ||
{ | ||
throw new InvalidOperationException($"The async load of the data has failed ('{_failure.Value}')"); | ||
} | ||
} | ||
|
||
/// <inheritdoc /> | ||
public bool CanRead(ulong position) | ||
=> _isCompleted || position < _loaded; | ||
|
||
internal void ReportDataWritten(uint length) | ||
{ | ||
_loaded += length; | ||
DataUpdated?.Invoke(this, default); | ||
} | ||
|
||
internal void ReportLoadCompleted(StreamedFileFailureMode? failure = null) | ||
{ | ||
// Note: this is expected to be invoke more than once! | ||
|
||
_failure ??= failure; | ||
|
||
if (!_isCompleted) | ||
{ | ||
_isCompleted = true; | ||
|
||
DataUpdated?.Invoke(this, default); | ||
} | ||
} | ||
} | ||
} |