From b1ca38f22144fda24a6e98b87be4cd91854236bd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marek=20Fi=C5=A1era?= Date: Fri, 22 Feb 2019 23:07:04 +0100 Subject: [PATCH] #218 - Sync parallel queries for profile. --- src/Money.UI.Blazor/Services/UserMiddleware.cs | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/src/Money.UI.Blazor/Services/UserMiddleware.cs b/src/Money.UI.Blazor/Services/UserMiddleware.cs index 9e953caa..cd63226e 100644 --- a/src/Money.UI.Blazor/Services/UserMiddleware.cs +++ b/src/Money.UI.Blazor/Services/UserMiddleware.cs @@ -15,13 +15,20 @@ internal class UserMiddleware : HttpQueryDispatcher.IMiddleware, IEventHandler { private ProfileModel profile; + private Task getProfileTask; public async Task ExecuteAsync(object query, HttpQueryDispatcher dispatcher, HttpQueryDispatcher.Next next) { - if (query is GetProfile) + if (query is GetProfile getProfile) { if (profile == null) - profile = (ProfileModel)await next(query); + { + if (getProfileTask == null) + getProfileTask = LoadProfileAsync(getProfile, next); + + await getProfileTask; + getProfileTask = null; + } return profile; } @@ -29,6 +36,11 @@ public async Task ExecuteAsync(object query, HttpQueryDispatcher dispatc return await next(query); } + private async Task LoadProfileAsync(GetProfile query, HttpQueryDispatcher.Next next) + { + profile = (ProfileModel)await next(query); + } + Task IEventHandler.HandleAsync(EmailChanged payload) { if (profile != null)