Skip to content

Commit 858755a

Browse files
authored
perf(IFrame): improve performance and reduce rendering time (#5522)
* feat: 优化性能减少因此数据推送 * refactor: 更改文档注释为英语 * refactor: 更新渲染逻辑 * chore: bump version 9.4.5 * chore: bump version 9.4.5-beta01
1 parent 7223483 commit 858755a

File tree

3 files changed

+34
-15
lines changed

3 files changed

+34
-15
lines changed

src/BootstrapBlazor/BootstrapBlazor.csproj

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
<Project Sdk="Microsoft.NET.Sdk.Razor">
1+
<Project Sdk="Microsoft.NET.Sdk.Razor">
22

33
<PropertyGroup>
4-
<Version>9.4.4</Version>
4+
<Version>9.4.5-beta01</Version>
55
</PropertyGroup>
66

77
<ItemGroup>

src/BootstrapBlazor/Components/IFrame/IFrame.razor.cs

+24-9
Original file line numberDiff line numberDiff line change
@@ -6,30 +6,30 @@
66
namespace BootstrapBlazor.Components;
77

88
/// <summary>
9-
/// Frame 组件封装 Html iframe 元素
9+
/// Frame component encapsulates the Html iframe element
1010
/// </summary>
1111
public partial class IFrame
1212
{
1313
/// <summary>
14-
/// 获得/设置 Frame 加载网页路径
14+
/// Gets or sets the URL of the webpage to be loaded in the Frame
1515
/// </summary>
1616
[Parameter]
1717
public string? Src { get; set; }
1818

1919
/// <summary>
20-
/// 获得/设置 需要传递的数据
20+
/// Gets or sets the data to be passed
2121
/// </summary>
2222
[Parameter]
2323
public object? Data { get; set; }
2424

2525
/// <summary>
26-
/// 获得/设置 Frame 加载页面传递过来的数据
26+
/// Gets or sets Frame loads the data passed by the page
2727
/// </summary>
2828
[Parameter]
2929
public Func<object?, Task>? OnPostDataAsync { get; set; }
3030

3131
/// <summary>
32-
/// 获得/设置 页面加载完毕后回调方法
32+
/// Gets or sets Callback method after the page is loaded.
3333
/// </summary>
3434
[Parameter]
3535
public Func<Task>? OnReadyAsync { get; set; }
@@ -40,6 +40,16 @@ public partial class IFrame
4040

4141
private object? _lastData;
4242

43+
/// <summary>
44+
/// <inheritdoc/>
45+
/// </summary>
46+
protected override void OnInitialized()
47+
{
48+
base.OnInitialized();
49+
50+
_lastData = Data;
51+
}
52+
4353
/// <summary>
4454
/// <inheritdoc/>
4555
/// </summary>
@@ -59,17 +69,22 @@ protected override async Task OnAfterRenderAsync(bool firstRender)
5969
/// <inheritdoc/>
6070
/// </summary>
6171
/// <returns></returns>
62-
protected override Task InvokeInitAsync() => InvokeVoidAsync("init", Id, Interop, nameof(TriggerPostData));
72+
protected override Task InvokeInitAsync() => InvokeVoidAsync("init", Id, Interop, new
73+
{
74+
Data,
75+
TriggerPostDataCallback = nameof(TriggerPostData),
76+
TriggerLoadedCallback = nameof(TriggerLoaded)
77+
});
6378

6479
/// <summary>
65-
/// 推送数据方法
80+
/// Method to push data
6681
/// </summary>
6782
/// <param name="data"></param>
6883
/// <returns></returns>
6984
public Task PushData(object? data) => InvokeVoidAsync("execute", Id, data);
7085

7186
/// <summary>
72-
/// 由 JavaScript 调用
87+
/// Called by JavaScript
7388
/// </summary>
7489
/// <param name="data"></param>
7590
/// <returns></returns>
@@ -83,7 +98,7 @@ public async Task TriggerPostData(object? data)
8398
}
8499

85100
/// <summary>
86-
/// 由 JavaScript 调用
101+
/// Called by JavaScript
87102
/// </summary>
88103
/// <returns></returns>
89104
[JSInvokable]

src/BootstrapBlazor/Components/IFrame/IFrame.razor.js

+8-4
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,20 @@
11
import Data from "../../modules/data.js"
22

3-
export function init(id, invoke, callback) {
3+
export function init(id, invoke, options) {
4+
const { data, triggerPostDataCallback, triggerLoadedCallback } = options;
45
const handler = e => {
5-
invoke.invokeMethodAsync(callback, e.data)
6+
invoke.invokeMethodAsync(triggerPostDataCallback, e.data)
67
}
78
Data.set(id, handler)
89

9-
window.addEventListener('message', handler);
1010
const frame = document.getElementById(id);
1111

1212
frame.onload = () => {
13-
invoke.invokeMethodAsync("TriggerLoaded");
13+
invoke.invokeMethodAsync(triggerLoadedCallback);
14+
window.addEventListener('message', handler);
15+
if (data) {
16+
frame.contentWindow.postMessage(data);
17+
}
1418
}
1519
}
1620

0 commit comments

Comments
 (0)