[Blazor] Move webassembly only functions to Boot.WebAssembly.ts (#13249)
This commit is contained in:
parent
1361b0e5d6
commit
8197e99c25
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
|
|
@ -6,7 +6,7 @@ import { getAssemblyNameFromUrl } from './Platform/Url';
|
||||||
import { renderBatch } from './Rendering/Renderer';
|
import { renderBatch } from './Rendering/Renderer';
|
||||||
import { SharedMemoryRenderBatch } from './Rendering/RenderBatch/SharedMemoryRenderBatch';
|
import { SharedMemoryRenderBatch } from './Rendering/RenderBatch/SharedMemoryRenderBatch';
|
||||||
import { Pointer } from './Platform/Platform';
|
import { Pointer } from './Platform/Platform';
|
||||||
import { fetchBootConfigAsync, loadEmbeddedResourcesAsync, shouldAutoStart } from './BootCommon';
|
import { shouldAutoStart } from './BootCommon';
|
||||||
import { setEventDispatcher } from './Rendering/RendererEventDispatcher';
|
import { setEventDispatcher } from './Rendering/RendererEventDispatcher';
|
||||||
|
|
||||||
let started = false;
|
let started = false;
|
||||||
|
|
@ -64,6 +64,46 @@ async function boot(options?: any): Promise<void> {
|
||||||
platform.callEntryPoint(mainAssemblyName, bootConfig.entryPoint, []);
|
platform.callEntryPoint(mainAssemblyName, bootConfig.entryPoint, []);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function fetchBootConfigAsync() {
|
||||||
|
// Later we might make the location of this configurable (e.g., as an attribute on the <script>
|
||||||
|
// element that's importing this file), but currently there isn't a use case for that.
|
||||||
|
const bootConfigResponse = await fetch('_framework/blazor.boot.json', { method: 'Get', credentials: 'include' });
|
||||||
|
return bootConfigResponse.json() as Promise<BootJsonData>;
|
||||||
|
}
|
||||||
|
|
||||||
|
function loadEmbeddedResourcesAsync(bootConfig: BootJsonData): Promise<any> {
|
||||||
|
const cssLoadingPromises = bootConfig.cssReferences.map(cssReference => {
|
||||||
|
const linkElement = document.createElement('link');
|
||||||
|
linkElement.rel = 'stylesheet';
|
||||||
|
linkElement.href = cssReference;
|
||||||
|
return loadResourceFromElement(linkElement);
|
||||||
|
});
|
||||||
|
const jsLoadingPromises = bootConfig.jsReferences.map(jsReference => {
|
||||||
|
const scriptElement = document.createElement('script');
|
||||||
|
scriptElement.src = jsReference;
|
||||||
|
return loadResourceFromElement(scriptElement);
|
||||||
|
});
|
||||||
|
return Promise.all(cssLoadingPromises.concat(jsLoadingPromises));
|
||||||
|
}
|
||||||
|
|
||||||
|
function loadResourceFromElement(element: HTMLElement) {
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
element.onload = resolve;
|
||||||
|
element.onerror = reject;
|
||||||
|
document.head!.appendChild(element);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// Keep in sync with BootJsonData in Microsoft.AspNetCore.Blazor.Build
|
||||||
|
interface BootJsonData {
|
||||||
|
main: string;
|
||||||
|
entryPoint: string;
|
||||||
|
assemblyReferences: string[];
|
||||||
|
cssReferences: string[];
|
||||||
|
jsReferences: string[];
|
||||||
|
linkerEnabled: boolean;
|
||||||
|
}
|
||||||
|
|
||||||
window['Blazor'].start = boot;
|
window['Blazor'].start = boot;
|
||||||
if (shouldAutoStart()) {
|
if (shouldAutoStart()) {
|
||||||
boot();
|
boot();
|
||||||
|
|
|
||||||
|
|
@ -1,43 +1,3 @@
|
||||||
export async function fetchBootConfigAsync() {
|
|
||||||
// Later we might make the location of this configurable (e.g., as an attribute on the <script>
|
|
||||||
// element that's importing this file), but currently there isn't a use case for that.
|
|
||||||
const bootConfigResponse = await fetch('_framework/blazor.boot.json', { method: 'Get', credentials: 'include' });
|
|
||||||
return bootConfigResponse.json() as Promise<BootJsonData>;
|
|
||||||
}
|
|
||||||
|
|
||||||
export function loadEmbeddedResourcesAsync(bootConfig: BootJsonData): Promise<any> {
|
|
||||||
const cssLoadingPromises = bootConfig.cssReferences.map(cssReference => {
|
|
||||||
const linkElement = document.createElement('link');
|
|
||||||
linkElement.rel = 'stylesheet';
|
|
||||||
linkElement.href = cssReference;
|
|
||||||
return loadResourceFromElement(linkElement);
|
|
||||||
});
|
|
||||||
const jsLoadingPromises = bootConfig.jsReferences.map(jsReference => {
|
|
||||||
const scriptElement = document.createElement('script');
|
|
||||||
scriptElement.src = jsReference;
|
|
||||||
return loadResourceFromElement(scriptElement);
|
|
||||||
});
|
|
||||||
return Promise.all(cssLoadingPromises.concat(jsLoadingPromises));
|
|
||||||
}
|
|
||||||
|
|
||||||
function loadResourceFromElement(element: HTMLElement) {
|
|
||||||
return new Promise((resolve, reject) => {
|
|
||||||
element.onload = resolve;
|
|
||||||
element.onerror = reject;
|
|
||||||
document.head!.appendChild(element);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
// Keep in sync with BootJsonData in Microsoft.AspNetCore.Blazor.Build
|
|
||||||
interface BootJsonData {
|
|
||||||
main: string;
|
|
||||||
entryPoint: string;
|
|
||||||
assemblyReferences: string[];
|
|
||||||
cssReferences: string[];
|
|
||||||
jsReferences: string[];
|
|
||||||
linkerEnabled: boolean;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Tells you if the script was added without <script src="..." autostart="false"></script>
|
// Tells you if the script was added without <script src="..." autostart="false"></script>
|
||||||
export function shouldAutoStart() {
|
export function shouldAutoStart() {
|
||||||
return !!(document &&
|
return !!(document &&
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue