Skip to content

Commit

Permalink
require a magic response for dev reload server to reconnect
Browse files Browse the repository at this point in the history
  • Loading branch information
samuelcolvin committed Dec 21, 2023
1 parent 4274a8d commit 2b28d23
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
14 changes: 10 additions & 4 deletions src/npm-fastui/src/dev.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { FC, useContext, useEffect } from 'react'
import { sleep } from './tools'
import { ErrorContext } from './hooks/error'
import { fireLoadEvent } from './events'
import { ConfigContext } from './hooks/config'

let devConnected = false

Expand All @@ -20,6 +21,7 @@ export const DevReload: FC<{ enabled?: boolean }> = ({ enabled }) => {

const DevReloadActive = () => {
const { setError } = useContext(ErrorContext)
const { rootUrl } = useContext(ConfigContext)

useEffect(() => {
let listening = true
Expand All @@ -34,7 +36,7 @@ const DevReloadActive = () => {
if (!listening || failCount >= 5) {
return count
}
const response = await fetch('/api/__dev__/reload')
const response = await fetch(rootUrl + '/__dev__/reload')
count++
console.debug(`dev reload connected ${count}...`)
// if the response is okay, and we previously failed, clear error
Expand All @@ -46,9 +48,10 @@ const DevReloadActive = () => {
if (response.status === 404) {
console.log('dev reload endpoint not found, disabling dev reload')
return count
} else if (response.ok) {
} else if (response.ok && text.startsWith('fastui-dev-reload')) {
failCount = 0
const value = parseInt(text.replace(/\./g, '')) || 0
const valueMatch = text.match(/(\d+)$/)
const value = valueMatch ? parseInt(valueMatch[1]!) : 0
if (value !== lastValue) {
lastValue = value
// wait long enough for the server to be back online
Expand All @@ -57,6 +60,9 @@ const DevReloadActive = () => {
fireLoadEvent({ reloadValue: value })
setError(null)
}
} else if (response.ok) {
console.log("dev reload endpoint didn't return magic value, disabling dev reload")
return count
} else {
failCount++
await sleep(2000)
Expand All @@ -72,6 +78,6 @@ const DevReloadActive = () => {
devConnected = false
}
}
}, [setError])
}, [setError, rootUrl])
return <></>
}
1 change: 1 addition & 0 deletions src/python-fastui/fastui/dev.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ def _on_signal(self, *_args: _t.Any):

async def ping(self):
# print('connected', os.getpid())
yield b'fastui-dev-reload\n'

Check warning on line 46 in src/python-fastui/fastui/dev.py

View check run for this annotation

Codecov / codecov/patch

src/python-fastui/fastui/dev.py#L46

Added line #L46 was not covered by tests
yield b'.'
while True:
try:
Expand Down

0 comments on commit 2b28d23

Please sign in to comment.