Skip to content

Commit

Permalink
Merge pull request #97 from linksplatform/feature/add_handler_state_s…
Browse files Browse the repository at this point in the history
…truct

Add `(Write|Read)HandlerState` struct
  • Loading branch information
FreePhoenix888 authored Jan 13, 2022
2 parents a4b0e12 + 6d6b351 commit 3b047f5
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 0 deletions.
37 changes: 37 additions & 0 deletions csharp/Platform.Data/ReadHandlerState.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using System.Collections.Generic;
using Platform.Delegates;

namespace Platform.Data
{
public struct ReadHandlerState<TLink>
{
private readonly EqualityComparer<TLink> _equalityComparer;
public TLink Result;
public ReadHandler<TLink> Handler;
public TLink Continue;
public TLink Break;

public ReadHandlerState(TLink @continue, TLink @break, ReadHandler<TLink> handler)
{
_equalityComparer = EqualityComparer<TLink>.Default;
Continue = @continue;
Break = @break;
Result = @continue;
Handler = handler;
}

public void Apply(TLink result)
{
if (_equalityComparer.Equals(Break, Result))
{
return;
}
if (!_equalityComparer.Equals(Break, result))
{
return;
}
Handler = null;
Result = Break;
}
}
}
39 changes: 39 additions & 0 deletions csharp/Platform.Data/WriteHandlerState.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using System.Collections.Generic;
using Platform.Delegates;

namespace Platform.Data
{
public struct WriteHandlerState<TLink>
{
private readonly EqualityComparer<TLink> _equalityComparer;
public TLink Result;
public WriteHandler<TLink> Handler;
public TLink Continue;
public TLink Break;

public WriteHandlerState(TLink @continue, TLink @break, WriteHandler<TLink> handler)
{
_equalityComparer = EqualityComparer<TLink>.Default;
Continue = @continue;
Break = @break;
Result = @continue;
Handler = handler;
}

public void Apply(TLink result)
{
if (_equalityComparer.Equals(Break, Result))
{
return;
}
if (!_equalityComparer.Equals(Break, result))
{
return;
}
Handler = null;
Result = Break;
}
}
}


0 comments on commit 3b047f5

Please sign in to comment.