mirror of
https://github.com/rickbarrette/simple-aircraft-manager-rails.git
synced 2026-11-17 15:50:36 -05:00
Weak signal counts as no signal
With a signal so weak requests hang, the phone still says it's online, so nothing fell back: Log Flight waited minutes with no feedback, and pages never reached the offline page. - Log Flight and Report Squawk say "Sending…" and, after 10 seconds without an answer (60 with photos), keep the entry on the device as with no signal. When the top bar is already red they keep it right away. If it got through after all, the copy sent later is dropped (same client_key). - What's kept on the device isn't sent while the top bar is red, gives up after 15 seconds, and goes again as soon as the server answers. - Every page of the app falls back to its kept copy or the offline page: after 10 seconds, then 3 while requests keep failing, and at once right after a failure. Downloads made while you wait (.sam.zip, records package, PDFs) have no time limit. Checked on an Android 14 emulator with a connection that accepts requests and never answers. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
d9de1be562
commit
e4bc3a5b7b
8 files changed
+134
-24
No files matched your search
@@ -12,6 +12,8 @@ import { t } from "i18n"
|
||||
// sam:progress { source, done, total } (done >= total when finished)
|
||||
// sam:waiting { count }
|
||||
// sam:unreachable / sam:reachable
|
||||
// and it says when the server answers again with sam:back-online (what was
|
||||
// logged offline is sent then).
|
||||
// The header is replaced on every page, so the state and the listeners live
|
||||
// in this module and the header on the page just shows them.
|
||||
const RETRY_EVERY = 15 * 1000
|
||||
@@ -38,6 +40,7 @@ function setOnline(online) {
|
||||
state.online = online
|
||||
header?.announce(online ? t("connection.back_online") : t("connection.offline"))
|
||||
update()
|
||||
if (online) document.dispatchEvent(new CustomEvent("sam:back-online"))
|
||||
}
|
||||
|
||||
// Several reports often arrive together (done sending, then none waiting):
|
||||
@@ -56,6 +59,9 @@ document.addEventListener("sam:reachable", () => setOnline(true))
|
||||
document.addEventListener("sam:progress", ({ detail }) => { state.progress[detail.source] = detail; update() })
|
||||
document.addEventListener("sam:waiting", ({ detail }) => { state.waiting = detail.count; update() })
|
||||
|
||||
// Whether the server answered last time (false while the top bar is red).
|
||||
export const serverReachable = () => state.online
|
||||
|
||||
export default class extends Controller {
|
||||
static targets = [ "ring", "label", "announce" ]
|
||||
|
||||
|
||||
@@ -1,10 +1,18 @@
|
||||
import { Controller } from "@hotwired/stimulus"
|
||||
import { t } from "i18n"
|
||||
import { queue, formFields, newKey, currentUser } from "controllers/offline_queue"
|
||||
import { serverReachable } from "controllers/connection_controller"
|
||||
|
||||
// How long to wait for the server before keeping it here instead. Photos
|
||||
// on a squawk take longer to send.
|
||||
const WAIT = 10 * 1000
|
||||
const WAIT_WITH_FILES = 60 * 1000
|
||||
|
||||
// Log Flight and Report Squawk: with no connection, the form is kept on this
|
||||
// device and sent later (see offline_sync_controller). For flights, the next
|
||||
// leg starts where the last saved one ended.
|
||||
// leg starts where the last saved one ended. A signal so weak the server
|
||||
// doesn't answer counts as none: it's kept here too, and if it got through
|
||||
// after all, the server drops the copy sent later (same client_key).
|
||||
export default class extends Controller {
|
||||
static targets = [ "key", "notice" ]
|
||||
static values = { kind: { type: String, default: "flight" }, tail: String }
|
||||
@@ -23,14 +31,21 @@ export default class extends Controller {
|
||||
|
||||
get flight() { return this.kindValue === "flight" }
|
||||
|
||||
// No connection: keep it here instead of submitting.
|
||||
// No connection (or the top bar is red): keep it here instead of submitting.
|
||||
async submit(event) {
|
||||
if (navigator.onLine) return
|
||||
if (navigator.onLine && serverReachable()) return
|
||||
event.preventDefault()
|
||||
event.stopImmediatePropagation()
|
||||
await this.save(formFields(this.element))
|
||||
}
|
||||
|
||||
// Give up waiting after a while; the request then fails (networkFailed).
|
||||
limit(event) {
|
||||
const options = event.detail.fetchOptions
|
||||
const files = [ ...this.element.querySelectorAll("input[type=file]") ].some((input) => input.files?.length)
|
||||
options.signal = AbortSignal.any([ options.signal, AbortSignal.timeout(files ? WAIT_WITH_FILES : WAIT) ].filter(Boolean))
|
||||
}
|
||||
|
||||
// Kept in case the request fails on the way (no signal but the browser thinks it's online).
|
||||
remember() { this.pending = formFields(this.element) }
|
||||
|
||||
|
||||
@@ -1,12 +1,20 @@
|
||||
import { Controller } from "@hotwired/stimulus"
|
||||
import { t } from "i18n"
|
||||
import { queue } from "controllers/offline_queue"
|
||||
import { serverReachable } from "controllers/connection_controller"
|
||||
|
||||
// How long to wait for the server to take one before trying again later
|
||||
// (a weak signal can leave a request hanging for minutes). Photos take longer.
|
||||
const WAIT = 15 * 1000
|
||||
const WAIT_WITH_FILES = 60 * 1000
|
||||
|
||||
const report = (name, detail) => document.dispatchEvent(new CustomEvent(name, { detail }))
|
||||
|
||||
// On every page: sends the flights and squawks this user saved offline once
|
||||
// there's a connection, says what happened, and shows the count on the app
|
||||
// icon. A rejected item stays until it's discarded.
|
||||
// icon. A rejected item stays until it's discarded. Nothing is tried while
|
||||
// the top bar is red; it goes again as soon as it turns back
|
||||
// (connection_controller.js), signal or not.
|
||||
export default class extends Controller {
|
||||
static targets = [ "banner" ]
|
||||
|
||||
@@ -14,19 +22,21 @@ export default class extends Controller {
|
||||
this.sync = this.sync.bind(this)
|
||||
window.addEventListener("online", this.sync)
|
||||
document.addEventListener("offline-queue:changed", this.sync)
|
||||
document.addEventListener("sam:back-online", this.sync)
|
||||
this.sync()
|
||||
}
|
||||
|
||||
disconnect() {
|
||||
window.removeEventListener("online", this.sync)
|
||||
document.removeEventListener("offline-queue:changed", this.sync)
|
||||
document.removeEventListener("sam:back-online", this.sync)
|
||||
}
|
||||
|
||||
async sync() {
|
||||
let items
|
||||
try { items = await queue.mine() } catch (_) { return }
|
||||
if (!items.length) { this.badge(0); report("sam:waiting", { count: 0 }); return this.show("") }
|
||||
if (!navigator.onLine || this.running) return this.status(items)
|
||||
if (!navigator.onLine || !serverReachable() || this.running) return this.status(items)
|
||||
this.running = true
|
||||
const sent = []
|
||||
const sending = items.filter((i) => !i.error)
|
||||
@@ -53,7 +63,9 @@ export default class extends Controller {
|
||||
if (token) body.append("authenticity_token", token)
|
||||
let response
|
||||
try {
|
||||
response = await fetch(item.url, { method: "POST", body, credentials: "same-origin", headers: { Accept: "text/html" } })
|
||||
const files = item.fields.some(([ , value ]) => value instanceof Blob)
|
||||
response = await fetch(item.url, { method: "POST", body, credentials: "same-origin", headers: { Accept: "text/html" },
|
||||
signal: AbortSignal.timeout(files ? WAIT_WITH_FILES : WAIT) })
|
||||
} catch (_) { return "offline" }
|
||||
if ([ 502, 503, 504 ].includes(response.status)) return "offline"
|
||||
if (response.url.includes("/session/new")) return "signin"
|
||||
|
||||
Reference in new issue
Block a user