Skip to content

Commit

Permalink
chore: lazy import adapter
Browse files Browse the repository at this point in the history
  • Loading branch information
Odonno authored and macjuul committed Oct 17, 2024
1 parent 8cf4922 commit ec5c2f9
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 37 deletions.
16 changes: 5 additions & 11 deletions src/adapter/base.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -97,20 +99,12 @@ export interface SurrealistAdapter {
/**
* Open a text file locally
*/
openTextFile(
title: string,
filters: any,
multiple: boolean,
): Promise<OpenedTextFile[]>;
openTextFile(title: string, filters: any, multiple: boolean): Promise<OpenedTextFile[]>;

/**
* Open a binary file locally
*/
openBinaryFile(
title: string,
filters: any,
multiple: boolean,
): Promise<OpenedBinaryFile[]>;
openBinaryFile(title: string, filters: any, multiple: boolean): Promise<OpenedBinaryFile[]>;

/**
* Log a message to the implemented logging system
Expand Down
28 changes: 14 additions & 14 deletions src/adapter/browser.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 {};
}

Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -166,10 +162,14 @@ export class BrowserAdapter implements SurrealistAdapter {
console.debug(`${label}: ${message}`);
}

public fetch(
url: string,
options?: RequestInit | undefined,
): Promise<Response> {
public fetch(url: string, options?: RequestInit | undefined): Promise<Response> {
return fetch(url, options);
}
}

/**
* Surrealist adapter for running as web app
*/
export class BrowserAdapter extends BaseBrowserAdapter {
public id = "browser" as const;
}
2 changes: 1 addition & 1 deletion src/adapter/desktop.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
15 changes: 6 additions & 9 deletions src/adapter/index.tsx
Original file line number Diff line number Diff line change
@@ -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";
6 changes: 4 additions & 2 deletions src/adapter/mini.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit ec5c2f9

Please sign in to comment.