refactor: hmr

This commit is contained in:
cha0s 2024-09-24 05:33:08 -05:00
parent 39e7082a28
commit 512c5a470a
3 changed files with 23 additions and 7 deletions

View File

@ -58,6 +58,7 @@ export default function PlaySpecific() {
markChange() {}
}
ecsRef.current = new ClientEcsPerf({Components, Systems});
mainEntityRef.current = undefined;
}, [ecsRef, Components, Systems]);
useEffect(() => {
async function setEcsStuff() {
@ -67,7 +68,7 @@ export default function PlaySpecific() {
setSystems(Systems);
}
setEcsStuff();
}, []);
});
useEffect(() => {
refreshEcs();
}, [refreshEcs]);
@ -90,6 +91,7 @@ export default function PlaySpecific() {
function onConnectionStatus(status) {
switch (status) {
case 'aborted': {
client.disconnect();
setDisconnected(true);
break;
}
@ -127,15 +129,10 @@ export default function PlaySpecific() {
if (!client || !disconnected) {
return;
}
mainEntityRef.current = undefined;
async function reconnect() {
await client.connect(url);
}
reconnect();
const handle = setInterval(reconnect, 1000);
return () => {
clearInterval(handle);
};
}, [client, disconnected, mainEntityRef, url]);
// useEffect(() => {
// let source = true;

View File

@ -130,6 +130,9 @@ export default class Engine {
});
this.server.addPacketListener('Heartbeat', (connection) => {
const playerData = this.connectedPlayers.get(connection);
if (!playerData) {
return;
}
const {distance} = playerData;
const now = performance.now();
distance.rtt = (now - distance.last) / 1000;

View File

@ -68,7 +68,19 @@ onmessage = async (event) => {
if (import.meta.hot) {
const before = withResolvers();
const promises = [before.promise];
import.meta.hot.on('vite:beforeUpdate', async () => {
const accepted = [
'/app/server/engine.js',
'/app/server/create/player.js',
'/app/server/create/forest.js',
'/app/server/create/town.js',
'/app/server/create/homestead.js',
];
let isAccepted = false;
import.meta.hot.on('vite:beforeUpdate', async ({updates}) => {
isAccepted = !!updates.find(({acceptedPath}) => accepted.includes(acceptedPath));
if (!isAccepted) {
return;
}
await engine.disconnectPlayer(0);
engine.stop();
await engine.saveEcses();
@ -128,8 +140,12 @@ import.meta.hot.accept('./create/town.js', async ({default: createTown}) => {
resolve();
});
import.meta.hot.on('vite:afterUpdate', async () => {
if (!isAccepted) {
return;
}
await Promise.all(promises);
postMessage(encode({type: 'ConnectionStatus', payload: 'aborted'}));
postMessage(encode({type: 'EcsChange'}));
close();
});
import.meta.hot.accept();