Skip to content

Commit f9842ee

Browse files
committed
Ensuring that RunListener/RunListenerAsync blocks until it has initialised.
1 parent 0c181c4 commit f9842ee

File tree

1 file changed

+8
-0
lines changed

1 file changed

+8
-0
lines changed

PowerSync/PowerSync.Common/Utils/EventStream.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,16 +53,20 @@ public CancellationTokenSource RunListenerAsync(
5353
Func<T, Task> callback)
5454
{
5555
var cts = new CancellationTokenSource();
56+
var started = new TaskCompletionSource<bool>();
5657

5758
_ = Task.Run(async () =>
5859
{
60+
started.SetResult(true);
5961
await foreach (var value in ListenAsync(cts.Token))
6062
{
6163
await callback(value);
6264
}
6365

6466
}, cts.Token);
6567

68+
started.Task.GetAwaiter().GetResult();
69+
6670
return cts;
6771
}
6872

@@ -76,15 +80,19 @@ public IAsyncEnumerable<T> ListenAsync(CancellationToken cancellationToken)
7680
public CancellationTokenSource RunListener(Action<T> callback)
7781
{
7882
var cts = new CancellationTokenSource();
83+
var started = new TaskCompletionSource<bool>();
7984

8085
_ = Task.Run(() =>
8186
{
87+
started.SetResult(true);
8288
foreach (var value in Listen(cts.Token))
8389
{
8490
callback(value);
8591
}
8692
}, cts.Token);
8793

94+
started.Task.GetAwaiter().GetResult();
95+
8896
return cts;
8997
}
9098

0 commit comments

Comments
 (0)