diff --git a/src/Extensions/Components/BootstrapBlazor.Topology/BootstrapBlazor.Topology.csproj b/src/Extensions/Components/BootstrapBlazor.Topology/BootstrapBlazor.Topology.csproj index 96b1e38ec4a47389e2eb2fec1b2047ad88454dca..39ec8ccd5ac948fef8c5c7cc2e5d8af55aa221c4 100644 --- a/src/Extensions/Components/BootstrapBlazor.Topology/BootstrapBlazor.Topology.csproj +++ b/src/Extensions/Components/BootstrapBlazor.Topology/BootstrapBlazor.Topology.csproj @@ -1,7 +1,7 @@ - 6.0.1 + 6.0.2 diff --git a/src/Extensions/Components/BootstrapBlazor.Topology/Components/Topology/Topology.razor.cs b/src/Extensions/Components/BootstrapBlazor.Topology/Components/Topology/Topology.razor.cs index 384d8973c3641653ac7a93bb4f34526d3936e289..a4af1c47fef01e21f033bd842954d6679ceed52a 100644 --- a/src/Extensions/Components/BootstrapBlazor.Topology/Components/Topology/Topology.razor.cs +++ b/src/Extensions/Components/BootstrapBlazor.Topology/Components/Topology/Topology.razor.cs @@ -3,6 +3,7 @@ // Website: https://www.blazor.zone or https://argozhang.github.io/ using Microsoft.AspNetCore.Components; +using Microsoft.JSInterop; namespace BootstrapBlazor.Components; @@ -21,6 +22,12 @@ public partial class Topology : IDisposable #endif public string? Content { get; set; } + /// + /// 获得/设置 推送数据间隔时间 最小值 100 默认 2000 毫秒 + /// + [Parameter] + public int Interval { get; set; } = 2000; + /// /// 获得/设置 获取推送数据回调委托方法 /// @@ -28,6 +35,12 @@ public partial class Topology : IDisposable [NotNull] public Func>>? OnQueryAsync { get; set; } + /// + /// 获得/设置 开始推送数据前回调方法 + /// + [Parameter] + public Func? OnBeforePushData { get; set; } + [NotNull] private JSModule? Module { get; set; } @@ -51,30 +64,40 @@ public partial class Topology : IDisposable if (!string.IsNullOrEmpty(Content)) { isInited = true; - Module = await JSRuntime.LoadModule("./_content/BootstrapBlazor.Topology/js/topology_bundle.js", false); - await Module.InvokeVoidAsync("init", Id, Content); + Module = await JSRuntime.LoadModule("./_content/BootstrapBlazor.Topology/js/topology_bundle.js", this, false); + await Module.InvokeVoidAsync("init", Id, Content, nameof(PushData)); + } + } + } - _ = Task.Run(async () => + /// + /// 开始推送数据方法 + /// + /// + [JSInvokable] + public async Task PushData() + { + if (!disposing) + { + if (OnBeforePushData != null) + { + await OnBeforePushData(); + } + Interval = Math.Max(100, Interval); + CancelToken = new CancellationTokenSource(); + while (CancelToken != null && !CancelToken.IsCancellationRequested) + { + try { - if (!disposing) - { - CancelToken = new CancellationTokenSource(); - while (CancelToken != null && !CancelToken.IsCancellationRequested) - { - try - { - await Task.Delay(2000, CancelToken.Token); - - var data = await OnQueryAsync(CancelToken.Token); - await Module.InvokeVoidAsync("push_data", CancelToken.Token, Id, data); - } - catch (TaskCanceledException) - { - - } - } - } - }); + var data = await OnQueryAsync(CancelToken.Token); + await Module.InvokeVoidAsync("push_data", CancelToken.Token, Id, data); + + await Task.Delay(Interval, CancelToken.Token); + } + catch (TaskCanceledException) + { + + } } } } diff --git a/src/Extensions/Components/BootstrapBlazor.Topology/wwwroot/js/topology_bundle.js b/src/Extensions/Components/BootstrapBlazor.Topology/wwwroot/js/topology_bundle.js index 9ba14db059e25fdd0f32d08afe26bad053b55f33..9573422b5527f6eaf904c734f2905f272ffde5ec 100644 --- a/src/Extensions/Components/BootstrapBlazor.Topology/wwwroot/js/topology_bundle.js +++ b/src/Extensions/Components/BootstrapBlazor.Topology/wwwroot/js/topology_bundle.js @@ -1,6 +1,6 @@ var topology = undefined; -export function init(id, data) { +export function init(id, data, method, obj) { BootstrapBlazorModules.addScript('_content/BootstrapBlazor.Topology/js/topology.js'); var handle = setInterval(function () { @@ -18,6 +18,7 @@ export function init(id, data) { }; topology.open(JSON.parse(data)); topology.lock(1); + obj.invokeMethodAsync(method); } }, 200); }