ant-design-blazor/components/spin/Spin.razor.cs
Tony Yip 4b04f09832 perf: avoid memory leak issue of event listener (#1857)
* perf: avoid memory leak #1834

Avoid memory leak by remove the exclusive parameter and logic in the code block on AddEventListener method in DomEventService class.

The following are the components affected:
components/affix/Affix.razor.cs
components/anchor/Anchor.razor.cs
components/carousel/Carousel.razor.cs
components/core/Component/Overlay/Overlay.razor.cs
components/core/Component/Overlay/OverlayTrigger.razor.cs
components/core/JsInterop/DomEventService.cs
components/descriptions/Descriptions.razor.cs
components/dropdown/DropdownButton.cs
components/grid/Row.razor.cs
components/input/Input.cs
components/input/TextArea.razor.cs
components/layout/Sider.razor.cs
components/list/ListItem.razor.cs
components/select/Select.razor.cs
components/select/internal/SelectContent.razor.cs
components/slider/Slider.razor.cs
components/table/Table.razor.cs
components/tabs/Tabs.razor.cs

* fix override AddEventListener method in AntDesign.TestKit project

* add register/remove event listerner for exclusive use in DomEventService class

* move _dotNetObjects to DomEventListerner class/service, so that users not required to maintain it in each component.

* * move share/reuse dom event listerner methods to DomEventListerner class

* remove method 'AddEventListener' that no longer exists in DomEventService class in AntDesign.TestKit project

* * change the component referring to an IDomEventListerner interface instead of a concrete class,
  so that the component can be tested via a mock TestDomEventListerner.

* introduce DisposeShared and Dispose method in DomEventListerner to ease user remove callback from DomEventListerner

* register IDomEventListerner into DI container instead of create manually

* fix FormatKey

* fix FormatKey

* fix tests

* fix test

* fix test

Co-authored-by: James Yeung <shunjiey@hotmail.com>
2021-09-09 12:56:11 +08:00

117 lines
3.0 KiB
C#

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Timers;
using Microsoft.AspNetCore.Components;
namespace AntDesign
{
public partial class Spin : AntDomComponentBase
{
/// <summary>
/// small | default | large
/// </summary>
[Parameter]
public string Size { get; set; } = "default";
[Parameter]
public string Tip { get; set; } = null;
[Parameter]
public int Delay { get; set; } = 0;
[Parameter]
public bool Spinning { get; set; } = true;
[Parameter]
public string WrapperClassName { get; set; }
[Parameter]
public RenderFragment Indicator { get; set; }
[Parameter]
public RenderFragment ChildContent { get; set; }
private ClassMapper WrapperClassMapper { get; set; } = new ClassMapper();
private const string PrefixCls = "ant-spin";
private bool _isLoading = true;
private bool Simple => ChildContent == null;
private string ContainerClass => _isLoading ? $"{PrefixCls}-blur" : "";
private Timer _delayTimer;
private void SetClass()
{
WrapperClassMapper
.If(WrapperClassName, () => !string.IsNullOrWhiteSpace(WrapperClassName))
.If($"{PrefixCls}-nested-loading", () => !Simple);
ClassMapper
.Add(PrefixCls)
.If($"{PrefixCls}-spinning", () => _isLoading)
.If($"{PrefixCls}-lg", () => Size == "large")
.If($"{PrefixCls}-sm", () => Size == "small")
.If($"{PrefixCls}-show-text", () => string.IsNullOrWhiteSpace(Tip))
.If($"{PrefixCls}-rtl", () => RTL);
}
protected override void OnInitialized()
{
SetClass();
_isLoading = Spinning;
if (Delay > 0)
{
_delayTimer = new Timer(Delay);
_delayTimer.Elapsed += DelayElapsed;
}
base.OnInitialized();
}
protected override void OnParametersSet()
{
base.OnParametersSet();
if (_delayTimer != null)
{
if (_isLoading != Spinning)
{
_delayTimer.Stop();
_delayTimer.Start();
}
else
{
_delayTimer.Stop();
}
}
else
{
if (_isLoading != Spinning)
{
_isLoading = Spinning;
InvokeAsync(StateHasChanged);
}
}
}
private void DelayElapsed(object sender, ElapsedEventArgs args)
{
_isLoading = Spinning;
InvokeAsync(StateHasChanged);
}
protected override void Dispose(bool disposing)
{
_delayTimer?.Dispose();
base.Dispose(disposing);
}
}
}