Skip to content
Source
Sign inSign up
fuko/trytanstackdb-v2
Open History to see the last commit

dev-console.ts

47 lines1.7 KB

1import { setEditorSaveDelay } from "@/lib/dev-config";
2​
3import { getNetworkConfig, setNetworkConfig } from "./network-config";
4import { clearRequestLog, requestLogStore } from "./request-log";
5​
6/**
7 * Dev only: the fake API's knobs on `window.__fakeApi`, until phase 4 puts
8 * them in the browser toolbar.
9 *
10 * __fakeApi.setNetworkConfig({ latencyMs: 2000 })
11 * __fakeApi.setNetworkConfig({ failNextRequest: true })
12 * console.table(__fakeApi.requests())
13 *
14 * For scripted browsers that can't run JS, the page URL can set them too,
15 * once on load: `?latency=3000` (ms), `?failNext=1` and `?saveDelay=4000`
16 * (the editor's wait before saving, in ms), also on deep links
17 * like `/folders/work/notes/note-07?latency=3000`.
18 */
19export function exposeFakeApiOnWindow() {
20 if (!import.meta.env.DEV) return;
21 Object.assign(window, {
22 __fakeApi: {
23 getNetworkConfig,
24 setNetworkConfig,
25 requests: () => requestLogStore.get(),
26 clearRequests: clearRequestLog,
27 },
28 });
30​
31/**
32 * Applied when this module loads: before any route renders, so before the
33 * app's first requests and before a redirect (`<Navigate replace>`) could
34 * replace the URL without its query string.
35 */
36if (import.meta.env.DEV && typeof window !== "undefined") {
37 const params = new URLSearchParams(window.location.search);
38 const latency = Number(params.get("latency"));
39 if (params.has("latency") && Number.isFinite(latency) && latency >= 0) {
40 setNetworkConfig({ latencyMs: latency });
41 }
42 if (params.get("failNext") === "1") setNetworkConfig({ failNextRequest: true });
43 const saveDelay = Number(params.get("saveDelay"));
44 if (params.has("saveDelay") && Number.isFinite(saveDelay) && saveDelay >= 0) {
45 setEditorSaveDelay(saveDelay);
46 }