From ec5c2f99528414479dddb0531de407c3461d994e Mon Sep 17 00:00:00 2001 From: Odonno Date: Thu, 3 Oct 2024 12:08:11 +0200 Subject: [PATCH] chore: lazy import adapter --- src/adapter/base.tsx | 16 +++++----------- src/adapter/browser.tsx | 28 ++++++++++++++-------------- src/adapter/desktop.tsx | 2 +- src/adapter/index.tsx | 15 ++++++--------- src/adapter/mini.tsx | 6 ++++-- 5 files changed, 30 insertions(+), 37 deletions(-) diff --git a/src/adapter/base.tsx b/src/adapter/base.tsx index 8548ba2e..de290f61 100644 --- a/src/adapter/base.tsx +++ b/src/adapter/base.tsx @@ -10,11 +10,13 @@ export interface OpenedBinaryFile { content: Blob; } +export type SurrealistAdapterType = "browser" | "desktop" | "mini"; + export interface SurrealistAdapter { /** * Identifier for this adapter */ - id: string; + id: SurrealistAdapterType; /** * Returns whether local database serving is supported @@ -97,20 +99,12 @@ export interface SurrealistAdapter { /** * Open a text file locally */ - openTextFile( - title: string, - filters: any, - multiple: boolean, - ): Promise; + openTextFile(title: string, filters: any, multiple: boolean): Promise; /** * Open a binary file locally */ - openBinaryFile( - title: string, - filters: any, - multiple: boolean, - ): Promise; + openBinaryFile(title: string, filters: any, multiple: boolean): Promise; /** * Log a message to the implemented logging system diff --git a/src/adapter/browser.tsx b/src/adapter/browser.tsx index 1b41ed9f..a50f504d 100644 --- a/src/adapter/browser.tsx +++ b/src/adapter/browser.tsx @@ -3,13 +3,14 @@ import type { OpenedBinaryFile, OpenedTextFile, SurrealistAdapter, + SurrealistAdapterType, } from "./base"; /** - * Surrealist adapter for running as web app + * Base adapter for running as web app */ -export class BrowserAdapter implements SurrealistAdapter { - public id = "browser"; +export abstract class BaseBrowserAdapter implements SurrealistAdapter { + abstract id: SurrealistAdapterType; public isServeSupported = false; public isUpdateCheckSupported = false; @@ -41,10 +42,7 @@ export class BrowserAdapter implements SurrealistAdapter { const config = localStorage.getItem("surrealist:config") || "{}"; const parsed = JSON.parse(config); - if ( - parsed.configVersion === undefined && - Object.keys(parsed).length > 0 - ) { + if (parsed.configVersion === undefined && Object.keys(parsed).length > 0) { return {}; } @@ -80,9 +78,7 @@ export class BrowserAdapter implements SurrealistAdapter { } const file = - typeof result === "string" - ? new File([result], "", { type: "text/plain" }) - : result; + typeof result === "string" ? new File([result], "", { type: "text/plain" }) : result; const url = window.URL.createObjectURL(file); const el = document.createElement("a"); @@ -166,10 +162,14 @@ export class BrowserAdapter implements SurrealistAdapter { console.debug(`${label}: ${message}`); } - public fetch( - url: string, - options?: RequestInit | undefined, - ): Promise { + public fetch(url: string, options?: RequestInit | undefined): Promise { return fetch(url, options); } } + +/** + * Surrealist adapter for running as web app + */ +export class BrowserAdapter extends BaseBrowserAdapter { + public id = "browser" as const; +} diff --git a/src/adapter/desktop.tsx b/src/adapter/desktop.tsx index 55b2d698..0a53899a 100644 --- a/src/adapter/desktop.tsx +++ b/src/adapter/desktop.tsx @@ -46,7 +46,7 @@ interface LinkResource { * Surrealist adapter for running as Wails desktop app */ export class DesktopAdapter implements SurrealistAdapter { - public id = "desktop"; + public id = "desktop" as const; public isServeSupported = true; public isUpdateCheckSupported = true; diff --git a/src/adapter/index.tsx b/src/adapter/index.tsx index f771cc3b..66f39d44 100644 --- a/src/adapter/index.tsx +++ b/src/adapter/index.tsx @@ -1,15 +1,12 @@ import type { SurrealistAdapter } from "./base"; -import { BrowserAdapter } from "./browser"; -import { DesktopAdapter } from "./desktop"; -import { MiniAdapter } from "./mini"; export const adapter: SurrealistAdapter = "__TAURI_INTERNALS__" in window - ? new DesktopAdapter() + ? new (await import("./desktop")).DesktopAdapter() : document.querySelector("meta[name=surrealist-mini]") - ? new MiniAdapter() - : new BrowserAdapter(); + ? new (await import("./mini")).MiniAdapter() + : new (await import("./browser")).BrowserAdapter(); -export const isDesktop = adapter instanceof DesktopAdapter; -export const isBrowser = adapter instanceof BrowserAdapter; -export const isMini = adapter instanceof MiniAdapter; +export const isDesktop = adapter.id === "desktop"; +export const isBrowser = adapter.id === "browser"; +export const isMini = adapter.id === "mini"; diff --git a/src/adapter/mini.tsx b/src/adapter/mini.tsx index 6ed9aab2..9ec84871 100644 --- a/src/adapter/mini.tsx +++ b/src/adapter/mini.tsx @@ -8,11 +8,13 @@ import { createBaseSettings, createBaseTab, createSandboxConnection } from "~/ut import { showError } from "~/util/helpers"; import { broadcastMessage } from "~/util/messaging"; import { parseDatasetURL } from "~/util/surrealql"; -import { BrowserAdapter } from "./browser"; +import { BaseBrowserAdapter } from "./browser"; const THEMES = new Set(["light", "dark", "auto"]); -export class MiniAdapter extends BrowserAdapter { +export class MiniAdapter extends BaseBrowserAdapter { + public id = "mini" as const; + public appearance: MiniAppearance = "normal"; public corners: string | undefined = undefined; public transparent = false;