|
5 | 5 | using Bedrock.Framework.Infrastructure;
|
6 | 6 | using Microsoft.AspNetCore.Connections;
|
7 | 7 |
|
8 |
| -namespace Bedrock.Framework |
| 8 | +namespace Bedrock.Framework; |
| 9 | + |
| 10 | +public partial class ClientBuilder : IConnectionBuilder |
9 | 11 | {
|
10 |
| - public partial class ClientBuilder : IConnectionBuilder |
11 |
| - { |
12 |
| - private readonly ConnectionBuilder _connectionBuilder; |
| 12 | + private readonly ConnectionBuilder _connectionBuilder; |
13 | 13 |
|
14 |
| - public ClientBuilder() : this(EmptyServiceProvider.Instance) |
15 |
| - { |
| 14 | + public ClientBuilder() : this(EmptyServiceProvider.Instance) |
| 15 | + { |
16 | 16 |
|
17 |
| - } |
| 17 | + } |
18 | 18 |
|
19 |
| - public ClientBuilder(IServiceProvider serviceProvider) |
20 |
| - { |
21 |
| - _connectionBuilder = new ConnectionBuilder(serviceProvider); |
22 |
| - } |
| 19 | + public ClientBuilder(IServiceProvider serviceProvider) |
| 20 | + { |
| 21 | + _connectionBuilder = new ConnectionBuilder(serviceProvider); |
| 22 | + } |
23 | 23 |
|
24 |
| - internal static object Key { get; } = new object(); |
| 24 | + internal static object Key { get; } = new object(); |
25 | 25 |
|
26 |
| - private IConnectionFactory ConnectionFactory { get; set; } = new ThrowConnectionFactory(); |
| 26 | + private IConnectionFactory ConnectionFactory { get; set; } = new ThrowConnectionFactory(); |
27 | 27 |
|
28 |
| - public IServiceProvider ApplicationServices => _connectionBuilder.ApplicationServices; |
| 28 | + public IServiceProvider ApplicationServices => _connectionBuilder.ApplicationServices; |
29 | 29 |
|
30 |
| - public Client Build() |
| 30 | + public Client Build() |
| 31 | + { |
| 32 | + // Middleware currently a single linear execution flow without a return value. |
| 33 | + // We need to return the connection when it reaches the innermost middleware (D in this case) |
| 34 | + // Then we need to wait until dispose is called to unwind that pipeline. |
| 35 | + |
| 36 | + // A -> |
| 37 | + // B -> |
| 38 | + // C -> |
| 39 | + // D |
| 40 | + // C <- |
| 41 | + // B <- |
| 42 | + // A <- |
| 43 | + |
| 44 | + _connectionBuilder.Run(connection => |
31 | 45 | {
|
32 |
| - // Middleware currently a single linear execution flow without a return value. |
33 |
| - // We need to return the connection when it reaches the innermost middleware (D in this case) |
34 |
| - // Then we need to wait until dispose is called to unwind that pipeline. |
35 |
| - |
36 |
| - // A -> |
37 |
| - // B -> |
38 |
| - // C -> |
39 |
| - // D |
40 |
| - // C <- |
41 |
| - // B <- |
42 |
| - // A <- |
43 |
| - |
44 |
| - _connectionBuilder.Run(connection => |
| 46 | + if (connection is ConnectionContextWithDelegate connectionContextWithDelegate) |
45 | 47 | {
|
46 |
| - if (connection is ConnectionContextWithDelegate connectionContextWithDelegate) |
47 |
| - { |
48 |
| - connectionContextWithDelegate.Initialized.TrySetResult(connectionContextWithDelegate); |
| 48 | + connectionContextWithDelegate.Initialized.TrySetResult(connectionContextWithDelegate); |
49 | 49 |
|
50 | 50 |
|
51 |
| - // This task needs to stay around until the connection is disposed |
52 |
| - // only then can we unwind the middleware chain |
53 |
| - return connectionContextWithDelegate.ExecutionTask; |
54 |
| - } |
| 51 | + // This task needs to stay around until the connection is disposed |
| 52 | + // only then can we unwind the middleware chain |
| 53 | + return connectionContextWithDelegate.ExecutionTask; |
| 54 | + } |
55 | 55 |
|
56 |
| - // REVIEW: Do we throw in this case? It's edgy but possible to call next with a differnt |
57 |
| - // connection delegate that originally given |
58 |
| - return Task.CompletedTask; |
59 |
| - }); |
| 56 | + // REVIEW: Do we throw in this case? It's edgy but possible to call next with a differnt |
| 57 | + // connection delegate that originally given |
| 58 | + return Task.CompletedTask; |
| 59 | + }); |
60 | 60 |
|
61 |
| - var application = _connectionBuilder.Build(); |
| 61 | + var application = _connectionBuilder.Build(); |
62 | 62 |
|
63 |
| - return new Client(ConnectionFactory, application); |
64 |
| - } |
| 63 | + return new Client(ConnectionFactory, application); |
| 64 | + } |
65 | 65 |
|
66 |
| - public ClientBuilder UseConnectionFactory(IConnectionFactory connectionFactory) |
67 |
| - { |
68 |
| - ConnectionFactory = connectionFactory ?? throw new ArgumentNullException(nameof(connectionFactory)); |
69 |
| - return this; |
70 |
| - } |
| 66 | + public ClientBuilder UseConnectionFactory(IConnectionFactory connectionFactory) |
| 67 | + { |
| 68 | + ConnectionFactory = connectionFactory ?? throw new ArgumentNullException(nameof(connectionFactory)); |
| 69 | + return this; |
| 70 | + } |
71 | 71 |
|
72 |
| - public ClientBuilder Use(Func<IConnectionFactory, IConnectionFactory> middleware) |
73 |
| - { |
74 |
| - ConnectionFactory = middleware(ConnectionFactory); |
75 |
| - return this; |
76 |
| - } |
| 72 | + public ClientBuilder Use(Func<IConnectionFactory, IConnectionFactory> middleware) |
| 73 | + { |
| 74 | + ConnectionFactory = middleware(ConnectionFactory); |
| 75 | + return this; |
| 76 | + } |
77 | 77 |
|
78 |
| - public IConnectionBuilder Use(Func<ConnectionDelegate, ConnectionDelegate> middleware) |
79 |
| - { |
80 |
| - return _connectionBuilder.Use(middleware); |
81 |
| - } |
| 78 | + public IConnectionBuilder Use(Func<ConnectionDelegate, ConnectionDelegate> middleware) |
| 79 | + { |
| 80 | + return _connectionBuilder.Use(middleware); |
| 81 | + } |
82 | 82 |
|
83 |
| - ConnectionDelegate IConnectionBuilder.Build() |
84 |
| - { |
85 |
| - return _connectionBuilder.Build(); |
86 |
| - } |
| 83 | + ConnectionDelegate IConnectionBuilder.Build() |
| 84 | + { |
| 85 | + return _connectionBuilder.Build(); |
| 86 | + } |
87 | 87 |
|
88 |
| - private class ThrowConnectionFactory : IConnectionFactory |
| 88 | + private class ThrowConnectionFactory : IConnectionFactory |
| 89 | + { |
| 90 | + public ValueTask<ConnectionContext> ConnectAsync(EndPoint endpoint, CancellationToken cancellationToken = default) |
89 | 91 | {
|
90 |
| - public ValueTask<ConnectionContext> ConnectAsync(EndPoint endpoint, CancellationToken cancellationToken = default) |
91 |
| - { |
92 |
| - throw new InvalidOperationException("No transport configured. Set the ConnectionFactory property."); |
93 |
| - } |
| 92 | + throw new InvalidOperationException("No transport configured. Set the ConnectionFactory property."); |
94 | 93 | }
|
95 | 94 | }
|
96 | 95 | }
|
0 commit comments