Blazor Interop导致LOS溢出

问题描述 投票:1回答:1

我正在使用Blazor,我正在使用interop使用.NET定期从javascript调用window.SetInterval方法。

调用.NET方法导致滞后性和浏览器中的此消息-debug:

信息

WASM: GC_MAJOR_SWEEP: major size: 1232K in use: 836K
blazor.webassembly.js:1 WASM: GC_MAJOR: (LOS overflow) time 76.10ms, stw 76.14ms los size: 24464K in use: 23455K
blazor.webassembly.js:1 WASM: GC_MINOR: (LOS overflow) time 19.33ms, stw 19.37ms promoted 0K major size: 1232K in use: 339K los size: 24464K in use: 23455K

js部分非常直接。我有一个subscription,一个set和一个clear间隔方法,将从.net调用。

JS

window.methods={

subscription: null,

setInterval: function (interval) {
        if (this.subscription != null || this.subscription != undefined) {
            window.clearInterval(this.subscription);
        }
        subscription = window.setInterval(async function () {
            console.log("calling .net from js");
            await DotNet.invokeMethodAsync("Sms.Studio.Web.Client", "RefreshCallbackAsync");
        }, interval);

    },

clearInterval: function () {
        if (subscription == null || subscription == undefined) {
            return;
        }
        console.log("Clearing subscription");
        window.clearInterval(subscription);
    }
}

.Net我有一个方法启动SetIntervalmethodJSInvokeable。这个可调用的方法启动我在我的组件中订阅的事件。

零件

private bool shouldRefresh;
[Parameter] protected bool ShouldRefresh {
        get {
            return this.shouldRefresh;
        }
        set {
             ManageSubscription(value);

        }
    }

public delegate Task OnRefresh();
public static event OnRefresh JSCalledRefresh;

[JSInvokable("RefreshCallbackAsync")]
public static async Task RefreshAsync() {
  Console.WriteLine("Invoked event");
  JSCalledRefresh?.Invoke();

        }

private  void ManageSubscription(bool value) {
  if (this.shouldRefresh == value) {
     return;
  }
  if(this.shouldRefresh==false && value == true) {
     JSMethods.SetInterval(INTERVAL);
     JSCalledRefresh += SubscribeMethod;     
  }
  else if(this.shouldRefresh==true && value == false) {
     JSMethods.ClearInterval();
     JSCalledRefresh -=SubscribeMethod; 
  }
  this.shouldRefresh = value;
}

基本上我正在做的是我有一个[Parameter] bool ShouldRefresh,当它改变值时,我通过clear订阅setjs interop。当订阅设置时,JSInvokeable方法也将向父组件发布一个事件。

P.S如果我停止从js调用JSInvokeable方法,我会停止获取滞后和溢出消息。

interop overflow blazor
1个回答
2
投票

@Bercovici Adrian,你的代码不完整。假设当你的子组件第一次呈现时,分配给ShouldRefresh的值等于true:调用window.methods.setInterval方法,它调用触发事件的C#RefreshCallbackAsync方法JSCalledRefresh ...我不知道看到任何为JSCalledRefresh分配false的代码,从而启动对window.methods.clearInterval的调用...结果,window.methods.setInterval无休止地执行,一次又一次地调用RefreshCallbackAsync方法。也许,这是例外的原因。

希望这可以帮助...

© www.soinside.com 2019 - 2024. All rights reserved.