hmr #1

Merged
SethTrowbridge merged 9 commits from hmr into master 2023-04-01 11:13:22 -04:00
6 changed files with 31 additions and 25 deletions
Showing only changes of commit 7e6377d5ef - Show all commits

View File

@ -2,10 +2,13 @@
"compilerOptions": {"lib": [
"deno.window", "DOM"
]},
"importMap": "deno.map.json",
"imports": {
"react-original": "https://esm.sh/react@18.2.0"
},
"tasks":
{
"install": "deno install -f -A --unstable --no-lock -n eno server.tsx",
"run": "deno run -A --unstable --no-lock server.tsx"
"run": "deno run -A --unstable --no-lock server.tsx",
"host": "deno run -A --unstable https://deno.land/std@0.181.0/http/file_server.ts"
}
}

View File

@ -1,5 +0,0 @@
{
"imports": {
"react-original": "https://esm.sh/react@18.2.0"
}
}

View File

@ -1,6 +1,6 @@
let reloads = 0;
const listeners = new Map();
const listeners = new Map() as Map<string, Array<(module:unknown)=>void>>;
new WebSocket("ws://"+document.location.host).addEventListener('message', (event) =>
{
let handlers = listeners.get(event.data)??[];
@ -11,22 +11,27 @@ new WebSocket("ws://"+document.location.host).addEventListener('message', (event
return import(event.data+"?reload="+reloads)
.then(updatedModule=>handler(updatedModule));
})
).then(HMR.update);
).then(()=>HMR.update());
});
export const HMR = {
export const FileListen =(inPath:string, inHandler:()=>void)=>
{
const members = listeners.get(inPath)??[];
members.push(inHandler);
listeners.set(inPath, members);
};
const HMR = {
registered: new Map() as Map<string, ()=>void>,
states: new Map(),
statesOld: new Map(),
reloads: 0,
wireframe: false,
wireframe: true,
onChange(key:string, value:()=>void):void
{
this.registered.set(key, value);
},
update()
{
this.reloads++;
this.registered.forEach(handler=>handler());
this.registered.clear();
this.statesOld = this.states;
@ -50,6 +55,8 @@ export const HMR = {
}
};
export {HMR};
export const MapAt =(inMap, inIndex)=>
{
let index = 0;

View File

@ -1,7 +0,0 @@
import * as FS from "https://deno.land/std@0.144.0/fs/mod.ts";
console.log(Deno.args, import.meta.url);
/*
await Deno.mkdir("deep");
await Deno.create(`deep/file.txt`, "sup");
*/

View File

@ -103,6 +103,7 @@ try
{
ImportObject.imports["react-original"] = importReact;
ImportObject.imports["react"] = "/lib/react.tsx";
ImportObject.imports["hmr"] = "/lib/hmr.tsx";
ImportString = JSON.stringify(ImportObject);
}
else
@ -147,9 +148,13 @@ const Index = `
ShadowDOM.append(ShadowCSS);
ShadowDOM.append(ShadowDiv);
import App from "./app.tsx";
App(ShadowDiv, Configure);
TW.observe(TW.twind(Configure, TW.cssom(ShadowCSS)), ShadowDiv);
import App from "./app.tsx";
import React from "react";
React.render(React.createElement(App), ShadowDiv);
</script>
</body>
</html>
@ -212,13 +217,17 @@ Deno.serve({ port: 3000 }, async(_req:Request) =>
const members = [];
for( const key in imp ) { members.push(key); }
body =
`import * as Import from "${url.pathname}?reload=0";
${ members.map(m=>`let proxy_${m} = Import.${m}; export { proxy_${m} as ${m} };`).join(" ") }
`
import {FileListen} from "hmr";
import * as Import from "${url.pathname}?reload=0";
${ members.map(m=>`let proxy_${m} = Import.${m};
export { proxy_${m} as ${m} };
`).join(" ") }
const reloadHandler = (updatedModule)=>
{
${ members.map(m=>`proxy_${m} = updatedModule.${m};`).join("\n") }
};
window.HMR("${url.pathname}", reloadHandler);`;
FileListen("${url.pathname}", reloadHandler);`;
}
else

View File

@ -1 +0,0 @@
export const test = "true";