Skip to content

Commit

Permalink
Try to fix async script loading
Browse files Browse the repository at this point in the history
  • Loading branch information
sean-mcl committed Sep 17, 2024
1 parent 3258a9e commit 54d352c
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 17 deletions.
2 changes: 1 addition & 1 deletion Plotly.Blazor.Generator/Plotly.Blazor.Generator.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
<ExcludeFromSingleFile>true</ExcludeFromSingleFile>
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="src\wwwroot\plotly-interop-5.1.1.js">
<None Update="src\wwwroot\plotly-interop-5.1.2.js">
<ExcludeFromSingleFile>true</ExcludeFromSingleFile>
<CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
Expand Down
2 changes: 1 addition & 1 deletion Plotly.Blazor.Generator/src/PlotlyJsInterop.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ namespace Plotly.Blazor;
/// </summary>
public class PlotlyJsInterop
{
private const string InteropPath = "./_content/Plotly.Blazor/plotly-interop-5.1.1.js";
private const string InteropPath = "./_content/Plotly.Blazor/plotly-interop-5.1.2.js";
private const string PlotlyPath = "./_content/Plotly.Blazor/plotly-2.33.0.min.js";
private const string PlotlyBasicPath = "./_content/Plotly.Blazor/plotly-basic-1.58.5.min.js";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,26 @@
let plotlyReady = false;
const plotlyReadyCallbacks = [];

function checkPlotlyReady(resolve) {
if (window.Plotly) {
plotlyReadyCallbacks.forEach(callback => callback());
plotlyReadyCallbacks.length = 0;
resolve();
} else {
setTimeout(() => checkPlotlyReady(resolve), 10);
}
}

export async function importScript(id, scriptUrl) {
return new Promise((resolve, reject) => {
var existingElement = document.getElementById(id);
if (existingElement) {
if (existingElement.dataset.originalSrc === scriptUrl) {
resolve();
if (window.Plotly) {
resolve();
} else {
checkPlotlyReady(resolve);
}
return;
} else {
existingElement.remove();
Expand All @@ -19,20 +33,16 @@ export async function importScript(id, scriptUrl) {
script.src = scriptUrl;
script.dataset.originalSrc = scriptUrl;
script.type = 'text/javascript';
script.async = true;
script.onload = () => {
scriptCache.set(id, scriptUrl);
plotlyReady = true;
plotlyReadyCallbacks.forEach(callback => callback());
resolve();
checkPlotlyReady(resolve);
};
script.onerror = (error) => reject(new Error(`Failed to load script ${scriptUrl}: ${error.message}`));
document.head.appendChild(script);
});
}

function onPlotlyReady(callback) {
if (plotlyReady) {
if (window.Plotly) {
callback();
} else {
plotlyReadyCallbacks.push(callback);
Expand Down
2 changes: 1 addition & 1 deletion Plotly.Blazor/PlotlyJsInterop.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ namespace Plotly.Blazor;
/// </summary>
public class PlotlyJsInterop
{
private const string InteropPath = "./_content/Plotly.Blazor/plotly-interop-5.1.1.js";
private const string InteropPath = "./_content/Plotly.Blazor/plotly-interop-5.1.2.js";
private const string PlotlyPath = "./_content/Plotly.Blazor/plotly-2.33.0.min.js";
private const string PlotlyBasicPath = "./_content/Plotly.Blazor/plotly-basic-1.58.5.min.js";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,26 @@
let plotlyReady = false;
const plotlyReadyCallbacks = [];

function checkPlotlyReady(resolve) {
if (window.Plotly) {
plotlyReadyCallbacks.forEach(callback => callback());
plotlyReadyCallbacks.length = 0;
resolve();
} else {
setTimeout(() => checkPlotlyReady(resolve), 10);
}
}

export async function importScript(id, scriptUrl) {
return new Promise((resolve, reject) => {
var existingElement = document.getElementById(id);
if (existingElement) {
if (existingElement.dataset.originalSrc === scriptUrl) {
resolve();
if (window.Plotly) {
resolve();
} else {
checkPlotlyReady(resolve);
}
return;
} else {
existingElement.remove();
Expand All @@ -19,20 +33,16 @@ export async function importScript(id, scriptUrl) {
script.src = scriptUrl;
script.dataset.originalSrc = scriptUrl;
script.type = 'text/javascript';
script.async = true;
script.onload = () => {
scriptCache.set(id, scriptUrl);
plotlyReady = true;
plotlyReadyCallbacks.forEach(callback => callback());
resolve();
checkPlotlyReady(resolve);
};
script.onerror = (error) => reject(new Error(`Failed to load script ${scriptUrl}: ${error.message}`));
document.head.appendChild(script);
});
}

function onPlotlyReady(callback) {
if (plotlyReady) {
if (window.Plotly) {
callback();
} else {
plotlyReadyCallbacks.push(callback);
Expand Down

0 comments on commit 54d352c

Please sign in to comment.