Example #1
0
            static CanceledUniTaskCache()
            {
                var promise = new UniTaskCompletionSource <T>();

                promise.TrySetCanceled(CancellationToken.None);
                promise.MarkHandled();
                Task = promise.Task;
            }
            public FirstValueToUniTaskObserver(UniTaskCompletionSource <T> promise, SingleAssignmentDisposable disposable, CancellationToken cancellationToken)
            {
                this.promise           = promise;
                this.disposable        = disposable;
                this.cancellationToken = cancellationToken;

                if (this.cancellationToken.CanBeCanceled)
                {
                    this.registration = this.cancellationToken.RegisterWithoutCaptureExecutionContext(callback, this);
                }
            }
        public static (UniTask, CancellationTokenRegistration) ToUniTask(this CancellationToken cancellationToken)
        {
            if (cancellationToken.IsCancellationRequested)
            {
                return(UniTask.FromCanceled(cancellationToken), default(CancellationTokenRegistration));
            }

            var promise = new UniTaskCompletionSource();

            return(promise.Task, cancellationToken.RegisterWithoutCaptureExecutionContext(cancellationTokenCallback, promise));
        }
Example #4
0
 public static UniTask <T> FromCanceled <T>(CancellationToken cancellationToken = default)
 {
     if (cancellationToken == CancellationToken.None)
     {
         return(CanceledUniTaskCache <T> .Task);
     }
     else
     {
         var promise = new UniTaskCompletionSource <T>();
         promise.TrySetCanceled(cancellationToken);
         promise.MarkHandled();
         return(promise.Task);
     }
 }
Example #5
0
        public static UniTask <T> ToUniTask <T>(this IObservable <T> source, bool useFirstValue = false,
                                                CancellationToken cancellationToken             = default)
        {
            var promise    = new UniTaskCompletionSource <T>();
            var disposable = new SingleAssignmentDisposable();

            var observer = useFirstValue
                ? (IObserver <T>) new FirstValueToUniTaskObserver <T>(promise, disposable, cancellationToken)
                : (IObserver <T>) new ToUniTaskObserver <T>(promise, disposable, cancellationToken);

            try {
                disposable.Disposable = source.Subscribe(observer);
            }
            catch (Exception ex) {
                promise.TrySetException(ex);
            }

            return(promise.Task);
        }
Example #6
0
        internal AsyncLazy(UniTask task)
        {
            this.taskFactory      = null;
            this.completionSource = new UniTaskCompletionSource();
            this.syncLock         = null;
            this.initialized      = true;

            var awaiter = task.GetAwaiter();

            if (awaiter.IsCompleted)
            {
                SetCompletionSource(awaiter);
            }
            else
            {
                this.awaiter = awaiter;
                awaiter.SourceOnCompleted(continuation, this);
            }
        }