Skip to content

Commit b412baf

Browse files
committed
WIP
1 parent 6b073fd commit b412baf

12 files changed

+105
-48
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
namespace Wolder.Core.Workspace.Events;
2+
3+
public record InvocationBeginContext(
4+
IInvokable Invokable, object? Parameter)
5+
{
6+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
namespace Wolder.Core.Workspace.Events;
2+
3+
public record InvocationEndContext(
4+
IInvokable Invokable)
5+
{
6+
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
namespace Wolder.Core.Workspace.Events;
2+
3+
public class WorkspaceStateEventDispatcher
4+
{
5+
internal WorkspaceStateEvents Events { get; }
6+
public ICollection<WorkspaceStateEvents> Delegates { get; } = new List<WorkspaceStateEvents>();
7+
8+
public WorkspaceStateEventDispatcher()
9+
{
10+
Events = new WorkspaceStateEvents()
11+
{
12+
WorkspaceInitializedAsync = async () =>
13+
await Task.WhenAll(Delegates.Select(d =>
14+
d.WorkspaceInitializedAsync())),
15+
InvocationBeginAsync = async (c) =>
16+
await Task.WhenAll(Delegates.Select(d =>
17+
d.InvocationBeginAsync(c))),
18+
InvocationEndAsync = async (c) =>
19+
await Task.WhenAll(Delegates.Select(d =>
20+
d.InvocationEndAsync(c))),
21+
};
22+
}
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
namespace Wolder.Core.Workspace.Events;
2+
3+
public class WorkspaceStateEvents
4+
{
5+
public Func<Task> WorkspaceInitializedAsync { get; set; } = () => Task.CompletedTask;
6+
public Func<InvocationBeginContext, Task> InvocationBeginAsync { get; set; } = (c) => Task.CompletedTask;
7+
public Func<InvocationEndContext, Task> InvocationEndAsync { get; set; } = (c) => Task.CompletedTask;
8+
}

src/Wolder.Core/Workspace/GeneratorWorkspaceBuilder.cs

+3-6
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using Microsoft.Extensions.Logging;
55
using Wolder.Core.Assistants;
66
using Wolder.Core.Files;
7+
using Wolder.Core.Workspace.Events;
78

89
namespace Wolder.Core.Workspace;
910

@@ -12,14 +13,15 @@ public class GeneratorWorkspaceBuilder
1213
private readonly ILoggerFactory _loggerFactory;
1314
private readonly IConfigurationSection _rootConfiguration;
1415
private readonly ServiceCollection _services;
15-
private readonly List<IWorkspaceStateDelegate> _stateDelegates = new List<IWorkspaceStateDelegate>();
16+
public WorkspaceStateEventDispatcher EventDispatcher { get; } = new();
1617

1718
public GeneratorWorkspaceBuilder(ILoggerFactory loggerFactory, IConfigurationSection rootConfiguration)
1819
{
1920
_loggerFactory = loggerFactory;
2021
_rootConfiguration = rootConfiguration;
2122
_services = new ServiceCollection();
2223
_services.AddSingleton<WorkspaceRootPath>();
24+
_services.AddSingleton(EventDispatcher);
2325
_services.AddSingleton(loggerFactory);
2426
_services.Add(ServiceDescriptor.Singleton(typeof(ILogger<>), typeof(Logger<>)));
2527
_services.AddScoped<IInvoke, InvocationMiddleware>();
@@ -31,11 +33,6 @@ public GeneratorWorkspaceBuilder(ILoggerFactory loggerFactory, IConfigurationSec
3133
public IServiceCollection Services => _services;
3234

3335
public IConfiguration Config => _rootConfiguration;
34-
35-
public void RegisterWorkspaceStateDelegate(IWorkspaceStateDelegate stateDelegate)
36-
{
37-
_stateDelegates.Add(stateDelegate);
38-
}
3936

4037
public GeneratorWorkspaceBuilder AddActions<TActionCollection>()
4138
where TActionCollection : class, ITypedActionCollection

src/Wolder.Core/Workspace/IWorkspaceStateDelegate.cs

-7
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,20 @@
11
using Microsoft.Extensions.DependencyInjection;
2+
using Wolder.Core.Workspace.Events;
23

34
namespace Wolder.Core.Workspace;
45

56
internal class InvocationMiddleware(IServiceProvider provider) : IInvoke
67
{
7-
private readonly IWorkspaceStateDelegate _stateDelegate = provider.GetRequiredService<IWorkspaceStateDelegate>();
8+
private readonly WorkspaceStateEventDispatcher _stateDelegate = provider.GetRequiredService<WorkspaceStateEventDispatcher>();
89

910
public async Task InvokeVoidAsync<TInvokable>()
1011
where TInvokable : IVoidInvokable
1112
{
1213
using var scope = provider.CreateScope();
1314
var invokable = ActivatorUtilities.CreateInstance<TInvokable>(scope.ServiceProvider);
14-
await _stateDelegate.InvocationBeginAsync(invokable, null);
15+
await _stateDelegate.Events.InvocationBeginAsync(new (invokable, null));
1516
await invokable.InvokeAsync();
16-
await _stateDelegate.InvocationEndAsync();
17+
await _stateDelegate.Events.InvocationEndAsync(new (invokable));
1718
}
1819

1920
public async Task InvokeVoidAsync<TInvokable, TParameter>(TParameter parameter)
@@ -22,9 +23,9 @@ public async Task InvokeVoidAsync<TInvokable, TParameter>(TParameter parameter)
2223
{
2324
var scope = provider.CreateScope();
2425
var invokable = ActivatorUtilities.CreateInstance<TInvokable>(scope.ServiceProvider, parameter);
25-
await _stateDelegate.InvocationBeginAsync(invokable, parameter);
26+
await _stateDelegate.Events.InvocationBeginAsync(new(invokable, parameter));
2627
await invokable.InvokeAsync();
27-
await _stateDelegate.InvocationEndAsync();
28+
await _stateDelegate.Events.InvocationEndAsync(new(invokable));
2829
}
2930

3031
public async Task<TOutput> InvokeAsync<TInvokable, TParameter, TOutput>(TParameter parameter)
@@ -33,9 +34,9 @@ public async Task<TOutput> InvokeAsync<TInvokable, TParameter, TOutput>(TParamet
3334
{
3435
var scope = provider.CreateScope();
3536
var invokable = ActivatorUtilities.CreateInstance<TInvokable>(scope.ServiceProvider, parameter);
36-
await _stateDelegate.InvocationBeginAsync(invokable, parameter);
37+
await _stateDelegate.Events.InvocationBeginAsync(new(invokable, parameter));
3738
var result = await invokable.InvokeAsync();
38-
await _stateDelegate.InvocationEndAsync();
39+
await _stateDelegate.Events.InvocationEndAsync(new(invokable));
3940
return result;
4041
}
4142

@@ -44,9 +45,9 @@ public async Task<TOutput> InvokeAsync<TInvokable, TOutput>()
4445
{
4546
var scope = provider.CreateScope();
4647
var invokable = ActivatorUtilities.CreateInstance<TInvokable>(scope.ServiceProvider);
47-
await _stateDelegate.InvocationBeginAsync(invokable, null);
48+
await _stateDelegate.Events.InvocationBeginAsync(new(invokable, null));
4849
var result = await invokable.InvokeAsync();
49-
await _stateDelegate.InvocationEndAsync();
50+
await _stateDelegate.Events.InvocationEndAsync(new(invokable));
5051
return result;
5152
}
5253
}

src/Wolder.Core/Workspace/WorkspaceStateDelegateDispatcher.cs

-18
This file was deleted.

src/Wolder.Interactive.Web/App.razor

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
</head>
1414

1515
<body>
16-
<Routes/>
16+
<Routes />
1717
<script src="_framework/blazor.web.js"></script>
1818
</body>
1919

src/Wolder.Interactive.Web/SetupExtensions.cs

+3-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ public static class GeneratorWorkspaceBuilderExtensions
66
{
77
public static GeneratorWorkspaceBuilder AddInteractiveWebServer(this GeneratorWorkspaceBuilder builder)
88
{
9-
builder.RegisterWorkspaceStateDelegate();
9+
var server = new WorkspaceInteractiveWebHost();
10+
builder.Services.AddSingleton(server);
11+
builder.EventDispatcher.Delegates.Add(server.WorkspaceStateNotifications.Events);
1012
return builder;
1113
}
1214
}

src/Wolder.Interactive.Web/WolderInteractiveHost.cs renamed to src/Wolder.Interactive.Web/WorkspaceInteractiveWebHost.cs

+19-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,28 @@
11
namespace Wolder.Interactive.Web;
22

3-
public class WolderInteractiveHost()
3+
public class WorkspaceInteractiveWebHost
44
{
55
private WebApplication? _app;
6+
private Task? _serverInitializeTask;
67

7-
public async Task StartAsync()
8+
public WorkspaceInteractiveWebHost()
89
{
10+
WorkspaceStateNotifications.WorkspaceInitialized += () =>
11+
{
12+
_serverInitializeTask = StartAsync();
13+
};
14+
}
15+
16+
public WorkspaceStateNotifications WorkspaceStateNotifications { get; } = new();
17+
18+
private async Task StartAsync()
19+
{
20+
if (_serverInitializeTask is not null)
21+
{
22+
await _serverInitializeTask;
23+
return;
24+
}
25+
926
var webBuilder = WebApplication.CreateBuilder();
1027

1128
// Add services to the container.

src/Wolder.Interactive.Web/WorkspaceStateNotifications.cs

+25-4
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,39 @@
11
using Wolder.Core.Workspace;
2+
using Wolder.Core.Workspace.Events;
23
using Wolder.Interactive.Web.Models;
34

45
namespace Wolder.Interactive.Web;
56

6-
public class WorkspaceStateNotifications : IWorkspaceStateDelegate
7+
public class WorkspaceStateNotifications
78
{
9+
public WorkspaceStateNotifications()
10+
{
11+
Events = new()
12+
{
13+
WorkspaceInitializedAsync = WorkspaceInitializedAsync,
14+
InvocationBeginAsync = InvocationBeginAsync,
15+
InvocationEndAsync = InvocationEndAsync,
16+
};
17+
}
18+
19+
internal WorkspaceStateEvents Events { get; }
20+
821
public event Action<InvocationDetail>? InvocationBegin;
9-
public Task InvocationBeginAsync(IInvokable invokable, object? parameters)
22+
public event Action? WorkspaceInitialized;
23+
24+
public Task WorkspaceInitializedAsync()
25+
{
26+
WorkspaceInitialized?.Invoke();
27+
return Task.CompletedTask;
28+
}
29+
30+
public Task InvocationBeginAsync(InvocationBeginContext c)
1031
{
11-
InvocationBegin?.Invoke(new InvocationDetail(invokable.GetType().Name));
32+
InvocationBegin?.Invoke(new InvocationDetail(c.Invokable.GetType().Name));
1233
return Task.CompletedTask;
1334
}
1435

15-
public Task InvocationEndAsync()
36+
public Task InvocationEndAsync(InvocationEndContext c)
1637
{
1738
return Task.CompletedTask;
1839
}

0 commit comments

Comments
 (0)