public void OnDestroy()
 {
     if (!_isDead)
     {
         _uts.TrySetCanceled();
     }
 }
Esempio n. 2
0
 public void OnDestroy()
 {
     taskCompletionSource.TrySetCanceled();
     taskCompletionSource = null;
     asyncFunction        = null;
     function             = null;
 }
Esempio n. 3
0
 public void CancelTask()
 {
     if (taskCompletionSource != null)
     {
         taskCompletionSource.TrySetCanceled();
         Destroy();
     }
 }
Esempio n. 4
0
		public void SetException(Exception exception) {
			if (promise == null) {
				promise = new UniTaskCompletionSource();
			}

			if (exception is OperationCanceledException ex) {
				promise.TrySetCanceled(ex);
			} else {
				promise.TrySetException(exception);
			}
		}
Esempio n. 5
0
 public void CanceledAllUTcs()
 {
     lock (packTcs)
     {
         foreach (var tcs in packTcs)
         {
             tcs.Value.TrySetCanceled();
         }
         packTcs.Clear();
         handshakeTcs.TrySetCanceled();
     }
 }
Esempio n. 6
0
        public async UniTask <bool> RestoreAsync(CancellationToken cancellationToken = default)
        {
#if UNITY_IOS
            CheckReady();
            var restoreTcs = new UniTaskCompletionSource <bool>();
            cancellationToken.Register(() => restoreTcs.TrySetCanceled());
            var apple = _extensionProvider.GetExtension <IAppleExtensions>();
            apple.RestoreTransactions(ret => restoreTcs.TrySetResult(ret));
            return(await restoreTcs.Task);
#else
            return(true);
#endif
        }
Esempio n. 7
0
 public void Cancel(bool isForce = false)
 {
     this.isForce = isForce;
     utcs.TrySetCanceled();
     if (socket.State != WebSocketState.Closed)
     {
         socket.Close();
     }
     if (protocol != null)
     {
         protocol.StopHeartbeat();
         protocol.CanceledAllUTcs();
     }
 }
Esempio n. 8
0
    private async UniTask RunAsyncInternal2()
    {
        var tcs = new UniTaskCompletionSource();

        tcs.TrySetCanceled();
        try
        {
            await tcs.Task;
        }
        finally
        {
            Debug.Log("RunAsyncInternal2 status= " + tcs.Task.Status);
        }
    }
Esempio n. 9
0
        public async Task SetFirst()
        {
            {
                var tcs = new UniTaskCompletionSource <int>();

                tcs.TrySetResult(10);
                var a = await tcs.Task; // ok.
                var b = await tcs.Task; // ok.
                a.Should().Be(10);
                b.Should().Be(10);
                tcs.Task.Status.Should().Be(UniTaskStatus.Succeeded);
            }

            {
                var tcs = new UniTaskCompletionSource <int>();

                tcs.TrySetException(new TestException());

                await Assert.ThrowsAsync <TestException>(async() => await tcs.Task);

                await Assert.ThrowsAsync <TestException>(async() => await tcs.Task);

                tcs.Task.Status.Should().Be(UniTaskStatus.Faulted);
            }

            var cts = new CancellationTokenSource();

            {
                var tcs = new UniTaskCompletionSource <int>();

                tcs.TrySetException(new OperationCanceledException(cts.Token));

                (await Assert.ThrowsAsync <OperationCanceledException>(async() => await tcs.Task)).CancellationToken.Should().Be(cts.Token);
                (await Assert.ThrowsAsync <OperationCanceledException>(async() => await tcs.Task)).CancellationToken.Should().Be(cts.Token);

                tcs.Task.Status.Should().Be(UniTaskStatus.Canceled);
            }

            {
                var tcs = new UniTaskCompletionSource <int>();

                tcs.TrySetCanceled(cts.Token);

                (await Assert.ThrowsAsync <OperationCanceledException>(async() => await tcs.Task)).CancellationToken.Should().Be(cts.Token);
                (await Assert.ThrowsAsync <OperationCanceledException>(async() => await tcs.Task)).CancellationToken.Should().Be(cts.Token);

                tcs.Task.Status.Should().Be(UniTaskStatus.Canceled);
            }
        }
Esempio n. 10
0
        private async UniTask Test()
        {
            UniTaskCompletionSource sc = new UniTaskCompletionSource();

            TimerManager.Schedule(0, 1, 1, delegate(Timer timer)
            {
                sc.TrySetCanceled();
            });

            TimerManager.Schedule(0, 3, 1, delegate(Timer timer)
            {
                sc.TrySetResult();
            });

            await sc.Task;
        }
        public static UniTask WaitForExitAsync(this Process process, CancellationToken cancellationToken = default(CancellationToken))
        {
            if (process.HasExited)
            {
                return(UniTask.CompletedTask);
            }

            var tcs = new UniTaskCompletionSource <object>();

            process.EnableRaisingEvents = true;
            process.Exited += (sender, args) => tcs.TrySetResult(null);
            if (cancellationToken != default(CancellationToken))
            {
                cancellationToken.Register(() => tcs.TrySetCanceled());
            }
            return(process.HasExited ? UniTask.CompletedTask : tcs.Task);
        }
Esempio n. 12
0
        public UniTask <TileId> OnClickTileAsync(CancellationToken cancellation = default)
        {
            var completion = new UniTaskCompletionSource <TileId>();

            TileClicked += Handler;
            cancellation.Register(() =>
            {
                completion.TrySetCanceled();
            });
            return(completion.Task);

            void Handler(PlayerHand hand, TileId id)
            {
                completion.TrySetResult(id);
                TileClicked -= Handler;
            }
        }
Esempio n. 13
0
        public async UniTask <string> BuyAsync(string id, CancellationToken cancellationToken = default)
        {
            CheckReady();

            Product product = _storeController.products.WithID(id);

            if (product == null || !product.availableToPurchase)
            {
                return($"Product {id} not available!");
            }

            var buyTcs = new UniTaskCompletionSource <string>();

            cancellationToken.Register(() => buyTcs.TrySetCanceled());
            _buyTasks.Value[id] = buyTcs;
            _storeController.InitiatePurchase(product);
            return(await buyTcs.Task);
        }
Esempio n. 14
0
        /// <summary>
        ///     Disconnect this connection
        /// </summary>
        public void Disconnect()
        {
            Libuv2kNGLogger.Log("libuv client: closed connection");

            _cancellationToken.Cancel();
            _connectedComplete?.TrySetCanceled();
            _client?.Dispose();

            while (_queuedIncomingData.TryDequeue(out _))
            {
                // do nothing
            }

            while (_queueOutgoingData.TryDequeue(out _))
            {
                // do nothing
            }

            _clientLoop?.Dispose();
        }
Esempio n. 15
0
        public async Task SingleOnFirst()
        {
            {
                var tcs = new UniTaskCompletionSource <int>();

                async UniTask <int> Await()
                {
                    return(await tcs.Task);
                }

                var a = Await();

                tcs.TrySetResult(10);
                var r1 = await a;
                var r2 = await tcs.Task; // ok.
                r1.Should().Be(10);
                r2.Should().Be(10);
                tcs.Task.Status.Should().Be(UniTaskStatus.Succeeded);
            }

            {
                var tcs = new UniTaskCompletionSource <int>();

                async UniTask <int> Await()
                {
                    return(await tcs.Task);
                }

                var a = Await();

                tcs.TrySetException(new TestException());
                await Assert.ThrowsAsync <TestException>(async() => await a);

                await Assert.ThrowsAsync <TestException>(async() => await tcs.Task);

                tcs.Task.Status.Should().Be(UniTaskStatus.Faulted);
            }

            var cts = new CancellationTokenSource();

            {
                var tcs = new UniTaskCompletionSource <int>();

                async UniTask <int> Await()
                {
                    return(await tcs.Task);
                }

                var a = Await();

                tcs.TrySetException(new OperationCanceledException(cts.Token));
                (await Assert.ThrowsAsync <OperationCanceledException>(async() => await a)).CancellationToken.Should().Be(cts.Token);
                (await Assert.ThrowsAsync <OperationCanceledException>(async() => await tcs.Task)).CancellationToken.Should().Be(cts.Token);
                tcs.Task.Status.Should().Be(UniTaskStatus.Canceled);
            }
            {
                var tcs = new UniTaskCompletionSource <int>();

                async UniTask <int> Await()
                {
                    return(await tcs.Task);
                }

                var a = Await();

                tcs.TrySetCanceled(cts.Token);
                (await Assert.ThrowsAsync <OperationCanceledException>(async() => await a)).CancellationToken.Should().Be(cts.Token);
                (await Assert.ThrowsAsync <OperationCanceledException>(async() => await tcs.Task)).CancellationToken.Should().Be(cts.Token);
                tcs.Task.Status.Should().Be(UniTaskStatus.Canceled);
            }
        }
 void OnCanceled()
 {
     disposable.Dispose();
     promise.TrySetCanceled();
 }
Esempio n. 17
0
 private void OnDestroy()
 {
     _uniTaskCompletionSource.TrySetCanceled();
 }
Esempio n. 18
0
        public async Task MultiTwo()
        {
            {
                var tcs = new UniTaskCompletionSource();

                async UniTask Await()
                {
                    await tcs.Task;
                }

                var a = Await();
                var b = Await();
                var c = Await();
                tcs.TrySetResult();
                await a;
                await b;
                await c;
                await tcs.Task; // ok.
                tcs.Task.Status.Should().Be(UniTaskStatus.Succeeded);
            }

            {
                var tcs = new UniTaskCompletionSource();

                async UniTask Await()
                {
                    await tcs.Task;
                }

                var a = Await();
                var b = Await();
                var c = Await();

                tcs.TrySetException(new TestException());
                await Assert.ThrowsAsync <TestException>(async() => await a);

                await Assert.ThrowsAsync <TestException>(async() => await b);

                await Assert.ThrowsAsync <TestException>(async() => await c);

                await Assert.ThrowsAsync <TestException>(async() => await tcs.Task);

                tcs.Task.Status.Should().Be(UniTaskStatus.Faulted);
            }

            var cts = new CancellationTokenSource();

            {
                var tcs = new UniTaskCompletionSource();

                async UniTask Await()
                {
                    await tcs.Task;
                }

                var a = Await();
                var b = Await();
                var c = Await();

                tcs.TrySetException(new OperationCanceledException(cts.Token));
                (await Assert.ThrowsAsync <OperationCanceledException>(async() => await a)).CancellationToken.Should().Be(cts.Token);
                (await Assert.ThrowsAsync <OperationCanceledException>(async() => await b)).CancellationToken.Should().Be(cts.Token);
                (await Assert.ThrowsAsync <OperationCanceledException>(async() => await c)).CancellationToken.Should().Be(cts.Token);
                (await Assert.ThrowsAsync <OperationCanceledException>(async() => await tcs.Task)).CancellationToken.Should().Be(cts.Token);
                tcs.Task.Status.Should().Be(UniTaskStatus.Canceled);
            }
            {
                var tcs = new UniTaskCompletionSource();

                async UniTask Await()
                {
                    await tcs.Task;
                }

                var a = Await();
                var b = Await();
                var c = Await();

                tcs.TrySetCanceled(cts.Token);
                (await Assert.ThrowsAsync <OperationCanceledException>(async() => await a)).CancellationToken.Should().Be(cts.Token);
                (await Assert.ThrowsAsync <OperationCanceledException>(async() => await b)).CancellationToken.Should().Be(cts.Token);
                (await Assert.ThrowsAsync <OperationCanceledException>(async() => await c)).CancellationToken.Should().Be(cts.Token);
                (await Assert.ThrowsAsync <OperationCanceledException>(async() => await tcs.Task)).CancellationToken.Should().Be(cts.Token);
                tcs.Task.Status.Should().Be(UniTaskStatus.Canceled);
            }
        }
Esempio n. 19
0
        public async Task MultiTwo()
        {
            {
                var tcs = new UniTaskCompletionSource <int>();

                async UniTask <int> Await()
                {
                    return(await tcs.Task);
                }

                var a = Await();
                var b = Await();
                var c = Await();
                tcs.TrySetResult(10);
                var r1 = await a;
                var r2 = await b;
                var r3 = await c;
                var r4 = await tcs.Task; // ok.
                (r1, r2, r3, r4).Should().Be((10, 10, 10, 10));
                tcs.Task.Status.Should().Be(UniTaskStatus.Succeeded);
            }

            {
                var tcs = new UniTaskCompletionSource <int>();

                async UniTask <int> Await()
                {
                    return(await tcs.Task);
                }

                var a = Await();
                var b = Await();
                var c = Await();

                tcs.TrySetException(new TestException());
                await Assert.ThrowsAsync <TestException>(async() => await a);

                await Assert.ThrowsAsync <TestException>(async() => await b);

                await Assert.ThrowsAsync <TestException>(async() => await c);

                await Assert.ThrowsAsync <TestException>(async() => await tcs.Task);

                tcs.Task.Status.Should().Be(UniTaskStatus.Faulted);
            }

            var cts = new CancellationTokenSource();

            {
                var tcs = new UniTaskCompletionSource <int>();

                async UniTask <int> Await()
                {
                    return(await tcs.Task);
                }

                var a = Await();
                var b = Await();
                var c = Await();

                tcs.TrySetException(new OperationCanceledException(cts.Token));
                (await Assert.ThrowsAsync <OperationCanceledException>(async() => await a)).CancellationToken.Should().Be(cts.Token);
                (await Assert.ThrowsAsync <OperationCanceledException>(async() => await b)).CancellationToken.Should().Be(cts.Token);
                (await Assert.ThrowsAsync <OperationCanceledException>(async() => await c)).CancellationToken.Should().Be(cts.Token);
                (await Assert.ThrowsAsync <OperationCanceledException>(async() => await tcs.Task)).CancellationToken.Should().Be(cts.Token);
                tcs.Task.Status.Should().Be(UniTaskStatus.Canceled);
            }
            {
                var tcs = new UniTaskCompletionSource <int>();

                async UniTask <int> Await()
                {
                    return(await tcs.Task);
                }

                var a = Await();
                var b = Await();
                var c = Await();

                tcs.TrySetCanceled(cts.Token);
                (await Assert.ThrowsAsync <OperationCanceledException>(async() => await a)).CancellationToken.Should().Be(cts.Token);
                (await Assert.ThrowsAsync <OperationCanceledException>(async() => await b)).CancellationToken.Should().Be(cts.Token);
                (await Assert.ThrowsAsync <OperationCanceledException>(async() => await c)).CancellationToken.Should().Be(cts.Token);
                (await Assert.ThrowsAsync <OperationCanceledException>(async() => await tcs.Task)).CancellationToken.Should().Be(cts.Token);
                tcs.Task.Status.Should().Be(UniTaskStatus.Canceled);
            }
        }