Skip to content
This repository was archived by the owner on Jun 24, 2025. It is now read-only.

Commit 09391a9

Browse files
committed
refactor(client): circular dep: toast <-> ws
1 parent 50db8ef commit 09391a9

File tree

6 files changed

+21
-24
lines changed

6 files changed

+21
-24
lines changed

apps/client/src/services/bundle.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import ScriptContext from "./script_context.js";
22
import server from "./server.js";
3-
import toastService from "./toast.js";
3+
import toastService, { showError } from "./toast.js";
44
import froca from "./froca.js";
55
import utils from "./utils.js";
66
import { t } from "./i18n.js";
@@ -37,7 +37,9 @@ async function executeBundle(bundle: Bundle, originEntity?: Entity | null, $cont
3737
} catch (e: any) {
3838
const note = await froca.getNote(bundle.noteId);
3939

40-
toastService.showAndLogError(`Execution of JS note "${note?.title}" with ID ${bundle.noteId} failed with error: ${e?.message}`);
40+
const message = `Execution of JS note "${note?.title}" with ID ${bundle.noteId} failed with error: ${e?.message}`;
41+
showError(message);
42+
logError(message);
4143
}
4244
}
4345

apps/client/src/services/clipboard.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import froca from "./froca.js";
44
import linkService from "./link.js";
55
import utils from "./utils.js";
66
import { t } from "./i18n.js";
7-
import toast from "./toast.js";
7+
import { throwError } from "./ws.js";
88

99
let clipboardBranchIds: string[] = [];
1010
let clipboardMode: string | null = null;
@@ -37,7 +37,7 @@ async function pasteAfter(afterBranchId: string) {
3737

3838
// copy will keep clipboardBranchIds and clipboardMode, so it's possible to paste into multiple places
3939
} else {
40-
toastService.throwError(`Unrecognized clipboard mode=${clipboardMode}`);
40+
throwError(`Unrecognized clipboard mode=${clipboardMode}`);
4141
}
4242
}
4343

@@ -69,7 +69,7 @@ async function pasteInto(parentBranchId: string) {
6969

7070
// copy will keep clipboardBranchIds and clipboardMode, so it's possible to paste into multiple places
7171
} else {
72-
toastService.throwError(`Unrecognized clipboard mode=${clipboardMode}`);
72+
throwError(`Unrecognized clipboard mode=${clipboardMode}`);
7373
}
7474
}
7575

apps/client/src/services/image.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { t } from "./i18n.js";
2-
import toastService from "./toast.js";
2+
import toastService, { showError } from "./toast.js";
33

44
function copyImageReferenceToClipboard($imageWrapper: JQuery<HTMLElement>) {
55
try {
@@ -11,7 +11,9 @@ function copyImageReferenceToClipboard($imageWrapper: JQuery<HTMLElement>) {
1111
if (success) {
1212
toastService.showMessage(t("image.copied-to-clipboard"));
1313
} else {
14-
toastService.showAndLogError(t("image.cannot-copy"));
14+
const message = t("image.cannot-copy");
15+
showError(message);
16+
logError(message);
1517
}
1618
} finally {
1719
window.getSelection()?.removeAllRanges();

apps/client/src/services/server.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import utils, { isShare } from "./utils.js";
22
import ValidationError from "./validation_error.js";
3+
import { throwError } from "./ws.js";
34

45
type Headers = Record<string, string | null | undefined>;
56

@@ -276,7 +277,7 @@ async function reportError(method: string, url: string, statusCode: number, resp
276277
} else {
277278
const title = `${statusCode} ${method} ${url}`;
278279
toastService.showErrorTitleAndMessage(title, messageStr);
279-
toastService.throwError(`${title} - ${message}`);
280+
throwError(`${title} - ${message}`);
280281
}
281282
}
282283

apps/client/src/services/toast.ts

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -78,13 +78,7 @@ function showMessage(message: string, delay = 2000) {
7878
});
7979
}
8080

81-
function showAndLogError(message: string, delay = 10000) {
82-
showError(message, delay);
83-
84-
ws.logError(message);
85-
}
86-
87-
function showError(message: string, delay = 10000) {
81+
export function showError(message: string, delay = 10000) {
8882
console.log(utils.now(), "error: ", message);
8983

9084
toast({
@@ -108,18 +102,10 @@ function showErrorTitleAndMessage(title: string, message: string, delay = 10000)
108102
});
109103
}
110104

111-
function throwError(message: string) {
112-
ws.logError(message);
113-
114-
throw new Error(message);
115-
}
116-
117105
export default {
118106
showMessage,
119107
showError,
120108
showErrorTitleAndMessage,
121-
showAndLogError,
122-
throwError,
123109
showPersistent,
124110
closePersistent
125111
};

apps/client/src/services/ws.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ let lastProcessedEntityChangeId = window.glob.maxEntityChangeIdAtLoad;
1717
let lastPingTs: number;
1818
let frontendUpdateDataQueue: EntityChange[] = [];
1919

20-
function logError(message: string) {
20+
export function logError(message: string) {
2121
console.error(utils.now(), message); // needs to be separate from .trace()
2222

2323
if (ws && ws.readyState === 1) {
@@ -301,6 +301,12 @@ setTimeout(() => {
301301
setInterval(sendPing, 1000);
302302
}, 0);
303303

304+
export function throwError(message: string) {
305+
logError(message);
306+
307+
throw new Error(message);
308+
}
309+
304310
export default {
305311
logError,
306312
subscribeToMessages,

0 commit comments

Comments
 (0)