Skip to content

Commit

Permalink
Updated documentation and sample project
Browse files Browse the repository at this point in the history
  • Loading branch information
TheDusty01 committed May 12, 2022
1 parent 3a333b9 commit bff66c9
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 9 deletions.
36 changes: 29 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,16 @@ The ``AsyncEventHandler`` type is completely thread-safe, no matter how many ev

Declaring an event:
```cs
private readonly AsyncEventHandler<AsyncEventArgs> myEvent = new AsyncEventHandler<AsyncEventArgs>();
public event AsyncEvent<AsyncEventArgs> MyEvent
{
add { myEvent.Register(value); }
remove { myEvent.Unregister(value); }
}
```

You can also just expose the ``AsyncEventHandler`` struct directly but that way it is not treated as an C# event and subscribers outside of your class can even invoke your event.
```cs
public AsyncEventHandler<AsyncEventArgs> MyEvent = new AsyncEventHandler();
public AsyncEventHandler<AsyncEventArgs> AnotherEvent; // You can even do this!
Expand All @@ -23,22 +33,33 @@ public class MyAsyncEventArgs : AsyncEventArgs
public string? Message { get; set; }
}

public AsyncEventHandlerDelegate<MyAsyncEventArgs> MyCustomEvent = new AsyncEventHandler<MyAsyncEventArgs>();
public AsyncEventHandler<MyAsyncEventArgs> MyCustomEvent = new AsyncEventHandler<MyAsyncEventArgs>();
```

Registering/Unregistering to the event:
```cs
class WebsocketServer
{
public AsyncEventHandler<AsyncEventArgs> MyEvent = new AsyncEventHandler<AsyncEventArgs>();
public AsyncEventHandler<AsyncEventArgs> MyExposedEvent = new AsyncEventHandler<AsyncEventArgs>();

private readonly AsyncEventHandler<AsyncEventArgs> myEvent = new AsyncEventHandler<AsyncEventArgs>();
public event AsyncEvent<AsyncEventArgs> MyEvent
{
add { myEvent.Register(value); }
remove { myEvent.Unregister(value); }
}
}

var ws = new WebsocketServer();
// Event property
ws.MyEvent += Ws_MyEvent; // Register
ws.MyEvent -= Ws_MyEvent; // Unregister
// Using the struct directly
ws.MyExposedEvent += Ws_MyEvent; // Register
ws.MyExposedEvent -= Ws_MyEvent; // Unregister
// Or use the methods
ws.MyEvent.Register(Ws_MyEvent);
ws.MyEvent.Unregister(Ws_MyEvent);
ws.MyExposedEvent.Register(Ws_MyEvent);
ws.MyExposedEvent.Unregister(Ws_MyEvent);

static Task Ws_MyEvent(object? sender, AsyncEventArgs e)
{
Expand All @@ -51,14 +72,15 @@ static Task Ws_MyEvent(object? sender, AsyncEventArgs e)
}
```

Invoking the event:
Invoking the event: \
**Note:** Always call ``InvokeAsync`` on the ``AsyncEventHandler`` struct itself, not on the event!
```cs
try
{
await MyEvent.InvokeAsync(this, new AsyncEventArgs());
await myEvent.InvokeAsync(this, new AsyncEventArgs());
// or with a cancellation token
var cts = new CancellationTokenSource();
await MyEvent.InvokeAsync(this, new AsyncEventArgs(), cts.Token);
await myEvent.InvokeAsync(this, new AsyncEventArgs(), cts.Token);
}
catch (OperationCanceledException)
{
Expand Down
9 changes: 7 additions & 2 deletions Samples/Type/FakeWebsocketServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,12 @@ public class ClientConnectedAsyncEventArgs : AsyncEventArgs
public class FakeWebsocketServer
{
public AsyncEventHandler<AsyncEventArgs> Started = default;
public AsyncEventHandler<ClientConnectedAsyncEventArgs> ClientConnected = new AsyncEventHandler<ClientConnectedAsyncEventArgs>();
private readonly AsyncEventHandler<ClientConnectedAsyncEventArgs> clientConnected = new AsyncEventHandler<ClientConnectedAsyncEventArgs>();
public event AsyncEvent<ClientConnectedAsyncEventArgs> ClientConnected
{
add { clientConnected.Register(value); }
remove { clientConnected.Unregister(value); }
}
public AsyncEventHandler<MessageAsyncEventArgs> MessageReceived = new AsyncEventHandler<MessageAsyncEventArgs>();

public void Run(CancellationToken cancellationToken)
Expand All @@ -40,7 +45,7 @@ private async Task SimulateTest(CancellationToken cancellationToken)

// Simulate client connecting
await Task.Delay(1000);
await ClientConnected.InvokeAsync(this, new ClientConnectedAsyncEventArgs { ClientId = 1 }, cancellationToken);
await clientConnected.InvokeAsync(this, new ClientConnectedAsyncEventArgs { ClientId = 1 }, cancellationToken);

// Simulate client message
await Task.Delay(1000);
Expand Down

0 comments on commit bff66c9

Please sign in to comment.