From 55fbab6163aa4aae7ddb7f505dd31a591117a6c5 Mon Sep 17 00:00:00 2001 From: "Sean R. Abraham" Date: Wed, 19 Feb 2025 09:06:25 -0500 Subject: [PATCH] make a few code style improvements no need for a recursive call anymore, since async/await makes it work without! making fire-and-forget promise use more clear allow TODOs, because I like to use them to highlight areas that need work --- pyproject.toml | 1 + src/ims/element/static/field_report.js | 60 ++++++++++++------------- src/ims/element/static/field_reports.js | 4 +- src/ims/element/static/incident.js | 18 ++++---- src/ims/element/static/incidents.js | 4 +- 5 files changed, 41 insertions(+), 46 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 5b09beaac..614030ca4 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -106,6 +106,7 @@ ignore = [ "EM102", # Exception must not use an f-string literal, assign to variable first "ERA001", # Found commented-out code "FIX001", # Line contains FIXME, consider resolving the issue + "FIX002", # Line contains TODO, consider resolving the issue "ISC001", # Implicitly concatenated strings on a single line # Disabled for formatter compatibility "N802", # Function name should be lowercase "N803", # Argument name should be lowercase diff --git a/src/ims/element/static/field_report.js b/src/ims/element/static/field_report.js index 04f374803..6f304a1fd 100644 --- a/src/ims/element/static/field_report.js +++ b/src/ims/element/static/field_report.js @@ -35,8 +35,8 @@ async function initFieldReportPage() { } }); - // Updates...it's fine to ignore the returned promise here - requestEventSourceLock(); + // Fire-and-forget this promise, since it tries forever to acquire a lock + let ignoredPromise = requestEventSourceLock(); const fieldReportChannel = new BroadcastChannel(fieldReportChannelName); fieldReportChannel.onmessage = async function (e) { @@ -312,40 +312,36 @@ async function editSummary() { async function makeIncident() { // Create the new incident - { - const incidentsURL = urlReplace(url_incidents); + const incidentsURL = urlReplace(url_incidents); - const authors = []; - if (fieldReport.report_entries?.length > 0) { - authors.push(fieldReport.report_entries[0].author); - } - let {resp, err} = await fetchJsonNoThrow(incidentsURL, { - body:{ - "summary": fieldReport.summary, - "ranger_handles": authors, - }, - }) - if (err != null) { - disableEditing(); - setErrorMessage(`Failed to create incident: ${err}`); - return; - } - fieldReport.incident = parseInt(resp.headers.get("X-IMS-Incident-Number")); + const authors = []; + if (fieldReport.report_entries?.length > 0) { + authors.push(fieldReport.report_entries[0].author); } + let {resp, err} = await fetchJsonNoThrow(incidentsURL, { + body:{ + "summary": fieldReport.summary, + "ranger_handles": authors, + }, + }) + if (err != null) { + disableEditing(); + setErrorMessage(`Failed to create incident: ${err}`); + return; + } + fieldReport.incident = parseInt(resp.headers.get("X-IMS-Incident-Number")); // Attach this FR to that new incident - { - const attachToIncidentUrl = - `${urlReplace(url_fieldReports)}${fieldReport.number}` + - `?action=attach;incident=${fieldReport.incident}`; - let {err} = await fetchJsonNoThrow(attachToIncidentUrl, { - body: {}, - }); - if (err != null) { - disableEditing(); - setErrorMessage(`Failed to attach field report: ${err}`); - return; - } + const attachToIncidentUrl = + `${urlReplace(url_fieldReports)}${fieldReport.number}` + + `?action=attach;incident=${fieldReport.incident}`; + let {attachErr} = await fetchJsonNoThrow(attachToIncidentUrl, { + body: {}, + }); + if (attachErr != null) { + disableEditing(); + setErrorMessage(`Failed to attach field report: ${attachErr}`); + return; } console.log("Created and attached to new incident " + fieldReport.incident); await loadAndDisplayFieldReport(); diff --git a/src/ims/element/static/field_reports.js b/src/ims/element/static/field_reports.js index e2c529282..f41c33d9f 100644 --- a/src/ims/element/static/field_reports.js +++ b/src/ims/element/static/field_reports.js @@ -75,8 +75,8 @@ function initFieldReportsTable() { enableEditing(); } - // it's ok to ignore the returned promise - requestEventSourceLock(); + // Fire-and-forget this promise, since it tries forever to acquire a lock + let ignoredPromise = requestEventSourceLock(); const fieldReportChannel = new BroadcastChannel(fieldReportChannelName); fieldReportChannel.onmessage = function (e) { if (e.data["update_all"]) { diff --git a/src/ims/element/static/incident.js b/src/ims/element/static/incident.js index ed943c925..a71bdabf5 100644 --- a/src/ims/element/static/incident.js +++ b/src/ims/element/static/incident.js @@ -43,8 +43,8 @@ async function initIncidentPage() { } }); - // Updates...it's good to ignore the returned promise here - requestEventSourceLock(); + // Fire-and-forget this promise, since it tries forever to acquire a lock + let ignoredPromise = requestEventSourceLock(); const incidentChannel = new BroadcastChannel(incidentChannelName); incidentChannel.onmessage = async function (e) { @@ -291,12 +291,13 @@ async function loadAllFieldReports() { return {err: null}; } -async function loadFieldReport(fieldReportNumber, success) { +async function loadFieldReport(fieldReportNumber) { if (allFieldReports === undefined) { return; } - const {resp, json, err} = await fetchJsonNoThrow(urlReplace(url_fieldReport).replace("", fieldReportNumber)) + const {resp, json, err} = await fetchJsonNoThrow( + urlReplace(url_fieldReport).replace("", fieldReportNumber)); if (err != null) { if (resp.status !== 403) { const message = `Failed to load field report ${fieldReportNumber} ${err}`; @@ -781,11 +782,10 @@ async function sendEdits(edits) { }); if (err != null) { - const message = "Failed to apply edit"; - console.log(message + ": " + err); + const message = `Failed to apply edit: ${err}`; await loadAndDisplayIncident(); setErrorMessage(message); - return {err: err} + return {err: message} } if (number == null) { @@ -1035,13 +1035,11 @@ async function detachFieldReport(sender) { async function attachFieldReport() { if (incidentNumber == null) { - // Incident doesn't exist yet. Create it and then retry. + // Incident doesn't exist yet. Create it first. const {err} = await sendEdits({}); if (err != null) { return; } - await attachFieldReport(); - return; } const select = $("#attached_field_report_add"); diff --git a/src/ims/element/static/incidents.js b/src/ims/element/static/incidents.js index 7548e89e9..49b1b6819 100644 --- a/src/ims/element/static/incidents.js +++ b/src/ims/element/static/incidents.js @@ -122,8 +122,8 @@ function initIncidentsTable() { enableEditing(); } - // ok to ignore returned Promise...have the tab wait for the lock - requestEventSourceLock(); + // Fire-and-forget this promise, since it tries forever to acquire a lock + let ignoredPromise = requestEventSourceLock(); const incidentChannel = new BroadcastChannel(incidentChannelName); incidentChannel.onmessage = async function (e) { if (e.data["update_all"]) {