예제 #1
0
        /// <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);
        }
예제 #2
0
        /// <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);
        }
예제 #3
0
        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
            });
        }
예제 #4
0
 public void CreateFutureItem(FutureItem item)
 {
     _futureItems.Add(item);
 }