Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions apps/oxfmt/src-js/prettier-proxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@ import Tinypool from "tinypool";
import type { WorkerData, FormatEmbeddedCodeArgs, FormatFileArgs } from "./prettier-worker.ts";

// Worker pool for parallel Prettier formatting
// Used by each exported function
let pool: Tinypool | null = null;

type SetupResult = string[];
let setupCache: SetupResult | null = null;

// ---

/**
Expand All @@ -13,14 +17,17 @@ let pool: Tinypool | null = null;
* @param numThreads - Number of worker threads to use (same as Rayon thread count)
* @returns Array of loaded plugin's `languages` info
* */
export async function setupConfig(configJSON: string, numThreads: number): Promise<string[]> {
export async function setupConfig(configJSON: string, numThreads: number): Promise<SetupResult> {
// NOTE: When called from CLI, it's only called once at the beginning.
// However, when called via API, like `format(fileName, code)`, it may be called multiple times.
// Therefore, allow it by returning cached result.
if (setupCache !== null) return setupCache;

const workerData: WorkerData = {
// SAFETY: Always valid JSON constructed by Rust side
prettierConfig: JSON.parse(configJSON),
};

if (pool) throw new Error("`setupConfig()` has already been called");

// Initialize worker pool for parallel Prettier formatting
// Pass config via workerData so all workers get it on initialization
pool = new Tinypool({
Expand All @@ -34,7 +41,9 @@ export async function setupConfig(configJSON: string, numThreads: number): Promi
// - Read `plugins` field
// - Load plugins dynamically and parse `languages` field
// - Map file extensions and filenames to Prettier parsers
return [];
setupCache = [];

return setupCache;
}

// ---
Expand Down
Loading