Skip to content

Commit

Permalink
chore: Move liveReload code into script.
Browse files Browse the repository at this point in the history
  • Loading branch information
sidharthv96 committed Aug 17, 2023
1 parent fe1a062 commit 718d52a
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 28 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,4 @@ stats/

demos/dev/**
!/demos/dev/example.html
!/demos/dev/reload.js
28 changes: 1 addition & 27 deletions demos/dev/example.html
Original file line number Diff line number Diff line change
Expand Up @@ -33,32 +33,6 @@
el.innerHTML = svg;
</script>

<script>
// Set to false to disable live reload
const liveReload = true;
// Connect to the server and reload the page if the server sends a reload message
const connectToEvents = () => {
const events = new EventSource('/events');
const loadTime = Date.now();
events.onmessage = (event) => {
const time = JSON.parse(event.data);
if (time && time > loadTime) {
location.reload();
}
};
events.onerror = () => {
// No need to log this error, as it's not useful.
events.close();
// Try to reconnect after 1 second in case of errors
setTimeout(connectToEvents, 1000);
};
events.onopen = () => {
console.log('Connected to live reload server');
};
};
if (liveReload) {
setTimeout(connectToEvents, 1000);
}
</script>
<script src="/dev/reload.js"></script>
</body>
</html>
22 changes: 22 additions & 0 deletions demos/dev/reload.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Connect to the server and reload the page if the server sends a reload message
const connectToEvents = () => {
const events = new EventSource('/events');
const loadTime = Date.now();
events.onmessage = (event) => {
const time = JSON.parse(event.data);
if (time && time > loadTime) {
location.reload();
}
};
events.onerror = (error) => {
console.error(error);
events.close();
// Try to reconnect after 1 second in case of errors
setTimeout(connectToEvents, 1000);
};
events.onopen = () => {
console.log('Connected to live reload server');
};
};

setTimeout(connectToEvents, 500);
11 changes: 10 additions & 1 deletion tsconfig.eslint.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,14 @@
// ensure that nobody can accidentally use this config for a build
"noEmit": true
},
"include": ["packages", "tests", "scripts", "cypress", "__mocks__", "./.eslintrc.cjs", "./*"]
"include": [
"packages",
"tests",
"scripts",
"cypress",
"__mocks__",
"./.eslintrc.cjs",
"./*",
"demos/dev"
]
}

0 comments on commit 718d52a

Please sign in to comment.