Skip to content

Commit 0ef4804

Browse files
committed
Completes reversal of NotifyAsync change
For scenarios where async notification of events is needed, this might fall in the telemetry category, which does the work in a separte buffered way already, so this wouldn't be necessary at all (i.e. in an OpenTelemetry scenario, for example).
1 parent 0170e3d commit 0ef4804

14 files changed

Lines changed: 68 additions & 81 deletions

File tree

src/Merq.Abstractions/IMessageBus.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ public interface IMessageBus
103103
/// <param name="callerName">Optional calling member name, provided by default by the compiler.</param>
104104
/// <param name="callerFile">Optional calling file name, provided by default by the compiler.</param>
105105
/// <param name="callerLine">Optional calling line number, provided by default by the compiler.</param>
106-
ValueTask NotifyAsync<TEvent>(TEvent e, [CallerMemberName] string? callerName = default, [CallerFilePath] string? callerFile = default, [CallerLineNumber] int? callerLine = default);
106+
void Notify<TEvent>(TEvent e, [CallerMemberName] string? callerName = default, [CallerFilePath] string? callerFile = default, [CallerLineNumber] int? callerLine = default);
107107

108108
/// <summary>
109109
/// Observes the events of a given type <typeparamref name="TEvent"/>.

src/Merq.Abstractions/IMessageBusExtensions.cs

Lines changed: 1 addition & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
1-
using System;
2-
using System.ComponentModel;
1+
using System.ComponentModel;
32
using System.Runtime.CompilerServices;
4-
using System.Threading;
5-
using System.Threading.Tasks;
63

74
namespace Merq;
85

@@ -12,32 +9,12 @@ namespace Merq;
129
[EditorBrowsable(EditorBrowsableState.Never)]
1310
public static class IMessageBusExtensions
1411
{
15-
/// <summary>
16-
/// Notifies the bus of an event.
17-
/// </summary>
18-
[EditorBrowsable(EditorBrowsableState.Never)]
19-
[Obsolete("Use await NotifyAsync instead.")]
20-
public static void Notify<TEvent>(this IMessageBus bus, TEvent e, [CallerMemberName] string? callerName = default, [CallerFilePath] string? callerFile = default, [CallerLineNumber] int? callerLine = default)
21-
{
22-
var result = bus.NotifyAsync(e, callerName, callerFile, callerLine);
23-
while (!result.IsCompleted)
24-
Thread.SpinWait(1);
25-
}
26-
2712
/// <summary>
2813
/// Notifies the bus of a new event instance of <typeparamref name="TEvent"/>.
2914
/// </summary>
30-
[EditorBrowsable(EditorBrowsableState.Never)]
31-
[Obsolete("Use await NotifyAsync instead.")]
3215
public static void Notify<TEvent>(this IMessageBus bus, [CallerMemberName] string? callerName = default, [CallerFilePath] string? callerFile = default, [CallerLineNumber] int? callerLine = default) where TEvent : new()
3316
=> bus.Notify(new TEvent(), callerName, callerFile, callerLine);
3417

35-
/// <summary>
36-
/// Notifies the bus of a new event instance of <typeparamref name="TEvent"/>.
37-
/// </summary>
38-
public static ValueTask NotifyAsync<TEvent>(this IMessageBus bus, [CallerMemberName] string? callerName = default, [CallerFilePath] string? callerFile = default, [CallerLineNumber] int? callerLine = default) where TEvent : new()
39-
=> bus.NotifyAsync(new TEvent(), callerName, callerFile, callerLine);
40-
4118
/// <summary>
4219
/// Executes the given synchronous command.
4320
/// </summary>

src/Merq.Abstractions/Merq.Abstractions.csproj

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,12 @@
1616

1717
<ItemGroup>
1818
<PackageReference Include="NuGetizer" />
19-
<PackageReference Include="Microsoft.CSharp" />
2019
<PackageReference Include="System.Threading.Tasks.Extensions" Condition="$(TargetFramework) == 'netstandard2.0'" />
2120
</ItemGroup>
2221

2322
<ItemGroup>
24-
<ProjectReference Include="..\Merq.CodeAnalysis\Merq.CodeAnalysis.csproj" ReferenceOutputAssembly="false" OutputItemType="Analyzer" />
25-
<ProjectReference Include="..\Merq.CodeFixes\Merq.CodeFixes.csproj" ReferenceOutputAssembly="false" OutputItemType="Analyzer" />
23+
<ProjectReference Include="..\Merq.CodeAnalysis\Merq.CodeAnalysis.csproj" ReferenceOutputAssembly="false" />
24+
<ProjectReference Include="..\Merq.CodeFixes\Merq.CodeFixes.csproj" ReferenceOutputAssembly="false" />
2625
</ItemGroup>
2726

2827
<ItemGroup>

src/Merq.CodeAnalysis.Tests/Merq.CodeAnalysis.Tests.csproj

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
<PackageReference Include="Moq" />
1818
<PackageReference Include="xunit" />
1919
<PackageReference Include="xunit.runner.visualstudio" />
20-
<PackageReference Include="Microsoft.CSharp" />
2120
<PackageReference Include="Scriban" />
2221
<PackageReference Include="SharpYaml" />
2322
<PackageReference Include="Superpower" PrivateAssets="all" />
@@ -35,4 +34,7 @@
3534
<Using Include="Xunit.Abstractions" />
3635
</ItemGroup>
3736

37+
<Import Project="..\SponsorLink\SponsorLink\buildTransitive\Devlooped.Sponsors.targets" />
38+
<Import Project="..\SponsorLink\SponsorLink.Analyzer.Tests.targets" />
39+
3840
</Project>

src/Merq.Tests/DuckTyping.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,29 +53,29 @@ public abstract class DuckTyping
5353

5454
#if NET6_0_OR_GREATER
5555
[Fact]
56-
public async Task ConvertEvent()
56+
public void ConvertEvent()
5757
{
5858
var bus = CreateMessageBus();
5959
string? message = null;
6060

6161
bus.Observe<Library1::Library.DuckEvent>()
6262
.Subscribe(e => message = e.Message);
6363

64-
await bus.NotifyAsync(new Library2::Library.DuckEvent("Foo"));
64+
bus.Notify(new Library2::Library.DuckEvent("Foo"));
6565

6666
Assert.Equal("Foo", message);
6767
}
6868

6969
[Fact]
70-
public async Task ConvertEventHierarchy()
70+
public void ConvertEventHierarchy()
7171
{
7272
var bus = CreateMessageBus();
7373
int sumstarts = 0;
7474

7575
bus.Observe<Library1::Library.OnDidEdit>()
7676
.Subscribe(e => sumstarts = e.Buffer.Lines.Select(l => l.Start).Sum(p => p.X));
7777

78-
await bus.NotifyAsync(new Library2::Library.OnDidEdit(
78+
bus.Notify(new Library2::Library.OnDidEdit(
7979
new Library2::Library.Buffer(new[]
8080
{
8181
new Library2::Library.Line(new Library2::Library.Point(1, 2), new Library2::Library.Point(3, 4)) ,
@@ -86,15 +86,15 @@ await bus.NotifyAsync(new Library2::Library.OnDidEdit(
8686
}
8787

8888
[Fact]
89-
public async Task CustomConvertEvent()
89+
public void CustomConvertEvent()
9090
{
9191
var bus = CreateMessageBus();
9292
Library2::Library.Line? line = null;
9393

9494
bus.Observe<Library2::Library.OnDidDrawLine>()
9595
.Subscribe(e => line = e.Line);
9696

97-
await bus.NotifyAsync(new Library1::Library.OnDidDrawLine(new Library1::Library.Line(new Library1::Library.Point(1, 2), new Library1::Library.Point(3, 4))));
97+
bus.Notify(new Library1::Library.OnDidDrawLine(new Library1::Library.Line(new Library1::Library.Point(1, 2), new Library1::Library.Point(3, 4))));
9898

9999
Assert.NotNull(line);
100100
Assert.Equal(1, line.Start.X);

src/Merq.Tests/Merq.Tests.csproj

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
<PackageReference Include="xunit" />
2525
<PackageReference Include="xunit.runner.visualstudio" />
2626
<PackageReference Include="Microsoft.Reactive.Testing" />
27-
<PackageReference Include="Microsoft.CSharp" />
2827
<PackageReference Include="Scriban" />
2928
<PackageReference Include="SharpYaml" />
3029
<PackageReference Include="Microsoft.NETFramework.ReferenceAssemblies" />

src/Merq.Tests/MessageBusServiceSpec.cs

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public void given_external_producer_when_subscribing_to_base_then_notifies()
5858
}
5959

6060
[Fact]
61-
public async Task given_external_producer_then_can_notify_event()
61+
public void given_external_producer_then_can_notify_event()
6262
{
6363
var producer = new Subject<ConcreteEvent>();
6464

@@ -74,81 +74,81 @@ public async Task given_external_producer_then_can_notify_event()
7474

7575
bus.Observe<ConcreteEvent>().Subscribe(e => actual = e);
7676

77-
await bus.NotifyAsync(expected);
77+
bus.Notify(expected);
7878

7979
Assert.Equal(expected, actual);
8080
}
8181

8282
[Fact]
83-
public async Task when_subscribing_subject_then_succeeds()
83+
public void when_subscribing_subject_then_succeeds()
8484
{
8585
int? value = default;
8686
bus.Observe<int>().Subscribe(i => value = i);
8787

88-
await bus.NotifyAsync(42);
88+
bus.Notify(42);
8989

9090
Assert.Equal(42, value);
9191
}
9292

9393
[Fact]
94-
public async Task when_notifying_null_event_then_throws()
95-
=> await Assert.ThrowsAsync<ArgumentNullException>(async () => await bus.NotifyAsync<object?>(null));
94+
public void when_notifying_null_event_then_throws()
95+
=> Assert.Throws<ArgumentNullException>(() => bus.Notify<object?>(null));
9696

9797
[Fact]
98-
public async Task when_notifying_non_public_event_type_then_calls_subscriber()
98+
public void when_notifying_non_public_event_type_then_calls_subscriber()
9999
{
100100
var called = false;
101101

102102
bus.Observe<NonPublicEvent>().Subscribe(x => called = true);
103103

104-
await bus.NotifyAsync(new NonPublicEvent());
104+
bus.Notify(new NonPublicEvent());
105105

106106
Assert.True(called);
107107
}
108108

109109
[Fact]
110-
public async Task when_notifying_nested_non_public_event_type_then_calls_subscriber()
110+
public void when_notifying_nested_non_public_event_type_then_calls_subscriber()
111111
{
112112
var called = false;
113113

114114
bus.Observe<NestedEvent>().Subscribe(x => called = true);
115115

116-
await bus.NotifyAsync(new NestedEvent());
116+
bus.Notify(new NestedEvent());
117117

118118
Assert.True(called);
119119
}
120120

121121
[Fact]
122-
public async Task when_notifying_non_subscribed_event_then_does_not_call_subscriber()
122+
public void when_notifying_non_subscribed_event_then_does_not_call_subscriber()
123123
{
124124
var called = false;
125125

126126
using var subs = bus.Observe<ConcreteEvent>().Subscribe(c => called = true);
127127

128-
await bus.NotifyAsync(new AnotherEvent());
128+
bus.Notify(new AnotherEvent());
129129

130130
Assert.False(called);
131131
}
132132

133133
[Fact]
134-
public async Task when_notifying_subscribed_event_using_base_type_then_calls_derived_subscriber()
134+
public void when_notifying_subscribed_event_using_base_type_then_calls_derived_subscriber()
135135
{
136136
var called = false;
137137
using var subscription = bus.Observe<ConcreteEvent>().Subscribe(c => called = true);
138138

139139
BaseEvent e = new ConcreteEvent();
140-
await bus.NotifyAsync(e);
140+
bus.Notify(e);
141141

142142
Assert.True(called);
143143
}
144144

145145
[Fact]
146-
public async Task when_subscribing_as_event_interface_then_calls_subscriber()
146+
public void when_subscribing_as_event_interface_then_calls_subscriber()
147147
{
148148
var called = false;
149149
using var subs = bus.Observe<IBaseEvent>().Subscribe(c => called = true);
150150

151-
await bus.NotifyAsync(new ConcreteEvent());
151+
bus.Notify(new ConcreteEvent());
152152

153153
Assert.True(called);
154154
}

src/Merq.Tests/MessageBusSpec.cs

Lines changed: 20 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ public void given_external_producer_then_can_notify_event()
129129
public void given_external_producer_then_can_notify_eventAsync()
130130
{
131131
*/
132-
public async Task given_external_producer_then_can_notify_eventAsync()
132+
public void given_external_producer_then_can_notify_eventAsync()
133133
{
134134
var producer = new Subject<ConcreteEvent>();
135135

@@ -143,81 +143,81 @@ public async Task given_external_producer_then_can_notify_eventAsync()
143143

144144
bus.Observe<ConcreteEvent>().Subscribe(e => actual = e);
145145

146-
await bus.NotifyAsync(expected);
146+
bus.Notify(expected);
147147

148148
Assert.Equal(expected, actual);
149149
}
150150

151151
[Fact]
152-
public async Task when_subscribing_subject_then_succeedsAsync()
152+
public void when_subscribing_subject_then_succeedsAsync()
153153
{
154154
int? value = default;
155155
bus.Observe<int>().Subscribe(i => value = i);
156156

157-
await bus.NotifyAsync(42);
157+
bus.Notify(42);
158158

159159
Assert.Equal(42, value);
160160
}
161161

162162
[Fact]
163-
public Task when_notifying_null_event_then_throws()
164-
=> Assert.ThrowsAsync<ArgumentNullException>(async () => await bus.NotifyAsync<object?>(null));
163+
public void when_notifying_null_event_then_throws()
164+
=> Assert.Throws<ArgumentNullException>(() => bus.Notify<object?>(null));
165165

166166
[Fact]
167-
public async Task when_notifying_non_public_event_type_then_calls_subscriber()
167+
public void when_notifying_non_public_event_type_then_calls_subscriber()
168168
{
169169
var called = false;
170170

171171
bus.Observe<NonPublicEvent>().Subscribe(x => called = true);
172172

173-
await bus.NotifyAsync(new NonPublicEvent());
173+
bus.Notify(new NonPublicEvent());
174174

175175
Assert.True(called);
176176
}
177177

178178
[Fact]
179-
public async Task when_notifying_nested_non_public_event_type_then_calls_subscriber()
179+
public void when_notifying_nested_non_public_event_type_then_calls_subscriber()
180180
{
181181
var called = false;
182182

183183
bus.Observe<NestedEvent>().Subscribe(x => called = true);
184184

185-
await bus.NotifyAsync(new NestedEvent());
185+
bus.Notify(new NestedEvent());
186186

187187
Assert.True(called);
188188
}
189189

190190
[Fact]
191-
public async Task when_notifying_non_subscribed_event_then_does_not_call_subscriber()
191+
public void when_notifying_non_subscribed_event_then_does_not_call_subscriber()
192192
{
193193
var called = false;
194194

195195
using var subs = bus.Observe<ConcreteEvent>().Subscribe(c => called = true);
196196

197-
await bus.NotifyAsync(new AnotherEvent());
197+
bus.Notify(new AnotherEvent());
198198

199199
Assert.False(called);
200200
}
201201

202202
[Fact]
203-
public async Task when_notifying_subscribed_event_using_base_type_then_calls_derived_subscriber()
203+
public void when_notifying_subscribed_event_using_base_type_then_calls_derived_subscriber()
204204
{
205205
var called = false;
206206
using var subscription = bus.Observe<ConcreteEvent>().Subscribe(c => called = true);
207207

208208
BaseEvent e = new ConcreteEvent();
209-
await bus.NotifyAsync(e);
209+
bus.Notify(e);
210210

211211
Assert.True(called);
212212
}
213213

214214
[Fact]
215-
public async Task when_subscribing_as_event_interface_then_calls_subscriber()
215+
public void when_subscribing_as_event_interface_then_calls_subscriber()
216216
{
217217
var called = false;
218218
using var subs = bus.Observe<IBaseEvent>().Subscribe(c => called = true);
219219

220-
await bus.NotifyAsync(new ConcreteEvent());
220+
bus.Notify(new ConcreteEvent());
221221

222222
Assert.True(called);
223223
}
@@ -491,19 +491,15 @@ public void when_executing_nested_public_command_then_invokes_handler()
491491
bus.Execute(command);
492492

493493
handler.Verify(x => x.Execute(command));
494-
495-
bus.Observe<ConcreteEvent>()
496-
.Select(value => Observable.FromAsync(async () => await OnSolutionOpened(value)))
497-
.Subscribe();
498494
}
499495

500-
async Task OnSolutionOpened(ConcreteEvent e)
496+
void OnSolutionOpened(ConcreteEvent e)
501497
{
502498
// do something, perhaps execute some command?
503499
// bus.Execute(new MyCommand("Hello World"));
504500

505501
// perhaps raise further events?
506-
await bus.NotifyAsync(new ConcreteEvent());
502+
bus.Notify(new ConcreteEvent());
507503
}
508504

509505
[Fact]
@@ -543,7 +539,7 @@ public async Task when_executing_non_public_àsynccommand_result_then_invokes_ha
543539
}
544540

545541
[Fact]
546-
public async Task when_notifying_can_access_event_from_activity_stop()
542+
public void when_notifying_can_access_event_from_activity_stop()
547543
{
548544
Activity? activity = default;
549545
using var listener = new ActivityListener
@@ -559,7 +555,7 @@ public async Task when_notifying_can_access_event_from_activity_stop()
559555

560556
ActivitySource.AddActivityListener(listener);
561557

562-
await bus.NotifyAsync(new ConcreteEvent());
558+
bus.Notify(new ConcreteEvent());
563559

564560
Assert.NotNull(activity);
565561
Assert.IsType<ConcreteEvent>(activity.GetCustomProperty("Event"));

0 commit comments

Comments
 (0)