hmr fixes

allow page refresh, add keep-alive ping
This commit is contained in:
Seth Trowbridge 2023-05-06 09:51:55 -04:00
parent d3f08a67a0
commit 8a1b7e7dbd
3 changed files with 13 additions and 4 deletions

View File

@ -3,8 +3,6 @@
"imports": "imports":
{ {
"react": "https://esm.sh/stable/preact@10.13.2/compat", "react": "https://esm.sh/stable/preact@10.13.2/compat",
"preact": "https://esm.sh/stable/preact@10.13.2/",
"@deep/": "./deep/",
"@eno/app": "./app.tsx", "@eno/app": "./app.tsx",
"@eno/iso": "http://localhost:4507/lib/iso.tsx" "@eno/iso": "http://localhost:4507/lib/iso.tsx"
}, },

View File

@ -1,7 +1,8 @@
let reloads = 0; let reloads = 0;
const listeners = new Map() as Map<string, Array<(module:unknown)=>void>>; const listeners = new Map() as Map<string, Array<(module:unknown)=>void>>;
new WebSocket("ws://"+document.location.host).addEventListener('message', (event) => const socket:WebSocket = new WebSocket("ws://"+document.location.host);
socket.addEventListener('message', (event) =>
{ {
let handlers = listeners.get(event.data)??[]; let handlers = listeners.get(event.data)??[];
reloads++; reloads++;
@ -13,6 +14,7 @@ new WebSocket("ws://"+document.location.host).addEventListener('message', (event
}) })
).then(()=>HMR.update()); ).then(()=>HMR.update());
}); });
const socketTimer = setInterval(()=>{socket.send("ping")}, 1000);
export const FileListen =(inPath:string, inHandler:()=>void)=> export const FileListen =(inPath:string, inHandler:()=>void)=>
{ {

View File

@ -89,6 +89,7 @@ function Transpiler(inDevMode:boolean)
: :
()=>false; ()=>false;
let reloads = 0;
const watcher =async()=> const watcher =async()=>
{ {
const FilesChanged:Map<string, string> = new Map(); const FilesChanged:Map<string, string> = new Map();
@ -99,6 +100,13 @@ function Transpiler(inDevMode:boolean)
const key = path.substring(Deno.cwd().length).replaceAll("\\", "/"); const key = path.substring(Deno.cwd().length).replaceAll("\\", "/");
if(action != "remove") if(action != "remove")
{ {
if(Imports?.imports)
{
reloads++;
const importApp = Imports.imports["@eno/app"];
const imp = await import(Path.Active+importApp+"?reloads="+reloads);
App = imp.default;
}
await TranspileURL(Path.Active+key, key, false); await TranspileURL(Path.Active+key, key, false);
SocketsBroadcast(key); SocketsBroadcast(key);
} }
@ -294,7 +302,8 @@ console.log(`Dev Mode: ${DevMode}`);
console.log(`Args seen: `, Flags); console.log(`Args seen: `, Flags);
const {Transpileable, TranspileURL, SocketsHandler} = Transpiler(DevMode); const {Transpileable, TranspileURL, SocketsHandler} = Transpiler(DevMode);
const {Imports, App, TwindInst, Error} = await Configure(DevMode, Path.LibDir); let {Imports, App, TwindInst, Error} = await Configure(DevMode, Path.LibDir);
if(Error) if(Error)
{ {
console.log(Error); console.log(Error);