Exemplo n.º 1
0
        public static MondValue GetAsync(string address)
        {
            AsyncUtil.EnsureAsync();

            var uri    = GetUri(address);
            var client = GetHttpClient();

            return(AsyncUtil.ToObject(client.GetStringAsync(uri).ContinueWith(t => (MondValue)t.Result)));
        }
Exemplo n.º 2
0
Arquivo: Task.cs Projeto: foobit/Mond
        public static MondValue WhenAny(MondState state, params MondValue[] tasks)
        {
            AsyncUtil.EnsureAsync();

            var taskArray = AsyncUtil.ToTaskArray(state, tasks);

            var task = Task.WhenAny(taskArray).ContinueWith(t =>
            {
                var index = Array.IndexOf(taskArray, t.Result);
                return(tasks[index]);
            });

            return(AsyncUtil.ToObject(task));
        }
Exemplo n.º 3
0
Arquivo: Task.cs Projeto: foobit/Mond
        public static MondValue WhenAll(MondState state, params MondValue[] tasks)
        {
            AsyncUtil.EnsureAsync();

            var taskArray = AsyncUtil.ToTaskArray(state, tasks);

            var task = Task.WhenAll(taskArray).ContinueWith(t =>
            {
                var array = MondValue.Array();
                array.ArrayValue.AddRange(t.Result);
                return(array);
            });

            return(AsyncUtil.ToObject(task));
        }
Exemplo n.º 4
0
Arquivo: Task.cs Projeto: foobit/Mond
        public static MondValue Delay(double seconds, MondValue cancellationToken = null)
        {
            AsyncUtil.EnsureAsync();

            var ct = AsyncUtil.AsCancellationToken(cancellationToken);

            if (!ct.HasValue)
            {
                throw new MondRuntimeException("Task.delay: second argument must be a CancellationToken");
            }

            var timeSpan = seconds >= 0 ?
                           TimeSpan.FromSeconds(seconds) :
                           TimeSpan.FromMilliseconds(-1);

            return(AsyncUtil.ToObject(Task.Delay(timeSpan, ct.Value)));
        }
Exemplo n.º 5
0
        public MondValue Start(MondState state, MondValue value)
        {
            if (value.Type == MondValueType.Function)
            {
                value = state.Call(value);
            }

            var getEnumerator = value["getEnumerator"];

            if (getEnumerator.Type != MondValueType.Function)
            {
                throw new MondRuntimeException("Task objects must define getEnumerator");
            }

            var enumerator = state.Call(getEnumerator);

            var task = _factory.StartNew(async() =>
            {
                try
                {
                    await AsyncUtil.RunMondTask(state, enumerator);
                }
                catch (Exception e)
                {
                    lock (_exceptions)
                        _exceptions.Enqueue(e);
                }
                finally
                {
                    Interlocked.Decrement(ref _activeTasks);
                }
            });

            Interlocked.Increment(ref _activeTasks);

            // return a task that completes when the started task completes
            Func <Task> waitTask = async() =>
            {
                await await task;
            };

            return(AsyncUtil.ToObject(waitTask()));
        }