public void CollectingTripData(int passengerCount, Trip trip, Route route) { //State dependencies passed directly in, no question of when they are available for use //Closures for local state so that their lifetime is the same as the encompassing state var stopTimes = new List <StopTime>(); int updatedPassengerCount = passengerCount; Receive <VehicleRouteStatus>(msg => { if (msg.State == VehicleRouteState.NoRoute) { var tripExecution = new TripExecution(trip.TripId, this.SumDistances(route, stopTimes)); Context.System.EventStream.Publish(tripExecution); //We declaratively carry forward the passenger count //In the previous example its not clear whether we are //keeping this value or have a stale state bug Become(() => WaitingForTrip(updatedPassengerCount)); } }); Receive <StopTime>(msg => { stopTimes.Add(msg); updatedPassengerCount = updatedPassengerCount + msg.PassengerOn - msg.PassengerOff; if (updatedPassengerCount < 0) { updatedPassengerCount = 0; } }); }
public ScheduleAdherenceActor1( ITripClient tripClient, IRouteClient routeClient) { this.tripClient = tripClient; this.routeClient = routeClient; ReceiveAsync <VehicleRouteStatus>(async msg => { if (msg.State == VehicleRouteState.OnRoute) { this.stopTimes = new List <StopTime>(); if (msg.TripId.IsSome()) { this.trip = await this.tripClient.GetTrip(msg.TripId.Value); this.route = await this.routeClient.GetRoute(trip.RouteId); } } else if (msg.State == VehicleRouteState.NoRoute) { if (this.trip == null) { return; } var tripExecution = new TripExecution(this.trip.TripId, this.SumDistances()); Context.System.EventStream.Publish(tripExecution); } }); Receive <StopTime>(msg => { this.stopTimes.Add(msg); this.passengerCount = this.passengerCount + msg.PassengerOn - msg.PassengerOff; if (this.passengerCount < 0) { this.passengerCount = 0; } }); }