public async Task WithAsync()
        {
            var a      = CreateSync(1);
            var b      = CreateAsync(2);
            var c      = CreateAsync(3);
            var result = await ValueTaskEx.WhenAll(a, b, c);

            result.Should().Be((1, 2, 3));
        }
        public async Task Array()
        {
            var a      = CreateSync(1);
            var b      = CreateAsync(2);
            var c      = CreateAsync(3);
            var result = await ValueTaskEx.WhenAll(new[] { a, b, c });

            result.Should().BeEquivalentTo(new[] { 1, 2, 3 });
        }
        static async Task Main(string[] args)
        {
            var ta = Foo();
            var tb = new ValueTask <int>(100);
            var tc = Foo();

            var tako = ValueTaskEx.WhenAll(ta, tb, tc);

            var(a, b, c) = await tako;
        }
Example #4
0
        public async Task Async()
        {
            var calledCount = 0;
            var syncLazy    = ValueTaskEx.Lazy(async() => { calledCount++; await Task.Delay(TimeSpan.FromSeconds(1)); });

            calledCount.Should().Be(0);

            await ValueTaskEx.WhenAll(syncLazy, syncLazy, syncLazy);

            calledCount.Should().Be(1);
        }
Example #5
0
        public override ValueTask SendAsync(ReadOnlyMemory <byte> dataBuffer)
        {
            List <ValueTask> sendTasks = new List <ValueTask>();

            lock (this.connectedSockets)
            {
                foreach (var clientSocket in this.connectedSockets)
                {
                    sendTasks.Add(clientSocket.SendAsync(dataBuffer));
                }
            }

            return(ValueTaskEx.WhenAll(sendTasks));
        }
Example #6
0
        public async Task Async()
        {
            var calledCount = 0;
            var syncLazy    = ValueTaskEx.Lazy(async() => { calledCount++; await Task.Delay(TimeSpan.FromSeconds(1)); return(new object()); });

            calledCount.Should().Be(0);

            var(v1, v2, v3) = await ValueTaskEx.WhenAll(syncLazy, syncLazy, syncLazy);

            calledCount.Should().Be(1);

            object.ReferenceEquals(v1, v2).Should().BeTrue();
            object.ReferenceEquals(v2, v3).Should().BeTrue();
        }
        public async Task Array()
        {
            var a = CreateSync();
            var b = CreateAsync();
            var c = CreateAsync();

            a.IsCompletedSuccessfully.Should().BeTrue();
            b.IsCompletedSuccessfully.Should().BeFalse();
            c.IsCompletedSuccessfully.Should().BeFalse();

            await ValueTaskEx.WhenAll(new[] { a, b, c });

            a.IsCompletedSuccessfully.Should().BeTrue();
            b.IsCompletedSuccessfully.Should().BeTrue();
            c.IsCompletedSuccessfully.Should().BeTrue();
        }
        public async Task AllSync()
        {
            var a = CreateSync();
            var b = CreateSync();
            var c = CreateSync();

            a.IsCompletedSuccessfully.Should().BeTrue();
            b.IsCompletedSuccessfully.Should().BeTrue();
            c.IsCompletedSuccessfully.Should().BeTrue();

            await ValueTaskEx.WhenAll(a, b, c);

            a.IsCompletedSuccessfully.Should().BeTrue();
            b.IsCompletedSuccessfully.Should().BeTrue();
            c.IsCompletedSuccessfully.Should().BeTrue();
        }
Example #9
0
        public async ValueTask <string?> FormatAsync(string text, EnrichedEvent @event)
        {
            if (string.IsNullOrWhiteSpace(text))
            {
                return(text);
            }

            if (TryGetTemplate(text.Trim(), out var template))
            {
                var vars = new TemplateVars
                {
                    ["event"] = @event
                };

                return(await templateEngine.RenderAsync(template, vars));
            }

            if (TryGetScript(text.Trim(), out var script))
            {
                // Script vars are just wrappers over dictionaries for better performance.
                var vars = new EventScriptVars
                {
                    Event   = @event,
                    AppId   = @event.AppId.Id,
                    AppName = @event.AppId.Name,
                    User    = Admin()
                };

                var result = (await scriptEngine.ExecuteAsync(vars, script)).ToString();

                if (result == "undefined")
                {
                    return(GlobalFallback);
                }

                return(result);
            }

            var parts = BuildParts(text, @event);

            if (parts.Any(x => !x.Var.IsCompleted))
            {
                await ValueTaskEx.WhenAll(parts.Select(x => x.Var));
            }

            return(CombineParts(text, parts));
        }
Example #10
0
        public async ValueTask <string?> FormatAsync(string text, EnrichedEvent @event)
        {
            if (string.IsNullOrWhiteSpace(text))
            {
                return(text);
            }

            if (TryGetTemplate(text.Trim(), out var template))
            {
                var vars = new TemplateVars
                {
                    ["event"] = @event
                };

                return(await templateEngine.RenderAsync(template, vars));
            }

            if (TryGetScript(text.Trim(), out var script))
            {
                var vars = new ScriptVars
                {
                    ["event"] = @event
                };

#pragma warning disable MA0042 // Do not use blocking calls in an async method
                var result = scriptEngine.Execute(vars, script).ToString();
#pragma warning restore MA0042 // Do not use blocking calls in an async method

                if (result == "undefined")
                {
                    return(GlobalFallback);
                }

                return(result);
            }

            var parts = BuildParts(text, @event);

            if (parts.Any(x => !x.Var.IsCompleted))
            {
                await ValueTaskEx.WhenAll(parts.Select(x => x.Var));
            }

            return(CombineParts(text, parts));
        }
Example #11
0
        /// <summary>
        /// 検索データを構築します。
        /// </summary>
        /// <param name="shortCode"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async ValueTask <SearchMetadata> BuildAsync(string shortCode, CancellationToken cancellationToken = default)
        {
            var post = await this.ScrapingService.ExtractPostAsync(shortCode, cancellationToken).ConfigureAwait(false);

            if (post.Owner.UserName != AccountName)
            {
                var message = $"{AccountName} 以外の投稿データを取得しようとしています | AccountName : {post.Owner.UserName}";
                throw new InvalidOperationException(message);
            }

            var result   = CaptionParser.Parse(post.Caption);
            var metadata = new SearchMetadata(post, result);
            var t1       = this.CopyThumbnailAsync(post.Medias[0].ImageUrl, metadata.Number, overwrite: true, cancellationToken);
            var t2       = this.UploadMetadataAsync(metadata, cancellationToken);
            await ValueTaskEx.WhenAll(t1, t2).ConfigureAwait(false);

            return(metadata);
        }
Example #12
0
        /// <summary>
        /// 検索データを構築します。
        /// </summary>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async ValueTask BuildAsync(CancellationToken cancellationToken = default)
        {
            var posts = await this.ScrapingService.ExtractPostSlimsAsync(AccountName, recursive : false, cancellationToken).ConfigureAwait(false);

            foreach (var x in posts)
            {
                try
                {
                    var result   = CaptionParser.Parse(x.Caption);
                    var metadata = new SearchMetadata(x, result);
                    var t1       = this.CopyThumbnailAsync(x.ImageUrl, metadata.Number, overwrite: false, cancellationToken);
                    var t2       = this.UploadMetadataAsync(metadata, cancellationToken);
                    await ValueTaskEx.WhenAll(t1, t2).ConfigureAwait(false);
                }
                catch (Exception ex)
                {
                    var message = $"検索データの構築中にエラーが発生しました | ShortCode : {x.ShortCode}";
                    this.Logger.LogError(ex, message);
                    throw;
                }
            }
        }
Example #13
0
        public async ValueTask <string?> FormatAsync(string text, EnrichedEvent @event)
        {
            if (string.IsNullOrWhiteSpace(text))
            {
                return(text);
            }

            if (TryGetTemplate(text.Trim(), out var template))
            {
                var vars = new TemplateVars
                {
                    ["event"] = @event
                };

                return(await templateEngine.RenderAsync(template, vars));
            }

            if (TryGetScript(text.Trim(), out var script))
            {
                var vars = new ScriptVars
                {
                    ["event"] = @event
                };

                return(scriptEngine.Interpolate(vars, script));
            }

            var parts = BuildParts(text, @event);

            if (parts.Any(x => !x.Var.IsCompleted))
            {
                await ValueTaskEx.WhenAll(parts.Select(x => x.Var));
            }

            return(CombineParts(text, parts));
        }
Example #14
0
        protected override ValueTask CloseAsync()
        {
            this.tcpListener?.Stop();
            this.tcpListener = null;

            List <ValueTask> closeTasks = new List <ValueTask>();

            if (this.waitClientsTask != null)
            {
                closeTasks.Add(this.waitClientsTask.DisposeAsync());
            }

            lock (this.connectedSockets)
            {
                foreach (var clientSocket in this.connectedSockets)
                {
                    closeTasks.Add(clientSocket.DisposeAsync());
                }

                this.connectedSockets.Clear();
            }

            return(ValueTaskEx.WhenAll(closeTasks));
        }
Example #15
0
 public ValueTask EnqueueMessagesAsync(IEnumerable <IMemoryOwner <byte> > messages, CancellationToken cancellationToken = default) =>
 ValueTaskEx.WhenAll(messages.Select(message => EnqueueMessageAsync(PendingMessage.Create(message, cancellationToken), cancellationToken)));