2023-03-29 22:43:23 -04:00
|
|
|
|
|
|
|
let reloads = 0;
|
2023-03-31 06:55:54 -04:00
|
|
|
const listeners = new Map() as Map<string, Array<(module:unknown)=>void>>;
|
2023-03-29 22:43:23 -04:00
|
|
|
new WebSocket("ws://"+document.location.host).addEventListener('message', (event) =>
|
|
|
|
{
|
|
|
|
let handlers = listeners.get(event.data)??[];
|
|
|
|
reloads++;
|
|
|
|
Promise.all(
|
|
|
|
handlers.map(handler=>
|
|
|
|
{
|
|
|
|
return import(event.data+"?reload="+reloads)
|
|
|
|
.then(updatedModule=>handler(updatedModule));
|
|
|
|
})
|
2023-03-31 06:55:54 -04:00
|
|
|
).then(()=>HMR.update());
|
2023-03-29 22:43:23 -04:00
|
|
|
});
|
|
|
|
|
2023-03-31 06:55:54 -04:00
|
|
|
export const FileListen =(inPath:string, inHandler:()=>void)=>
|
|
|
|
{
|
|
|
|
const members = listeners.get(inPath)??[];
|
|
|
|
members.push(inHandler);
|
|
|
|
listeners.set(inPath, members);
|
|
|
|
};
|
|
|
|
|
|
|
|
const HMR = {
|
2023-03-31 20:16:23 -04:00
|
|
|
reloads:0,
|
2023-03-29 22:43:23 -04:00
|
|
|
registered: new Map() as Map<string, ()=>void>,
|
|
|
|
states: new Map(),
|
|
|
|
statesOld: new Map(),
|
2023-03-31 20:16:23 -04:00
|
|
|
wireframe: false,
|
2023-03-29 22:43:23 -04:00
|
|
|
onChange(key:string, value:()=>void):void
|
|
|
|
{
|
|
|
|
this.registered.set(key, value);
|
|
|
|
},
|
|
|
|
update()
|
|
|
|
{
|
2023-03-31 20:16:23 -04:00
|
|
|
this.reloads++;
|
2023-03-29 22:43:23 -04:00
|
|
|
this.registered.forEach(handler=>handler());
|
|
|
|
this.registered.clear();
|
|
|
|
this.statesOld = this.states;
|
|
|
|
this.states = new Map();
|
|
|
|
this.echoState();
|
|
|
|
},
|
|
|
|
echoState()
|
|
|
|
{
|
|
|
|
let output = [];
|
|
|
|
for(const[key, val] of HMR.statesOld)
|
|
|
|
{
|
|
|
|
output[key] = val.state+"--"+val.reload;
|
|
|
|
}
|
|
|
|
console.log(output);
|
|
|
|
output = [];
|
|
|
|
for(const[key, val] of HMR.states)
|
|
|
|
{
|
|
|
|
output[key] = val.state+"--"+val.reload;
|
|
|
|
}
|
|
|
|
console.log(output);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2023-03-31 06:55:54 -04:00
|
|
|
export {HMR};
|
|
|
|
|
2023-03-29 22:43:23 -04:00
|
|
|
export const MapAt =(inMap, inIndex)=>
|
|
|
|
{
|
|
|
|
let index = 0;
|
|
|
|
for(const kvp of inMap)
|
|
|
|
{
|
|
|
|
if(index == inIndex)
|
|
|
|
{
|
|
|
|
return kvp;
|
|
|
|
}
|
|
|
|
index++;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
};
|