/// <summary> /// Dequeue an item from the queue. The queue is single consumer, DequeueAsync must not be called again until the previous dequeue operation have completed. /// Warning: The returned IFutureItem must not be awaited more than once. /// </summary> /// <returns>A future of the dequeued item.</returns> public IFutureItem DequeueAsync() { FutureItem futureItem = new FutureItem(); lock (_queue) { if (_pendingDequeue != null) { throw new InvalidOperationException("Dequeue operation is already in progress. This queue is single consumer."); } if (_isDisposed) { futureItem.SetCanceled(); } else if (_queue.Count > 0) { T value = _queue.Dequeue(); futureItem.SetItem(value); } else if (_addingCompleted) { futureItem.SetCanceled(); } else { _pendingDequeue = futureItem; } } return(futureItem); }
/// <summary> /// Add an item to the queue. Can be called concurrently. /// </summary> /// <returns>True if the item was enqueued, otherwise False.</returns> public bool Enqueue(T item) { FutureItem completedWaiter = null; lock (_queue) { if (_addingCompleted) { return(false); } if (_pendingDequeue != null) { completedWaiter = _pendingDequeue; _pendingDequeue = null; } else { _queue.Enqueue(item); } } completedWaiter?.SetItem(item, _dispatcher); return(true); }
public async Task <FutureItemDto> Handle(CreateUserFutureItemCommand request, CancellationToken cancellationToken) { var user = await _userRepository.GetUserDetailsAsync(request.UserId); var futureItem = new FutureItem(Guid.NewGuid(), request.Name, request.StorageUri, request.ItemType, request.IsActive); user.CreateFutureItem(futureItem); var result = await _userRepository.CommitAsync(); return(new FutureItemDto { Id = futureItem.Id, Name = futureItem.Name, ItemType = futureItem.ItemType, StorageUri = futureItem.StorageUri, IsActive = request.IsActive }); }
public void CreateFutureItem(FutureItem item) { _futureItems.Add(item); }