@mui/internal-docs-infra
Version:
MUI Infra - internal documentation creation tools.
47 lines • 2.15 kB
text/typescript
import type { Root as HastRoot } from 'hast';
import type { createStarryNight } from '@wooorm/starry-night';
type Grammar = Parameters<typeof createStarryNight>[0][number];
/**
* Asynchronously parses source code into a HAST tree, typically off the main
* thread (e.g. via a Web Worker). Used during live typing so the UI thread
* stays responsive. Accepts an `AbortSignal` so a stale request can be
* cancelled when newer keystrokes arrive.
*
* Internal: not exported as part of the public API. Consumers wire up async
* parsing by passing a worker URL to `CodeProvider`; the produced client
* exposes a `parseSourceAsync` callable directly without referring to this
* type.
*/
export type ParseSourceAsync = (source: string, fileName: string, language?: string, signal?: AbortSignal) => Promise<HastRoot>;
export interface ParseSourceWorkerClient {
/**
* Send the (heavy) grammar payload to the worker. Idempotent: subsequent
* calls return the same promise. Must be awaited (or composed via
* `parseSourceAsync`, which awaits it implicitly) before parse requests
* will be processed.
*/
init(grammars: Grammar[]): Promise<void>;
/**
* Add more grammars to an already-initialized worker (the per-language path:
* a block becomes editable in a language the worker wasn't initialized with).
* Resolves on the worker's `register-ack`. Call after `init()`.
*/
register(grammars: Grammar[]): Promise<void>;
/**
* Async syntax-highlighter that runs inside the worker. Returns the same
* HAST shape as the sync `parseSource`. If `signal` aborts before the
* worker responds, the in-flight request is dropped and the promise
* rejects with `signal.reason`.
*/
parseSourceAsync: ParseSourceAsync;
terminate(): void;
}
/**
* Create a worker-backed `parseSourceAsync` implementation. The caller owns
* the lifecycle and must invoke `terminate()` on unmount.
*
* Each client owns exactly one underlying `Worker`. Concurrent in-flight
* requests are demuxed by a monotonically increasing `id`.
*/
export declare function createParseSourceWorkerClient(): ParseSourceWorkerClient;
export {};