Skip to content

Commit

Permalink
Update preflight.js
Browse files Browse the repository at this point in the history
  • Loading branch information
gsteinLTU authored Dec 27, 2023
1 parent 19f6fe1 commit ef68113
Showing 1 changed file with 66 additions and 62 deletions.
128 changes: 66 additions & 62 deletions extensions/RoboScapeOnline2/preflight/preflight.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
function runTest() {
let reportList = document.getElementById('report-list');
let path = document.currentScript.src;
path = path.substring(0, path.lastIndexOf("/"));

function runTest() {
// Step -1: Importing Babylon.js and Pseudomorphic
// Add CSS
var styleElementPM = document.createElement('link');
Expand Down Expand Up @@ -33,84 +29,92 @@ function runTest() {
loaderScriptElement.async = false;

loaderScriptElement.onload = () => {

var guiScriptElement = document.createElement('script');
guiScriptElement.async = false;
guiScriptElement.setAttribute('src', 'https://preview.babylonjs.com/gui/babylon.gui.js');
document.head.appendChild(guiScriptElement);
guiScriptElement.onload = () => {
runTest1();
};
};

loaderScriptElement.setAttribute('src', 'https://preview.babylonjs.com/loaders/babylonjs.loaders.min.js');
document.head.appendChild(loaderScriptElement);

var guiScriptElement = document.createElement('script');
guiScriptElement.async = false;
guiScriptElement.setAttribute('src', 'https://preview.babylonjs.com/gui/babylon.gui.js');
document.head.appendChild(guiScriptElement);

};

scriptElementB.setAttribute('src', 'https://preview.babylonjs.com/babylon.js');
document.head.appendChild(scriptElementB);
};

function runTest1() {
let reportList = document.getElementById('report-list');

// Report status
setTimeout(() => {
let pseudomorphicLoaded = createDialog != undefined && Object.values(document.styleSheets).filter(s => s.href === 'https://pseudomorphic.netsblox.org/style.css').length > 0;
let babylonLoaded = BABYLON != undefined && BABYLON.GUI != undefined && BABYLON.SceneLoader != undefined;
let pseudomorphicLoaded = createDialog != undefined && Object.values(document.styleSheets).filter(s => s.href === 'https://pseudomorphic.netsblox.org/style.css').length > 0;
let babylonLoaded = BABYLON != undefined && BABYLON.GUI != undefined && BABYLON.SceneLoader != undefined;

if (pseudomorphicLoaded && babylonLoaded) {
reportList.appendChild(createReport('success', 'Successfully imported Babylon.js and Pseudomorphic'));
} else {
reportList.appendChild(createReport('error', 'Failed to import Babylon.js and Pseudomorphic'));
return;
}

// Step 0: Import WASM module
var s = document.createElement('script');
s.type = "module";
s.innerHTML = `import init, {step1, step2, step3, step4} from '/pkg/roboscapesim_preflight.js';
await init();
if (pseudomorphicLoaded && babylonLoaded) {
reportList.appendChild(createReport('success', 'Successfully imported Babylon.js and Pseudomorphic'));
} else {
reportList.appendChild(createReport('error', 'Failed to import Babylon.js and Pseudomorphic'));
return;
}
window.RoboScapeOnline_fns = {};
window.RoboScapeOnline_fns.step1 = step1;
window.RoboScapeOnline_fns.step2 = step2;
window.RoboScapeOnline_fns.step3 = step3;
window.RoboScapeOnline_fns.step4 = step4;
// Step 0: Import WASM module
var s = document.createElement('script');
s.type = "module";
s.innerHTML = `import init, {step1, step2, step3, step4} from '${path}/pkg/roboscapesim_preflight.js';
// Trigger a custom event to signal that the module is ready
const event = new Event('roboscapesim:module:ready');
document.dispatchEvent(event);
await init();
`;

window.RoboScapeOnline_fns = {};
window.RoboScapeOnline_fns.step1 = step1;
window.RoboScapeOnline_fns.step2 = step2;
window.RoboScapeOnline_fns.step3 = step3;
window.RoboScapeOnline_fns.step4 = step4;
`;
document.body.appendChild(s);
s.onerror = () => {
reportList.appendChild(createReport('error', 'Failed to import WASM module'));
};

document.addEventListener('roboscapesim:module:ready', () => {
// Report status
setTimeout(() => {
if (window.RoboScapeOnline_fns.step1 != undefined && window.RoboScapeOnline_fns.step2 != undefined && window.RoboScapeOnline_fns.step3 != undefined && window.RoboScapeOnline_fns.step4 != undefined) {
reportList.appendChild(createReport('success', 'Successfully imported WASM module'));
} else {
reportList.appendChild(createReport('error', 'Failed to import WASM module'));
return;
}

// Run tests in WASM module
window.RoboScapeOnline_fns.step1().then(() => {
reportList.appendChild(createReport('success', 'Successfully connected to API server'));
window.RoboScapeOnline_fns.step2().then(() => {
reportList.appendChild(createReport('success', 'Successfully requested new room'));
window.RoboScapeOnline_fns.step3().then(() => {
reportList.appendChild(createReport('success', 'Successfully established WebSocket connection'));
window.RoboScapeOnline_fns.step4().then(() => {
reportList.appendChild(createReport('success', 'Successfully received message from server'));
}).catch(e => {
reportList.appendChild(createReport('error', 'Failed to received message from server: ' + e.toString()));
});
if (window.RoboScapeOnline_fns.step1 != undefined && window.RoboScapeOnline_fns.step2 != undefined && window.RoboScapeOnline_fns.step3 != undefined && window.RoboScapeOnline_fns.step4 != undefined) {
reportList.appendChild(createReport('success', 'Successfully imported WASM module'));
} else {
reportList.appendChild(createReport('error', 'Failed to import WASM module'));
return;
}

// Run tests in WASM module
window.RoboScapeOnline_fns.step1().then(() => {
reportList.appendChild(createReport('success', 'Successfully connected to API server'));
window.RoboScapeOnline_fns.step2().then(() => {
reportList.appendChild(createReport('success', 'Successfully requested new room'));
window.RoboScapeOnline_fns.step3().then(() => {
reportList.appendChild(createReport('success', 'Successfully established WebSocket connection'));
window.RoboScapeOnline_fns.step4().then(() => {
reportList.appendChild(createReport('success', 'Successfully received message from server'));
}).catch(e => {
reportList.appendChild(createReport('error', 'Failed to establish WebSocket connection: ' + e.toString()));
reportList.appendChild(createReport('error', 'Failed to received message from server: ' + e.toString()));
});
}).catch(e => {
reportList.appendChild(createReport('error', 'Failed to request new room: ' + e.toString()));
reportList.appendChild(createReport('error', 'Failed to establish WebSocket connection: ' + e.toString()));
});
}).catch(e => {
reportList.appendChild(createReport('error', 'Failed to connect to API server: ' + e.toString()));
reportList.appendChild(createReport('error', 'Failed to request new room: ' + e.toString()));
});

}, 2000);

}, 2000);
}).catch(e => {
reportList.appendChild(createReport('error', 'Failed to connect to API server: ' + e.toString()));
});
});
document.body.appendChild(s);
}

function createReport(type, message) {
Expand All @@ -120,4 +124,4 @@ function createReport(type, message) {
return report;
}

runTest();
runTest();

0 comments on commit ef68113

Please sign in to comment.