auto-reload.js 980 B

123456789101112131415161718192021222324252627282930
  1. // Copyright 2019-2023 Tauri Programme within The Commons Conservancy
  2. // SPDX-License-Identifier: Apache-2.0
  3. // SPDX-License-Identifier: MIT
  4. // taken from https://github.com/thedodd/trunk/blob/5c799dc35f1f1d8f8d3d30c8723cbb761a9b6a08/src/autoreload.js
  5. ;(function () {
  6. const url = 'ws:' + '//' + window.location.host + '/_tauri-cli/ws'
  7. const poll_interval = 5000
  8. const reload_upon_connect = () => {
  9. window.setTimeout(() => {
  10. // when we successfully reconnect, we'll force a
  11. // reload (since we presumably lost connection to
  12. // trunk due to it being killed, so it will have
  13. // rebuilt on restart)
  14. const ws = new WebSocket(url)
  15. ws.onopen = () => window.location.reload()
  16. ws.onclose = reload_upon_connect
  17. }, poll_interval)
  18. }
  19. const ws = new WebSocket(url)
  20. ws.onmessage = (ev) => {
  21. const msg = JSON.parse(ev.data)
  22. if (msg.reload) {
  23. window.location.reload()
  24. }
  25. }
  26. ws.onclose = reload_upon_connect
  27. })()