From 28e195806d0ce601de00a2e41190b8e738fa80b2 Mon Sep 17 00:00:00 2001 From: zxyao Date: Wed, 28 Feb 2024 23:29:00 +0800 Subject: [PATCH] fix(module: message): Message non thread safe (#3698) Co-authored-by: James Yeung --- components/message/Message.razor | 25 ++++++++++++---------- components/message/Message.razor.cs | 23 ++++++++++---------- components/message/config/MessageConfig.cs | 2 ++ 3 files changed, 27 insertions(+), 23 deletions(-) diff --git a/components/message/Message.razor b/components/message/Message.razor index 7bfafccd..e9c5d106 100644 --- a/components/message/Message.razor +++ b/components/message/Message.razor @@ -1,16 +1,19 @@ @namespace AntDesign @inherits AntDomComponentBase -@if (_configs.Count > 0) -{ -
-
- - @foreach (var config in _configs) - { - - } - +@{ + var coinfigs = _configDict.Values.OrderBy(x=>x.Order).ToList(); + if (coinfigs.Count > 0) + { +
+
+ + @foreach (var config in coinfigs) + { + + } + +
-
+ } } \ No newline at end of file diff --git a/components/message/Message.razor.cs b/components/message/Message.razor.cs index 47aaa2a3..00d4e4ef 100644 --- a/components/message/Message.razor.cs +++ b/components/message/Message.razor.cs @@ -1,6 +1,8 @@ using System; +using System.Collections.Concurrent; using System.Collections.Generic; using System.Diagnostics; +using System.Linq; using System.Threading; using System.Threading.Tasks; using Microsoft.AspNetCore.Components; @@ -64,11 +66,10 @@ namespace AntDesign #endregion global config - private readonly List _configs - = new List(); + private readonly ConcurrentDictionary _configDict + = new ConcurrentDictionary(); - private readonly Dictionary _configDict - = new Dictionary(); + private static int _counter = 0; private Task NotifyAsync(MessageConfig config) { @@ -79,12 +80,11 @@ namespace AntDesign var count = _configDict.Count; if (count >= _maxCount) { - var removeConfig = _configs[0]; + var removeConfig = _configDict.First().Value; var firstKey = removeConfig.Key; removeConfig.Cts.Cancel(); - _configDict.Remove(firstKey); - _configs.Remove(removeConfig); + _configDict.TryRemove(firstKey, out _); } } @@ -99,8 +99,8 @@ namespace AntDesign } else { - _configDict.Add(config.Key, config); - _configs.Add(config); + config.Order = Interlocked.Increment(ref _counter); + _configDict.TryAdd(config.Key, config); } InvokeAsync(StateHasChanged); @@ -146,8 +146,7 @@ namespace AntDesign Task.Delay(500) .ContinueWith((result) => { - _configDict.Remove(config.Key); - _configs.Remove(config); + _configDict.TryRemove(config.Key, out _); InvokeAsync(StateHasChanged); }, TaskScheduler.Current); } @@ -158,8 +157,8 @@ namespace AntDesign private void Destroy() { _configDict.Clear(); - _configs.Clear(); InvokeAsync(StateHasChanged); } + } } diff --git a/components/message/config/MessageConfig.cs b/components/message/config/MessageConfig.cs index d3818a85..8c08182d 100644 --- a/components/message/config/MessageConfig.cs +++ b/components/message/config/MessageConfig.cs @@ -11,6 +11,8 @@ namespace AntDesign { public class MessageConfig { + internal int Order { get; set; } + internal string AnimationClass { get; set; } = MessageAnimationType.Enter; internal CancellationTokenSource Cts { get; set; }