Skip to content

Instantly share code, notes, and snippets.

@Maarten88
Last active April 6, 2017 00:45
Show Gist options
  • Save Maarten88/dc6f0392ec9a50e12a754d8b6540d6a7 to your computer and use it in GitHub Desktop.
Save Maarten88/dc6f0392ec9a50e12a754d8b6540d6a7 to your computer and use it in GitHub Desktop.
public async Task StartCounterTimer()
{
if (this.timer != null)
throw new Exception("Can't start: already started");
await this.Dispatch(new StartCounterAction());
await this.WriteStateAsync();
this.timer = this.RegisterTimer(async (state) => {
var action = new IncrementCounterAction();
// This sends the action to the clients for processing there
// This processes the action here on the server, and also sends the syncstate to make sure the outcome is the same
// The order of events is important here
await this.Dispatch(action);
await this.WriteStateAsync();
await this.actionsToClientStream.OnNextAsync(action);
}, null, TimeSpan.FromSeconds(3), TimeSpan.FromSeconds(3));
await this.actionsToClientStream.OnNextAsync(new CounterStartedAction());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment