refactor(client): circular dep: toast <-> ws

This commit is contained in:
Elian Doran 2025-06-19 22:29:44 +03:00
parent 50db8ef9c3
commit 09391a92e5
No known key found for this signature in database
6 changed files with 21 additions and 24 deletions

View File

@ -1,6 +1,6 @@
import ScriptContext from "./script_context.js"; import ScriptContext from "./script_context.js";
import server from "./server.js"; import server from "./server.js";
import toastService from "./toast.js"; import toastService, { showError } from "./toast.js";
import froca from "./froca.js"; import froca from "./froca.js";
import utils from "./utils.js"; import utils from "./utils.js";
import { t } from "./i18n.js"; import { t } from "./i18n.js";
@ -37,7 +37,9 @@ async function executeBundle(bundle: Bundle, originEntity?: Entity | null, $cont
} catch (e: any) { } catch (e: any) {
const note = await froca.getNote(bundle.noteId); const note = await froca.getNote(bundle.noteId);
toastService.showAndLogError(`Execution of JS note "${note?.title}" with ID ${bundle.noteId} failed with error: ${e?.message}`); const message = `Execution of JS note "${note?.title}" with ID ${bundle.noteId} failed with error: ${e?.message}`;
showError(message);
logError(message);
} }
} }

View File

@ -4,7 +4,7 @@ import froca from "./froca.js";
import linkService from "./link.js"; import linkService from "./link.js";
import utils from "./utils.js"; import utils from "./utils.js";
import { t } from "./i18n.js"; import { t } from "./i18n.js";
import toast from "./toast.js"; import { throwError } from "./ws.js";
let clipboardBranchIds: string[] = []; let clipboardBranchIds: string[] = [];
let clipboardMode: string | null = null; let clipboardMode: string | null = null;
@ -37,7 +37,7 @@ async function pasteAfter(afterBranchId: string) {
// copy will keep clipboardBranchIds and clipboardMode, so it's possible to paste into multiple places // copy will keep clipboardBranchIds and clipboardMode, so it's possible to paste into multiple places
} else { } else {
toastService.throwError(`Unrecognized clipboard mode=${clipboardMode}`); throwError(`Unrecognized clipboard mode=${clipboardMode}`);
} }
} }
@ -69,7 +69,7 @@ async function pasteInto(parentBranchId: string) {
// copy will keep clipboardBranchIds and clipboardMode, so it's possible to paste into multiple places // copy will keep clipboardBranchIds and clipboardMode, so it's possible to paste into multiple places
} else { } else {
toastService.throwError(`Unrecognized clipboard mode=${clipboardMode}`); throwError(`Unrecognized clipboard mode=${clipboardMode}`);
} }
} }

View File

@ -1,5 +1,5 @@
import { t } from "./i18n.js"; import { t } from "./i18n.js";
import toastService from "./toast.js"; import toastService, { showError } from "./toast.js";
function copyImageReferenceToClipboard($imageWrapper: JQuery<HTMLElement>) { function copyImageReferenceToClipboard($imageWrapper: JQuery<HTMLElement>) {
try { try {
@ -11,7 +11,9 @@ function copyImageReferenceToClipboard($imageWrapper: JQuery<HTMLElement>) {
if (success) { if (success) {
toastService.showMessage(t("image.copied-to-clipboard")); toastService.showMessage(t("image.copied-to-clipboard"));
} else { } else {
toastService.showAndLogError(t("image.cannot-copy")); const message = t("image.cannot-copy");
showError(message);
logError(message);
} }
} finally { } finally {
window.getSelection()?.removeAllRanges(); window.getSelection()?.removeAllRanges();

View File

@ -1,5 +1,6 @@
import utils, { isShare } from "./utils.js"; import utils, { isShare } from "./utils.js";
import ValidationError from "./validation_error.js"; import ValidationError from "./validation_error.js";
import { throwError } from "./ws.js";
type Headers = Record<string, string | null | undefined>; type Headers = Record<string, string | null | undefined>;
@ -276,7 +277,7 @@ async function reportError(method: string, url: string, statusCode: number, resp
} else { } else {
const title = `${statusCode} ${method} ${url}`; const title = `${statusCode} ${method} ${url}`;
toastService.showErrorTitleAndMessage(title, messageStr); toastService.showErrorTitleAndMessage(title, messageStr);
toastService.throwError(`${title} - ${message}`); throwError(`${title} - ${message}`);
} }
} }

View File

@ -78,13 +78,7 @@ function showMessage(message: string, delay = 2000) {
}); });
} }
function showAndLogError(message: string, delay = 10000) { export function showError(message: string, delay = 10000) {
showError(message, delay);
ws.logError(message);
}
function showError(message: string, delay = 10000) {
console.log(utils.now(), "error: ", message); console.log(utils.now(), "error: ", message);
toast({ toast({
@ -108,18 +102,10 @@ function showErrorTitleAndMessage(title: string, message: string, delay = 10000)
}); });
} }
function throwError(message: string) {
ws.logError(message);
throw new Error(message);
}
export default { export default {
showMessage, showMessage,
showError, showError,
showErrorTitleAndMessage, showErrorTitleAndMessage,
showAndLogError,
throwError,
showPersistent, showPersistent,
closePersistent closePersistent
}; };

View File

@ -17,7 +17,7 @@ let lastProcessedEntityChangeId = window.glob.maxEntityChangeIdAtLoad;
let lastPingTs: number; let lastPingTs: number;
let frontendUpdateDataQueue: EntityChange[] = []; let frontendUpdateDataQueue: EntityChange[] = [];
function logError(message: string) { export function logError(message: string) {
console.error(utils.now(), message); // needs to be separate from .trace() console.error(utils.now(), message); // needs to be separate from .trace()
if (ws && ws.readyState === 1) { if (ws && ws.readyState === 1) {
@ -301,6 +301,12 @@ setTimeout(() => {
setInterval(sendPing, 1000); setInterval(sendPing, 1000);
}, 0); }, 0);
export function throwError(message: string) {
logError(message);
throw new Error(message);
}
export default { export default {
logError, logError,
subscribeToMessages, subscribeToMessages,