Skip to content
This repository has been archived by the owner on Jul 21, 2022. It is now read-only.

Commit

Permalink
Simplify waiting for a single event.
Browse files Browse the repository at this point in the history
Add method to EventEmitter that allows you to await a single event type
to be raised. No more dealing with TaskCompletionSource and setting
event handlers.
  • Loading branch information
vegardlarsen committed Jan 18, 2014
1 parent ef4c711 commit e33d014
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 16 deletions.
30 changes: 30 additions & 0 deletions Pusher/EventEmitter.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace Pusher
{
Expand Down Expand Up @@ -46,5 +47,34 @@ public void TryConvertEvent(object sender, IIncomingEvent e)
}
}
}

/// <summary>
/// Wait for a single incoming event of a certain type.
/// </summary>
/// <typeparam name="TResult">The type of the value you want to extract from the event</typeparam>
/// <typeparam name="TEventArgs">The expected event type arguments</typeparam>
/// <param name="resultDelegate">A function that produces the result from the event data.</param>
/// <returns>The return value produced by resultDelegate</returns>
protected async Task<TResult> WaitForSingleEventAsync<TResult, TEventArgs>(
Func<object, IIncomingEvent<TEventArgs>, TResult> resultDelegate)
{
var completionSource = new TaskCompletionSource<TResult>();
GenericEventEmittedHandler<TEventArgs> eventHandler =
(sender, e) => completionSource.SetResult(resultDelegate(sender, e));
var eventSubscription = GetEventSubscription<TEventArgs>();
eventSubscription.EventEmitted += eventHandler;
var result = await completionSource.Task;
eventSubscription.EventEmitted -= eventHandler;
return result;
}

/// <summary>
/// Wait for a single event of a certain type.
/// </summary>
/// <returns>An empty task.</returns>
protected Task WaitForSingleEventAsync<TEventArgs>()
{
return WaitForSingleEventAsync<object, TEventArgs>((sender, e) => null);
}
}
}
20 changes: 4 additions & 16 deletions Pusher/Pusher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,14 +77,8 @@ public async Task ConnectAsync()
ApplicationKey)));
_connection.OnData += ReceivedEvent;
await _connection.Open();

// wait for the connection established event before returning
var completionSource = new TaskCompletionSource<string>();
GenericEventEmittedHandler<ConnectionEstablishedEventArgs> eventHandler =
(sender, e) => completionSource.SetResult(e.DataObject.SocketId);
GetEventSubscription<ConnectionEstablishedEventArgs>().EventEmitted += eventHandler;
await completionSource.Task;
GetEventSubscription<ConnectionEstablishedEventArgs>().EventEmitted -= eventHandler;

await WaitForSingleEventAsync<ConnectionEstablishedEventArgs>();
}

public void AddContract(IEventContract contract)
Expand All @@ -104,14 +98,8 @@ private async Task<string> GetSocketIdAsync()
return SocketId;
}

// wait for ConnectionEstablishedEvent, and use socket ID
var completionSource = new TaskCompletionSource<string>();
GenericEventEmittedHandler<ConnectionEstablishedEventArgs> eventHandler =
(sender, e) => completionSource.SetResult(e.DataObject.SocketId);
GetEventSubscription<ConnectionEstablishedEventArgs>().EventEmitted += eventHandler;
await completionSource.Task;
GetEventSubscription<ConnectionEstablishedEventArgs>().EventEmitted -= eventHandler;
return completionSource.Task.Result;
return await WaitForSingleEventAsync<string, ConnectionEstablishedEventArgs>(
(sender, e) => e.DataObject.SocketId);
}

private void ReceivedEvent(object sender, DataReceivedEventArgs dataReceivedEventArgs)
Expand Down

0 comments on commit e33d014

Please sign in to comment.