Skip to content

Commit

Permalink
Enable automatic children retrieval
Browse files Browse the repository at this point in the history
References #21
  • Loading branch information
andreashuber-lawo committed Apr 29, 2016
1 parent c0a0a4c commit def7512
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 29 deletions.
37 changes: 21 additions & 16 deletions Lawo.EmberPlusSharp/Model/Consumer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,21 +97,7 @@ public Task SendChangesAsync()
/// <see cref="ChildrenRetrievalPolicy.None"/>.</remarks>
public async Task SendAsync()
{
if (this.root.HasChanges)
{
MemoryStream stream;

// TODO: Reuse MemoryStream and EmberWriter for all outgoing messages.
using (stream = new MemoryStream())
using (var writer = new EmberWriter(stream))
{
this.root.WriteChanges(writer, this.pendingInvocations);
}

await this.client.SendMessageAsync(this.emberDataMessage, stream.ToArray());
}

if (await this.SendRequestAsync())
if (await this.SendCoreAsync())
{
await this.isVerifiedSource.Task;
}
Expand Down Expand Up @@ -257,6 +243,25 @@ private Consumer(S101Client client, int timeout, ChildrenRetrievalPolicy childre
this.client.ConnectionLost += this.receiveQueue.OnConnectionLost;
}

private async Task<bool> SendCoreAsync()
{
if (this.root.HasChanges)
{
MemoryStream stream;

// TODO: Reuse MemoryStream and EmberWriter for all outgoing messages.
using (stream = new MemoryStream())
using (var writer = new EmberWriter(stream))
{
this.root.WriteChanges(writer, this.pendingInvocations);
}

await this.client.SendMessageAsync(this.emberDataMessage, stream.ToArray());
}

return await this.SendRequestAsync();
}

private void CancelAutoSendDelay()
{
if (!this.autoSendDelayCancellationSource.IsCancellationRequested)
Expand Down Expand Up @@ -307,7 +312,7 @@ private async void SendReceiveLoop()
if (await Task.WhenAny(localTask, providerTask) == localTask)
{
await localTask;
await this.SendAsync();
await this.SendCoreAsync();
localTask = this.WaitForLocalChangesAsync();
}
else
Expand Down
8 changes: 2 additions & 6 deletions Lawo.EmberPlusSharp/Model/Element.cs
Original file line number Diff line number Diff line change
Expand Up @@ -154,14 +154,10 @@ internal virtual RetrievalState RetrievalState
set { } // Intentionally empty
}

internal void ResetRetrievalState()
internal virtual void ResetRetrievalState()
{
this.RetrievalState = RetrievalState.None;

if (this.parent != null)
{
this.parent.ResetRetrievalState();
}
this.parent.ResetRetrievalState();
}

internal virtual void SetContext(Context context)
Expand Down
24 changes: 17 additions & 7 deletions Lawo.EmberPlusSharp/Model/Root.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,7 @@ void IParent.SetHasChanges()
if (!this.HasChanges)
{
this.HasChanges = true;

var handler = this.HasChangesSet;

if (handler != null)
{
handler(this, EventArgs.Empty);
}
this.RaiseHasChangesSet();
}
}

Expand Down Expand Up @@ -153,6 +147,12 @@ internal sealed override void WriteChanges(EmberWriter writer, IInvocationCollec
}
}

internal sealed override void ResetRetrievalState()
{
this.RetrievalState = RetrievalState.None;
this.RaiseHasChangesSet();
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////

/// <summary>Initializes a new instance of the <see cref="Root{TMostDerived}"/> class.</summary>
Expand All @@ -166,6 +166,16 @@ protected Root()

////////////////////////////////////////////////////////////////////////////////////////////////////////////////

private void RaiseHasChangesSet()
{
var handler = this.HasChangesSet;

if (handler != null)
{
handler(this, EventArgs.Empty);
}
}

private void ReadQualifiedChild(EmberReader reader, ElementType actualType)
{
reader.ReadAndAssertOuter(GlowQualifiedNode.Path.OuterId);
Expand Down

0 comments on commit def7512

Please sign in to comment.