/// <summary> /// This method is started by the first message of a batch and waits for a specified amount of time before forwarding the entire batch. /// </summary> /// <param name="message">The first message</param> /// <returns></returns> private async Task StartCollect(TypedModelUpdate message) { lock (_batchLock) { _batch.AddOrUpdate(message.Property, message, (key, old) => (old.TimestampUtc > message.TimestampUtc) ? (old) : (message)); } await Task.Delay(_delayMilliseconds); lock (_onMessageModeLock) { lock (_batchLock) { SendAndClearBag(); } _isCollectionMode = false; } }
/// <summary> /// Receives a ModelUpdate for delivery after the current waiting delay has completed. /// ModelUpdates will NOT be sent out if they are superseded by new updates for the same property with greater TimestampUtc /// </summary> /// <param name="mu"></param> public void OnMessage(TypedModelUpdate message) { lock (_onMessageModeLock) { if (_isCollectionMode) { lock (_batchLock) { _batch.AddOrUpdate(message.Property, message, (key, old) => (old.TimestampUtc > message.TimestampUtc) ? (old) : (message)); } } else { _isCollectionMode = true; _collectionWaitTask = StartCollect(message); } } }