UNPKG

marko

Version:

Optimized runtime for Marko templates.

169 lines (168 loc) • 8.8 kB
import { _hoist_read_error } from "../common/errors"; import { type Opt } from "../common/opt"; import { type $Global, type Accessor, type Falsy, ResumeSymbol } from "../common/types"; import * as FlushStatus from "./constants/flush-status"; import * as Mark from "./constants/mark"; import { K_SCOPE_ID, Serializer } from "./serializer"; import type { ServerRenderer } from "./template"; export type PartialScope = Record<Accessor, unknown>; interface SerializeState { readyId?: string; parent?: SerializeState; resumes: string; writeScopes: Record<number, PartialScope>; passiveScopes?: Record<number, PartialScope>; flushScopes: boolean; } type ScopeInternals = PartialScope & { [K_SCOPE_ID]?: number; }; export declare function getChunk(): Chunk | undefined; export declare function getContext(key: keyof NonNullable<Chunk["context"]>): unknown; export declare function getState(): State; 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 isInResumedBranch(): boolean; export declare function withBranchId<T>(branchId: number, cb: () => T): T; export declare function _html(html: string): void; export declare function writeScript(script: string): void; export declare function _script(scopeId: number, registryId: string): void; export declare function _trailers(html: string): void; export declare function _resume<T extends WeakKey>(val: T, id: string, scopeId?: number): 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, shouldResume?: number): string; export declare function _sep(shouldResume: number): "" | "<!>"; export declare function _resume_branch(scopeId: number): void; export declare function _attr_content(nodeAccessor: Accessor, scopeId: number, content: unknown, serializeReason?: number): void; export declare function _var(parentScopeId: number, scopeOffsetAccessor: Accessor, childScopeId: number, registryId: string, nodeAccessor?: Accessor): void; export declare function _show_start(display: unknown, mark?: unknown): void; export declare function _show_end(scopeId: number, accessor: Accessor, display: unknown, serializeMarker?: number, serializeStateful?: number, parentEndTag?: string | 0, singleNode?: 1 | 0): void; export declare function _for_of(list: Falsy | Iterable<unknown>, cb: (item: unknown, index: number) => void, by: Falsy | ((item: unknown, index: number) => unknown), scopeId: number, accessor: Accessor, serializeBranch?: number, serializeMarker?: number, serializeStateful?: number, parentEndTag?: string | 0, singleNode?: 1): void; export declare function _for_in(obj: Falsy | {}, cb: (key: string, value: unknown) => void, by: Falsy | ((key: string, v: unknown) => unknown), scopeId: number, accessor: Accessor, serializeBranch?: number, serializeMarker?: number, serializeStateful?: number, parentEndTag?: string | 0, singleNode?: 1): void; export declare function _for_to(to: number, from: number | Falsy, step: number | Falsy, cb: (index: number) => void, by: Falsy | ((v: number) => unknown), scopeId: number, accessor: Accessor, serializeBranch?: number, serializeMarker?: number, serializeStateful?: number, parentEndTag?: string | 0, singleNode?: 1): void; export declare function _for_until(to: number, from: number | Falsy, step: number | Falsy, cb: (index: number) => void, by: Falsy | ((v: number) => unknown), scopeId: number, accessor: Accessor, serializeBranch?: number, serializeMarker?: number, serializeStateful?: number, parentEndTag?: string | 0, singleNode?: 1): void; export declare function _if(cb: () => void | number, scopeId: number, accessor: Accessor, serializeBranch?: number, serializeMarker?: number, serializeStateful?: number, parentEndTag?: string | 0, singleNode?: 1): 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): ScopeInternals; export type SerializeReasonValue = undefined | number | Partial<Record<string, 0 | 1>>; export declare function _set_serialize_reason(reason: SerializeReasonValue): void; export declare function _scope_reason(): SerializeReasonValue; export declare function _serialize_if(condition: SerializeReasonValue, key: number): 1 | undefined; export declare function _serialize_guard(condition: SerializeReasonValue, key: number): 0 | 1; export declare function writeWaitReady(readyId: string, renderer: ServerRenderer, input: unknown): void; export declare function _await<T>(scopeId: number, accessor: Accessor, promise: Promise<T> | T, content: (value: T) => void, serializeMarker?: number): void; export declare function _try(scopeId: number, accessor: Accessor, content: () => void, input: { placeholder?: { content?(): void; }; catch?: { content?(err: unknown): void; }; }): void; declare const NOOP: () => void; type Mark = Mark.Value; export declare class State implements SerializeState { tagId: number; scopeId: number; reorderId: number; readyGate: number; hasGlobals: boolean; needsMainRuntime: boolean; hasMainRuntime: boolean; hasReadyRuntime: boolean; hasReorderRuntime: boolean; hasWrittenResume: boolean; walkOnNextFlush: boolean; trailerHTML: string; resumes: string; nonceAttr: string; serializer: Serializer; writeReorders: Chunk[] | null; scopes: Map<number, ScopeInternals>; flushScopes: boolean; writeScopes: Record<number, PartialScope>; readyIds: Set<string> | null; serializeReason: SerializeReasonValue; $global: $Global & { renderId: string; runtimeId: string; }; constructor($global: $Global & { renderId: string; runtimeId: string; }); get runtimePrefix(): string; get commentPrefix(): string; reorder(chunk: Chunk): void; writeReady(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 extends AbortController { onNext: typeof NOOP; count: number; state: State; parent?: Boundary; constructor(state: State, signal?: AbortSignal, parent?: Boundary); flush(): 0 | 1 | 2; startAsync(): void; endAsync(): void; } export declare class Chunk { html: string; scripts: string; effects: string; lastEffect: string; async: boolean; consumed: boolean; needsWalk: boolean; reorderId: string | null; deferredReady: Opt<Chunk>; placeholder: { body: Chunk; render: () => void; branchId: number; } | null; boundary: Boundary; next: Chunk | null; context: Record<string | symbol, unknown> | null; serializeState: SerializeState; constructor(boundary: Boundary, next: Chunk | null, context: Record<string | symbol, unknown> | null, serializeState: SerializeState); fork(boundary: Boundary, next: Chunk | null): Chunk; writeHTML(html: string): void; writeEffect(scopeId: number, registryId: string): void; writeScript(script: string): void; append(chunk: Chunk): void; takeDeferredReady(): Opt<Chunk>; deferOwnReady(): void; flushPlaceholder(): void; consume(): Chunk; render(content: () => void): Chunk; render<T>(content: (val: T) => void, val: T): Chunk; flushReadyScripts(reservations?: string[]): string; flushScript(): this; flushHTML(): string; } type QueueCallback = (ticked: true) => void; export declare function queueTick(cb: QueueCallback): void; export declare function offTick(cb: QueueCallback): void;