Skip to content

Instantly share code, notes, and snippets.

@mikeminutillo
Created March 30, 2011 03:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mikeminutillo/893814 to your computer and use it in GitHub Desktop.
Save mikeminutillo/893814 to your computer and use it in GitHub Desktop.
A reusable component designed to allow only the most recent requests to update the UI
public interface ISequencerToken
{
bool IsCurrent { get; }
}
public class Sequencer
{
private long _currentToken = 0;
private long Bump()
{
return Interlocked.Increment(ref _currentToken);
}
public ISequencerToken GetToken()
{
return new Token(this);
}
private class Token : ISequencerToken
{
private readonly long _val;
private readonly Sequencer _sequencer;
public Token(Sequencer sequencer)
{
_sequencer = sequencer;
_val = _sequencer.Bump();
}
public bool IsCurrent
{
get { return _sequencer._currentToken == _val; } }
}
}
class SomeViewModel
{
private readonly Sequencer _seq = new Sequencer();
public async void DoStuff()
{
var token = _seq.GetToken();
var results = await _someService.GetResults();
if(token.IsCurrent)
{
UI.UpdateWith(results);
}
}
}
@mikeminutillo
Copy link
Author

So if you fire DoStuff() 1000 times only the last one will update the UI

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment