Created
March 30, 2011 03:27
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; } } | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
So if you fire DoStuff() 1000 times only the last one will update the UI