Skip to content

Commit fe0c1a7

Browse files
committed
Improved proxies
1 parent 057920b commit fe0c1a7

29 files changed

+462
-362
lines changed

src/DragonFly.AspNetCore/BackgroundTasks/BackgroundTaskContextExtensions.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public static async Task ProcessQueryAsync<T, TQuery>(this BackgroundTaskContext
2626
break;
2727
}
2828

29-
QueryResult<T> result = await queryAction(ctx.Input);
29+
QueryResult<T> result = await queryAction(ctx.Input).ConfigureAwait(false);
3030

3131
if (result.Items.Count == 0)
3232
{
@@ -40,7 +40,7 @@ public static async Task ProcessQueryAsync<T, TQuery>(this BackgroundTaskContext
4040
break;
4141
}
4242

43-
await ctx.UpdateStatusAsync(item.ToString(), counter, result.TotalCount);
43+
await ctx.UpdateStatusAsync(item.ToString(), counter, result.TotalCount).ConfigureAwait(false);
4444

4545
try
4646
{
@@ -61,6 +61,6 @@ public static async Task ProcessQueryAsync<T, TQuery>(this BackgroundTaskContext
6161
ctx.Input.Skip += ctx.Input.Take;
6262
}
6363

64-
await ctx.UpdateStatusAsync($"Succeed: {counterSucceed} / Failed: {counterFailed}", progressValue: counter);
64+
await ctx.UpdateStatusAsync($"Succeed: {counterSucceed} / Failed: {counterFailed}", progressValue: counter).ConfigureAwait(false);
6565
}
6666
}

src/DragonFly.Client/_Imports.razor

+1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
@using BlazorStrap
3131
@using BlazorStrap.V5
3232
@using DragonFly.Client.Base
33+
@using DragonFly.Proxies
3334
@using static Microsoft.AspNetCore.Components.Web.RenderMode
3435
@attribute [Authorize]
3536
@inject IDragonFlyApi DragonFly

src/DragonFly.Core/Api/Init/DragonFlyApiExtensions.cs

+1-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
// https://github.com/usercode/DragonFly
33
// MIT License
44

5-
using System.Security.Claims;
65
using Microsoft.Extensions.DependencyInjection;
76
using Microsoft.Extensions.Logging;
87

@@ -37,7 +36,7 @@ public static async Task InitAsync(this IDragonFlyApi api)
3736
logger.LogInformation("Initializing {type}", GetName(item));
3837

3938
await item.ExecuteAsync(api);
40-
}
39+
}
4140
}
4241

4342
private static string? GetName(object initializer)

src/DragonFly.Core/AsyncLazy.cs

+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
// Copyright (c) usercode
2+
// https://github.com/usercode/DragonFly
3+
// MIT License
4+
5+
using System.Runtime.CompilerServices;
6+
7+
namespace DragonFly;
8+
9+
/// <summary>
10+
/// AsyncLazy
11+
/// </summary>
12+
/// <typeparam name="T"></typeparam>
13+
public class AsyncLazy<T>
14+
{
15+
public AsyncLazy(ContentReference reference, Func<Task<T?>> taskFactory)
16+
{
17+
ContentReference = reference;
18+
_lazy = new Lazy<Task<T?>>(taskFactory);
19+
}
20+
21+
public AsyncLazy(T? value)
22+
{
23+
SetDirectValue(value);
24+
}
25+
26+
private Lazy<Task<T?>> _lazy;
27+
public ContentReference? ContentReference { get; private set; }
28+
private T? _directValue;
29+
private bool _directValueChanged;
30+
31+
public TaskAwaiter<T?> GetAwaiter()
32+
{
33+
return _lazy.Value.GetAwaiter();
34+
}
35+
36+
public void SetDirectValue(T? value)
37+
{
38+
if (value is ContentItem content)
39+
{
40+
ContentReference = content.ToReference();
41+
}
42+
else if (value is Asset asset)
43+
{
44+
ContentReference = asset.ToReference();
45+
}
46+
else
47+
{
48+
ContentReference = null;
49+
}
50+
51+
_directValue = value;
52+
_directValueChanged = true;
53+
54+
_lazy = new Lazy<Task<T?>>(() => Task.FromResult(value));
55+
}
56+
57+
public bool TryGetDirectValue(out T? value)
58+
{
59+
if (_directValueChanged)
60+
{
61+
value = _directValue;
62+
63+
return true;
64+
}
65+
66+
value = default;
67+
68+
return false;
69+
}
70+
71+
public static implicit operator AsyncLazy<T>(T? value)
72+
{
73+
return new AsyncLazy<T>(value);
74+
}
75+
}

src/DragonFly.Core/Modules/ContentItems/Models/Fields/AssetField.cs

+2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
// https://github.com/usercode/DragonFly
33
// MIT License
44

5+
using DragonFly.Core;
6+
57
namespace DragonFly;
68

79
/// <summary>

src/DragonFly.Core/Modules/Proxies/IProxyObject.cs

+1
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,5 @@ namespace DragonFly.Generator;
66

77
public interface IProxyObject
88
{
9+
Task LoadAsync();
910
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Copyright (c) usercode
2+
// https://github.com/usercode/DragonFly
3+
// MIT License
4+
5+
using DragonFly.Generator;
6+
7+
namespace DragonFly.Proxies;
8+
9+
public static class Proxy
10+
{
11+
public static async Task<bool> LoadAsync(object? obj)
12+
{
13+
if (obj is IProxyObject proxyObject)
14+
{
15+
await proxyObject.LoadAsync();
16+
17+
return true;
18+
}
19+
20+
return false;
21+
}
22+
}

src/DragonFly.Generator.Tests/DragonFly.Generator.Tests.csproj

-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
</PropertyGroup>
88

99
<ItemGroup>
10-
<PackageReference Include="Microsoft.CodeAnalysis.Analyzer.Testing" Version="1.1.2" />
1110
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.13.0" />
1211
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.13.0" />
1312
<PackageReference Include="System.Formats.Asn1" Version="9.0.3" />

0 commit comments

Comments
 (0)