Put lox test script into its own file

This commit is contained in:
Cecylia Bocovich 2023-01-13 14:16:37 -05:00
parent 9de53da203
commit 21a29a09eb
2 changed files with 37 additions and 39 deletions

View File

@ -5,45 +5,7 @@
<title>Lox binding test</title> <title>Lox binding test</title>
</head> </head>
<body> <body>
<script type="module"> <script type="module" src="index.js"></script>
import init, { open_invite, set_panic_hook } from "./pkg/lox_wasm.js";
init().then(() => {
set_panic_hook();
request_open_invite().then((token) => {
open_invite(token);
});
});
function request_open_invite() {
return new Promise((fulfill, reject) => {
const xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {
if (xhr.DONE !== xhr.readyState) {
return;
}
if (xhr.status !== 200) {
console.log("Error. Status code: "+xhr.status);
console.log(xhr);
reject();
return;
}
const response = JSON.parse(xhr.responseText);
console.log("Got invitation token: "+response.invite);
fulfill(response.invite);
return;
};
try {
xhr.open('GET', "http://localhost:8001");
} catch (err) {
console.log("Error connecting to lox bridge db");
reject();
return;
}
xhr.send();
});
}
</script>
</body> </body>
</html> </html>

36
crates/lox-wasm/index.js Normal file
View File

@ -0,0 +1,36 @@
import init, { open_invite, set_panic_hook } from "./pkg/lox_wasm.js";
init().then(() => {
set_panic_hook();
request_open_invite().then((token) => {
open_invite(token);
});
});
function request_open_invite() {
return new Promise((fulfill, reject) => {
const xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {
if (xhr.DONE !== xhr.readyState) {
return;
}
if (xhr.status !== 200) {
console.log("Error. Status code: "+xhr.status);
console.log(xhr);
reject();
return;
}
const response = JSON.parse(xhr.responseText);
console.log("Got invitation token: "+response.invite);
fulfill(response.invite);
return;
};
try {
xhr.open('GET', "http://localhost:8001");
} catch (err) {
console.log("Error connecting to lox bridge db");
reject();
return;
}
xhr.send();
});
}