Skip to content

Commit

Permalink
Merge AutoSendLoop and ReceiveLoop into one method
Browse files Browse the repository at this point in the history
References #21
  • Loading branch information
andreashuber-lawo committed Apr 26, 2016
1 parent 8bce51e commit 7fe7989
Showing 1 changed file with 23 additions and 34 deletions.
57 changes: 23 additions & 34 deletions Lawo.EmberPlusSharp/Model/Consumer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -221,8 +221,7 @@ public static async Task<Consumer<TRoot>> CreateAsync(

var result = new Consumer<TRoot>(client, timeout, childrenRetrievalPolicy, slot);
await result.RetrieveChildrenAsync();
result.ReceiveLoop();
result.AutoSendLoop();
result.SendReceiveLoop();
return result;
}

Expand Down Expand Up @@ -284,58 +283,48 @@ private async Task RetrieveChildrenAsync()
await retrieveChildrenTask;
}

private async void ReceiveLoop()
private async void SendReceiveLoop()
{
Exception exception = null;

try
{
while (true)
{
await this.WaitForProviderChangesAsync();
this.ApplyProviderChanges();
await this.RetrieveChildrenAsync();
}
}
catch (OperationCanceledException)
{
}
catch (Exception ex)
{
exception = ex;
}
finally
{
this.OnConnectionLost(this, new ConnectionLostEventArgs(exception));
this.Dispose();
}
}

private async void AutoSendLoop()
{
this.autoSendDelayCancellationSource = new CancellationTokenSource();
this.hasChangesSetSource = new TaskCompletionSource<bool>();
this.root.HasChangesSet += this.OnHasChangesSet;

try
{
var localTask = this.WaitForLocalChangesAsync();
var providerTask = this.WaitForProviderChangesAsync();

while (true)
{
await this.WaitForLocalChangesAsync();
await this.SendAsync();
if (await Task.WhenAny(localTask, providerTask) == localTask)
{
await localTask;
await this.SendAsync();
localTask = this.WaitForLocalChangesAsync();
}
else
{
await providerTask;
this.ApplyProviderChanges();
await this.RetrieveChildrenAsync();
providerTask = this.WaitForProviderChangesAsync();
}
}

// TODO: Exceptions should be propagated to client code via an event similar to
// S101Client.ConnectionLost
}
catch (OperationCanceledException)
{
}
catch (ObjectDisposedException)
{
}
catch (Exception ex)
{
exception = ex;
}
finally
{
this.OnConnectionLost(this, new ConnectionLostEventArgs(exception));
this.CancelAutoSendDelay();
this.autoSendDelayCancellationSource.Dispose();
this.Dispose();
Expand Down

0 comments on commit 7fe7989

Please sign in to comment.