fix(module: message): Message non thread safe (#3698)

Co-authored-by: James Yeung <shunjiey@hotmail.com>
This commit is contained in:
zxyao 2024-02-28 23:29:00 +08:00 committed by GitHub
parent 2fb0dcfeee
commit 28e195806d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 27 additions and 23 deletions

View File

@ -1,16 +1,19 @@
@namespace AntDesign @namespace AntDesign
@inherits AntDomComponentBase @inherits AntDomComponentBase
@if (_configs.Count > 0) @{
{ var coinfigs = _configDict.Values.OrderBy(x=>x.Order).ToList();
<div> if (coinfigs.Count > 0)
<div class="ant-message" style="top: @(_top)px;" @ref="Ref"> {
<span> <div>
@foreach (var config in _configs) <div class="ant-message" style="top: @(_top)px;" @ref="Ref">
{ <span>
<MessageItem @key="config" Config="config"></MessageItem> @foreach (var config in coinfigs)
} {
</span> <MessageItem @key="config" Config="config"></MessageItem>
}
</span>
</div>
</div> </div>
</div> }
} }

View File

@ -1,6 +1,8 @@
using System; using System;
using System.Collections.Concurrent;
using System.Collections.Generic; using System.Collections.Generic;
using System.Diagnostics; using System.Diagnostics;
using System.Linq;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.AspNetCore.Components; using Microsoft.AspNetCore.Components;
@ -64,11 +66,10 @@ namespace AntDesign
#endregion global config #endregion global config
private readonly List<MessageConfig> _configs private readonly ConcurrentDictionary<string, MessageConfig> _configDict
= new List<MessageConfig>(); = new ConcurrentDictionary<string, MessageConfig>();
private readonly Dictionary<string, MessageConfig> _configDict private static int _counter = 0;
= new Dictionary<string, MessageConfig>();
private Task NotifyAsync(MessageConfig config) private Task NotifyAsync(MessageConfig config)
{ {
@ -79,12 +80,11 @@ namespace AntDesign
var count = _configDict.Count; var count = _configDict.Count;
if (count >= _maxCount) if (count >= _maxCount)
{ {
var removeConfig = _configs[0]; var removeConfig = _configDict.First().Value;
var firstKey = removeConfig.Key; var firstKey = removeConfig.Key;
removeConfig.Cts.Cancel(); removeConfig.Cts.Cancel();
_configDict.Remove(firstKey); _configDict.TryRemove(firstKey, out _);
_configs.Remove(removeConfig);
} }
} }
@ -99,8 +99,8 @@ namespace AntDesign
} }
else else
{ {
_configDict.Add(config.Key, config); config.Order = Interlocked.Increment(ref _counter);
_configs.Add(config); _configDict.TryAdd(config.Key, config);
} }
InvokeAsync(StateHasChanged); InvokeAsync(StateHasChanged);
@ -146,8 +146,7 @@ namespace AntDesign
Task.Delay(500) Task.Delay(500)
.ContinueWith((result) => .ContinueWith((result) =>
{ {
_configDict.Remove(config.Key); _configDict.TryRemove(config.Key, out _);
_configs.Remove(config);
InvokeAsync(StateHasChanged); InvokeAsync(StateHasChanged);
}, TaskScheduler.Current); }, TaskScheduler.Current);
} }
@ -158,8 +157,8 @@ namespace AntDesign
private void Destroy() private void Destroy()
{ {
_configDict.Clear(); _configDict.Clear();
_configs.Clear();
InvokeAsync(StateHasChanged); InvokeAsync(StateHasChanged);
} }
} }
} }

View File

@ -11,6 +11,8 @@ namespace AntDesign
{ {
public class MessageConfig public class MessageConfig
{ {
internal int Order { get; set; }
internal string AnimationClass { get; set; } = MessageAnimationType.Enter; internal string AnimationClass { get; set; } = MessageAnimationType.Enter;
internal CancellationTokenSource Cts { get; set; } internal CancellationTokenSource Cts { get; set; }