Skip to content

Commit ac3b514

Browse files
committed
Add dispatch synchronization
1 parent 3f997bf commit ac3b514

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

Abies/Types.cs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.Runtime.CompilerServices;
44
using System.Runtime.InteropServices;
55
using System.Runtime.InteropServices.JavaScript;
6+
using System.Threading;
67
using Abies;
78
using Abies.DOM;
89

@@ -164,6 +165,7 @@ public record Program<TApplication, TArguments, TModel> : Program
164165
private Node? _dom;
165166
// todo: clean up handlers when they are no longer needed
166167
private readonly ConcurrentDictionary<string, Message> _handlers = new();
168+
private readonly SemaphoreSlim _dispatchLock = new(1, 1);
167169

168170
public async Task Run(TArguments arguments)
169171
{
@@ -212,6 +214,9 @@ private void RegisterHandlers(Node node)
212214

213215
public async Task Dispatch(Message message)
214216
{
217+
await _dispatchLock.WaitAsync();
218+
try
219+
{
215220
if (model is null)
216221
{
217222
await Interop.WriteToConsole("Model not initialized");
@@ -264,7 +269,14 @@ public async Task Dispatch(Message message)
264269
{
265270
await HandleCommand(command);
266271
}
267-
} private static async Task HandleCommand(Command command)
272+
}
273+
finally
274+
{
275+
_dispatchLock.Release();
276+
}
277+
}
278+
279+
private static async Task HandleCommand(Command command)
268280
{
269281
switch(command)
270282
{

0 commit comments

Comments
 (0)