mirror of
https://github.com/mermaid-js/mermaid.git
synced 2025-01-14 06:43:25 +08:00
chore: Move liveReload code into script.
This commit is contained in:
parent
fe1a06271a
commit
718d52a72c
1
.gitignore
vendored
1
.gitignore
vendored
@ -46,3 +46,4 @@ stats/
|
|||||||
|
|
||||||
demos/dev/**
|
demos/dev/**
|
||||||
!/demos/dev/example.html
|
!/demos/dev/example.html
|
||||||
|
!/demos/dev/reload.js
|
||||||
|
@ -33,32 +33,6 @@ graph TB
|
|||||||
el.innerHTML = svg;
|
el.innerHTML = svg;
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<script>
|
<script src="/dev/reload.js"></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>
|
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
22
demos/dev/reload.js
Normal file
22
demos/dev/reload.js
Normal file
@ -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);
|
@ -5,5 +5,14 @@
|
|||||||
// ensure that nobody can accidentally use this config for a build
|
// ensure that nobody can accidentally use this config for a build
|
||||||
"noEmit": true
|
"noEmit": true
|
||||||
},
|
},
|
||||||
"include": ["packages", "tests", "scripts", "cypress", "__mocks__", "./.eslintrc.cjs", "./*"]
|
"include": [
|
||||||
|
"packages",
|
||||||
|
"tests",
|
||||||
|
"scripts",
|
||||||
|
"cypress",
|
||||||
|
"__mocks__",
|
||||||
|
"./.eslintrc.cjs",
|
||||||
|
"./*",
|
||||||
|
"demos/dev"
|
||||||
|
]
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user