Skip to content

Build your personal knowledge base with Trilium Notes

1.8 KB
Raw Blame
/**
 * Interface for platform-specific services. This is used to abstract away platform-specific implementations, such as crash reporting, from the core logic of the application.
 */
export interface PlatformProvider {
    crash(message: string): void;
    /** Returns the value of an environment variable, or undefined if not set. */
    getEnv(key: string): string | undefined;
    readonly isElectron: boolean;
    /**
     * `true` in the standalone build, where core runs inside a browser worker rather than on Node.
     * The OS flags below stay honest there, so this is what tells standalone apart from a desktop.
     */
    readonly isStandalone: boolean;
    readonly isMac: boolean;
    readonly isWindows: boolean;
    readonly isLinux: boolean;
    /**
     * Where the database file is, for the platforms whose database is a file the user can reach.
     * Null where it is not — the browser build keeps it in storage the browser owns, which has no
     * path to give — and absent where a platform does not answer at all.
     */
    getDatabasePath?(): string | null;
    /**
     * Lets a platform decide whether an HTTP-server startup error should be
     * swallowed (logged-only) rather than treated as fatal. The desktop uses
     * this to tolerate `EADDRINUSE` when launched with `--new-window` or as a
     * secondary instance — the primary handles the request via Electron's
     * `second-instance` event. Optional; absent = always fatal.
     */
    shouldIgnoreStartupError?(error: NodeJS.ErrnoException): boolean;
}

let platformProvider: PlatformProvider | null = null;

export function initPlatform(provider: PlatformProvider) {
    platformProvider = provider;
}

export function getPlatform(): PlatformProvider {
    if (!platformProvider) throw new Error("Platform provider not initialized");
    return platformProvider;
}