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
@inherits AntDomComponentBase
@if (_configs.Count > 0)
{
<div>
<div class="ant-message" style="top: @(_top)px;" @ref="Ref">
<span>
@foreach (var config in _configs)
{
<MessageItem @key="config" Config="config"></MessageItem>
}
</span>
@{
var coinfigs = _configDict.Values.OrderBy(x=>x.Order).ToList();
if (coinfigs.Count > 0)
{
<div>
<div class="ant-message" style="top: @(_top)px;" @ref="Ref">
<span>
@foreach (var config in coinfigs)
{
<MessageItem @key="config" Config="config"></MessageItem>
}
</span>
</div>
</div>
</div>
}
}

View File

@ -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<MessageConfig> _configs
= new List<MessageConfig>();
private readonly ConcurrentDictionary<string, MessageConfig> _configDict
= new ConcurrentDictionary<string, MessageConfig>();
private readonly Dictionary<string, MessageConfig> _configDict
= new Dictionary<string, MessageConfig>();
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);
}
}
}

View File

@ -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; }