Files
dioxus/packages/liveview/src/main.js
T
Evan Almloff cab573eefd Synchronous prevent default (#2792)
* add prevent default methods to the event

* sync prevent default almost working

* sync prevent default working

* Move event handling into the runtime

* update core tests

* restore desktop file dialog

* implement prevent default on web

* add a hint about the new prevent default method

* fix web prevent default

* Fix CTRL+click on links

* fix values memorize in place test

* Fix a few more tests

* Add a playwright test for sync prevent default

* Fix core doc tests

* create a deprecated VirtualDom::handle_event

* fix macos imports in desktop

* Fix onmounted event

* Fix liveview support

* switch to RefCell for metadata

* Remove println

* remove prevent default attribute

* remove web specific link behavior

* Fix liveview links

* more liveview fixes for link

* Fix merge conflicts

* Fix clippy

* use the new prevent default in the file upload example
2024-08-13 11:57:54 -07:00

69 lines
1.6 KiB
JavaScript

const intercept_link_redirects = false;
function main() {
let root = window.document.getElementById("main");
if (root != null) {
window.ipc = new IPC(root);
}
}
class IPC {
constructor(root) {
window.interpreter = new NativeInterpreter();
window.interpreter.initialize(root);
window.interpreter.liveview = true;
window.interpreter.ipc = this;
const ws = new WebSocket(WS_ADDR);
ws.binaryType = "arraybuffer";
function ping() {
ws.send("__ping__");
}
ws.onopen = () => {
// we ping every 30 seconds to keep the websocket alive
setInterval(ping, 30000);
ws.send(window.interpreter.serializeIpcMessage("initialize"));
};
ws.onerror = (err) => {
// todo: retry the connection
};
ws.onmessage = (message) => {
const u8view = new Uint8Array(message.data);
const binaryFrame = u8view[0] == 1;
const messageData = message.data.slice(1);
// The first byte tells the shim if this is a binary of text frame
if (binaryFrame) {
// binary frame
window.interpreter.run_from_bytes(messageData);
} else {
// text frame
let decoder = new TextDecoder("utf-8");
// Using decode method to get string output
let str = decoder.decode(messageData);
// Ignore pongs
if (str != "__pong__") {
const event = JSON.parse(str);
switch (event.type) {
case "query":
Function("Eval", `"use strict";${event.data};`)();
break;
}
}
}
};
this.ws = ws;
}
postMessage(msg) {
this.ws.send(msg);
}
}
main();