private async Task StartDequeuingMarkedItems(MarkedDelegate onSuccess = null, MarkedDelegate onFailure = null) { try { if (_markedMessagesQueue.Count > 0) { ProductMarkChangedMessage msg = null; lock (_markedMessagesQueue) { msg = _markedMessagesQueue.Dequeue(); } var succeeded = await MarkProductAsFavorite(msg.Model.GroupId, msg.Model.Id, msg.Marked); if (succeeded) { _previuosTryWasFailure = false; onSuccess?.Invoke(msg.Model.GroupId, msg.Model.Id, msg.Marked); } else { onFailure?.Invoke(msg.Model.GroupId, msg.Model.Id, msg.Marked); if (_previuosTryWasFailure) { foreach (var queuedMsg in _markedMessagesQueue) { onFailure?.Invoke(queuedMsg.Model.GroupId, queuedMsg.Model.Id, queuedMsg.Marked); } _markedMessagesQueue = new Queue <ProductMarkChangedMessage>(); _markedMessagesDequeuingStarted = false; } _previuosTryWasFailure = true; } await StartDequeuingMarkedItems(onSuccess, onFailure); } else { _markedMessagesDequeuingStarted = false; } } catch (Exception ex) { OnException(ex); } }
public void MarkProductAsFavorite(ProductMarkChangedMessage msg, MarkedDelegate onSuccess = null, MarkedDelegate onFailure = null) { lock (_markedMessagesQueue) { if (!_markedMessagesQueue.Any(x => (!x.Model.GroupId.IsNullOrEmpty() && x.Model.GroupId == msg.Model.GroupId) || x.Model.Id == msg.Model.Id)) { _markedMessagesQueue.Enqueue(msg); } } if (!_markedMessagesDequeuingStarted) { _markedMessagesDequeuingStarted = true; Task.Run(() => StartDequeuingMarkedItems(onSuccess, onFailure)); } }