2023-06-15 23:08:24 -04:00
|
|
|
import { type StateCapture } from "./react.tsx";
|
|
|
|
|
2023-06-17 14:48:39 -04:00
|
|
|
|
|
|
|
const FileListeners = new Map() as Map<string, Array<(module:unknown)=>void>>;
|
2023-06-15 17:13:51 -04:00
|
|
|
export const FileListen =(inPath:string, inHandler:()=>void)=>
|
|
|
|
{
|
|
|
|
const members = FileListeners.get(inPath)??[];
|
|
|
|
members.push(inHandler);
|
|
|
|
FileListeners.set(inPath, members);
|
|
|
|
};
|
2023-06-06 22:48:45 -04:00
|
|
|
|
2023-06-15 17:13:51 -04:00
|
|
|
const Socket:WebSocket = new WebSocket("ws://"+document.location.host);
|
2023-06-17 14:48:39 -04:00
|
|
|
Socket.addEventListener('message', async(event:{data:string})=>
|
2023-06-06 22:48:45 -04:00
|
|
|
{
|
2023-06-17 14:48:39 -04:00
|
|
|
// When a file changes, dynamically re-import it to get the updated members
|
|
|
|
// send the updated members to any listeners for that file
|
2023-06-18 14:37:13 -04:00
|
|
|
const reImport = await import(event.data+"?reload="+Math.random());
|
|
|
|
FileListeners.get(event.data)?.forEach(reExport=>reExport(reImport));
|
2023-06-17 14:48:39 -04:00
|
|
|
HMR.update();
|
2023-06-06 22:48:45 -04:00
|
|
|
});
|
2023-06-18 07:36:55 -04:00
|
|
|
Socket.addEventListener("error", ()=>{clearInterval(SocketTimer); console.log("HMR socket lost")})
|
2023-06-15 17:13:51 -04:00
|
|
|
const SocketTimer = setInterval(()=>{Socket.send("ping")}, 5000);
|
2023-06-06 22:48:45 -04:00
|
|
|
|
2023-06-18 14:37:13 -04:00
|
|
|
|
|
|
|
/*
|
|
|
|
|
|
|
|
Each custom component is secretly modified to have an extra state and id.
|
|
|
|
When there is an HMR update, this state is changed, forcing it to re-render.
|
|
|
|
|
|
|
|
Each *user-created* React.useState is secretly modified and accompanied by an ID.
|
|
|
|
Every time its state is set, the HMR.statesNew map for this ID is set to contain the new state and updater.
|
|
|
|
When a component is removed, any of it's states in HMR.statesNew are also removed.
|
|
|
|
(HMR.statesNew is the "running total" of all states currently at play).
|
|
|
|
|
|
|
|
---
|
|
|
|
|
|
|
|
When a state is interacted with:
|
|
|
|
- statesNew for this id is set
|
|
|
|
- the internal state is also set in the traditional way
|
|
|
|
|
|
|
|
When there is an HMR update:
|
|
|
|
- All custom components are re-rendered...
|
|
|
|
for each useState(value) call that then happens in the re-render:
|
|
|
|
- if there is a "statesOld" value for this state, use that and ignore the passed value, otherwise use the passed value
|
|
|
|
- if this state has not been interacted with since the last reload (statesNew is empty at this id), set statesNew<id> with whatever is in statesOld<id>
|
|
|
|
- statesNew is moved into *statesOld*
|
|
|
|
- statesNew is cleared.
|
|
|
|
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
2023-06-17 14:48:39 -04:00
|
|
|
const HMR =
|
|
|
|
{
|
|
|
|
reloads:1,
|
|
|
|
RegisteredComponents: new Map() as Map<string, ()=>void>,
|
2023-06-16 14:59:27 -04:00
|
|
|
statesNew: new Map() as Map<string, StateCapture>,
|
2023-06-15 23:08:24 -04:00
|
|
|
statesOld: new Map() as Map<string, StateCapture>,
|
2023-06-06 22:48:45 -04:00
|
|
|
wireframe: false,
|
2023-06-17 14:48:39 -04:00
|
|
|
RegisterComponent(reactID:string, value:()=>void):void
|
2023-06-06 22:48:45 -04:00
|
|
|
{
|
2023-06-17 14:48:39 -04:00
|
|
|
this.RegisteredComponents.set(reactID, value);
|
2023-06-06 22:48:45 -04:00
|
|
|
},
|
|
|
|
update()
|
|
|
|
{
|
|
|
|
this.reloads++;
|
2023-06-17 14:48:39 -04:00
|
|
|
this.RegisteredComponents.forEach(handler=>handler());
|
|
|
|
this.RegisteredComponents.clear();
|
2023-06-16 14:59:27 -04:00
|
|
|
this.statesOld = this.statesNew;
|
|
|
|
this.statesNew = new Map();
|
2023-06-06 22:48:45 -04:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2023-06-15 23:08:24 -04:00
|
|
|
export {HMR};
|