UNPKG

marko

Version:

Optimized runtime for Marko templates.

210 lines (209 loc) • 9.68 kB
import { _hoist_read_error } from "../common/errors"; import { type Opt } from "../common/opt"; import { type $Global, type Accessor, ResumeSymbol } from "../common/types"; import * as FlushStatus from "./constants/flush-status"; import * as Mark from "./constants/mark"; import { K_SCOPE_ID, type Locals, type SerializeChannel, SerializerState } from "./serializer"; import type { ServerRenderer } from "./template"; export type PartialScope = Record<Accessor, unknown>; interface Channel extends SerializeChannel { parent?: Channel; resumes: string; writeScopes: Record<number, PartialScope>; passiveScopes?: Record<number, PartialScope>; flushScopes: boolean; } export type ScopeInternals = PartialScope & { [K_SCOPE_ID]?: number; }; export declare let $chunk: Chunk; export declare function getChunk(): Chunk | undefined; export declare function withChunk<T>(chunk: Chunk, cb: () => T): T; export declare function getContext(key: keyof NonNullable<Chunk["context"]>): unknown; export declare function getState(): State; export declare function rendererKey(renderer: unknown): unknown; export declare function getScopeId(scope: unknown): number | undefined; export declare function getScopeById(scopeId: number | undefined): ScopeInternals | undefined; export declare function $global(): $Global & { renderId: string; runtimeId: string; }; export declare function _id(): string; export declare function _scope_id(): number; export declare function _peek_scope_id(): number; export declare function withContext<T>(key: PropertyKey, value: unknown, cb: () => T): T; export declare function withContext<T, U>(key: PropertyKey, value: unknown, cb: (value: U) => T, cbValue: U): T; export declare function withContext<T, U, V>(key: PropertyKey, value: unknown, cb: (value: U, value2: V) => T, cbValue: U, cbValue2: V): T; export declare function captureContext(chunk: Chunk): void; export declare const kBranchId: unique symbol; export interface AsyncContent { visit?: string; end?: Chunk; parent?: AsyncContent; resumed?: true; } export declare function isInResumedBranch(): boolean; export declare function withBranchId<T>(branchId: number, cb: () => T): T; export declare function withBranchId<T, U>(branchId: number, cb: (value: U) => T, cbValue: U): T; export declare function withBranchId<T, U, V>(branchId: number, cb: (value: U, value2: V) => T, cbValue: U, cbValue2: V): T; export declare function withAsyncContent<T, U>(async: AsyncContent, cb: (value: U) => T, value: U): T; export declare function _html(html: string): void; export declare function writeScript(script: string): void; export declare function catchableBoundary(): Boundary | null; export declare function mayDrop(written: Boundary): boolean; export declare function _script(scopeId: number, registryId: string, markerGuard?: number): void; export declare function _trailers(html: string): void; export declare function _resume<T extends WeakKey>(val: T, id: string, scopeId?: number, locals?: Locals): T; export declare function _resume_locals<T extends WeakKey>(val: T, id: string, locals: Record<string, unknown>, ownerScopeId?: number): T; export declare function _el(scopeId: number, id: string): () => void; export declare function _hoist(scopeId: number, id: string): { (): void; [Symbol.iterator]: typeof _hoist_read_error; }; export declare function _el_resume(scopeId: number, accessor: Accessor, markerGuard?: number): string; export declare function _text_resume(scopeId: number, accessor: Accessor, val: unknown, markerGuard?: number): string; export declare function _html_resume(scopeId: number, accessor: Accessor, val: unknown, markerGuard?: number): string; export declare function _resume_branch(scopeId: number): void; export declare function _attr_content(nodeAccessor: Accessor, scopeId: number, content: unknown, markerGuard?: number): void; export declare function _var(parentScopeId: number, scopeOffsetAccessor: Accessor, childScopeId: number, registryId: string, nodeAccessor?: Accessor): void; declare let writeScope: (scopeId: number, partialScope: PartialScope) => ScopeInternals; export { writeScope as _scope }; export declare function _existing_scope(scopeId: number): ScopeInternals; export declare function _scope_with_id(scopeId: number): ScopeInternals; export declare function _subscribe(subscribers: Set<ScopeInternals> | undefined, scope: ScopeInternals, resumeId?: string, markerGuard?: number): ScopeInternals; export type GroupMask = undefined | number | Partial<Record<string, number>>; export declare const ALL_GROUPS = 715827883; export declare function getGroupReason(reason: GroupMask, group: number): number; export declare function _set_scope_reason(reason: GroupMask): void; export declare function _scope_reason(): GroupMask; export declare function _write_if(reason: GroupMask, group: number): 1 | undefined; export declare function _write_guard(reason: GroupMask, group: number): 0 | 1; export declare function writeWaitReady(readyId: string, renderer: ServerRenderer, input: unknown): void; export declare function deferBranchStart(chunk: Chunk): string; export declare function applyBranchStart(chunk: Chunk, beforeBranch: string, rendered: boolean): void; declare const NOOP: () => void; export declare function getAsyncContent(chunk: Chunk): AsyncContent | undefined; type Mark = Mark.Value; export declare class State implements Channel { tagId: number; scopeId: number; reorderId: number; readyGate: number; hasWrittenGlobals: boolean; needsWalkerRuntime: boolean; hasWalkerRuntime: boolean; hasWrittenReady: boolean; hasReorderRuntime: boolean; hasWrittenResume: boolean; walkOnNextFlush: boolean; trailerHTML: string; resumes: string; nonceAttr: string; serializer: SerializerState; writeReorders: Chunk[] | null; scopes: Map<number, ScopeInternals>; scope: (scopeId: number) => ScopeInternals; flushScopes: boolean; writeScopes: Record<number, PartialScope>; readyIds: Set<string> | null; scopeReason: GroupMask; $global: $Global & { renderId: string; runtimeId: string; }; constructor($global: $Global & { renderId: string; runtimeId: string; }); joinFlush(html: string, scripts: string, pending: number): string; walkScript(): string; addResumes(channel: Channel, resumes: string): void; queueScope(target: Channel, scopeId: number, partialScope: PartialScope): void; queuePassiveScope(target: Channel, scopeId: number, partialScope: PartialScope): void; queueEffect(scopeId: number, registryId: string): void; serializeFlush(boundary: Boundary): void; runtimeScript(): string; keepsTryMarks(chunk: Chunk, resumeWrites: number): boolean; resumeScript(resumes: string): string; get runtimePrefix(): string; get commentPrefix(): string; reorder(chunk: Chunk): void; readyScript(id: string, resumes: string): string; readyAccess(readyKey: string): string; nextReorderId(): string; mark(code: ResumeSymbol | Mark, str: string): string; } type FlushStatus = FlushStatus.Value; export { FlushStatus }; export declare class Boundary { onNext: typeof NOOP; count: number; resumeWrites: number; state: State; parent?: Boundary; aborted: boolean; reason: unknown; subscribed?: unknown[]; pendingReorders?: Set<Chunk>; children?: Boundary[]; withinCatch: boolean; constructor(state: State, signal?: AbortSignal, parent?: Boundary); abort(reason: unknown): void; handleEvent(event: Event): void; flush(): 0 | 1 | 2; startAsync(): void; endAsync(): void; } export declare class Chunk { html: string; scripts: string; effects: string; lastEffect: string; async: boolean; consumed: boolean; reorderId: string | null; reorderBranch: number; deferredReady: Opt<Chunk>; heldEffects: Opt<Chunk>; deferredReorder: Chunk | null; catchRange: { id: string; end: Chunk; } | null; placeholder: { body: Chunk; render: () => void; branchId: number; scopeId: number; placeholderBranchId: number; } | null; boundary: Boundary; next: Chunk | null; context: Record<string | symbol, unknown> | null; channel: Channel; constructor(boundary: Boundary, next: Chunk | null, context: Record<string | symbol, unknown> | null, channel: Channel); fork(boundary: Boundary, next: Chunk | null): Chunk; forkRest(boundary: Boundary): Chunk; writeHTML(html: string): void; writeEffect(scopeId: number, registryId: string): void; writeScript(script: string): void; append(chunk: Chunk): void; takeHeldEffects(): Opt<Chunk>; takeDeferredReady(): Opt<Chunk>; deferOwnReady(): void; renderPlaceholders(boundary: Boundary): void; renderPlaceholder(state: State): boolean; markCatchRange(): void; queueDeferredReorder(): void; consume(boundary: Boundary): Chunk; render(content: () => void): Chunk; render<T>(content: (val: T) => void, val: T): Chunk; render<T, U>(content: (val: T, val2: U) => void, val: T, val2: U): Chunk; flushReadyScripts(boundary: Boundary, reservations?: string[], holdEffects?: boolean): string; flush(boundary: Boundary): this; flushHTML(boundary: Boundary): string; } export declare function joinHeldEffects(heldEffects: Opt<Chunk>, effects: string): string; type QueueCallback = (ticked: true) => void; export declare function queueTick(cb: QueueCallback): void; export declare function offTick(cb: QueueCallback): void;