Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@NMZivkovic
Created September 28, 2017 10:49
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 NMZivkovic/aade390ce4b27fa0b1ae9470e0e29a2b to your computer and use it in GitHub Desktop.
Save NMZivkovic/aade390ce4b27fa0b1ae9470e0e29a2b to your computer and use it in GitHub Desktop.
public class UserActor : ReceiveActor
{
private Stopwatch _stopwatch;
private bool _isAlreadyReading;
public UserActor()
{
_stopwatch = new Stopwatch();
Receive<StartedReadingMessage>(message => ReceivedStartMessage(message));
Receive<StopedReadingMessage>(message => ReceivedStopMessage(message));
}
private void ReceivedStartMessage(StartedReadingMessage message)
{
if (_isAlreadyReading)
throw new InvalidOperationException("User is already reading another article!");
_stopwatch.Start();
_isAlreadyReading = true;
}
private void ReceivedStopMessage(StopedReadingMessage message)
{
if (!_isAlreadyReading)
throw new InvalidOperationException("User was not reading any article!");
_stopwatch.Stop();
_isAlreadyReading = false;
Context.ActorSelection("../../reporting").Tell(new ReportMessage(message.User, message.Article, _stopwatch.ElapsedMilliseconds));
_stopwatch.Reset();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment