/** * Copyright (c) 2025 Elara AI Pty Ltd * Dual-licensed under AGPL-3.0 and commercial license. See LICENSE for details. */ import type { EastType, option } from "@elaraai/east"; import type { OptimiseRowRef } from "./optimise-diff.js"; import { type OptimiseRowsReader } from "./optimise-rows.js"; import { type FuncCallBinding, type FuncCallDraft } from "./use-func-call.js"; /** How long the footer says `Stopped · nothing changed` after Stop (§8): 2.5s. */ export declare const OPTIMISE_STOPPED_MS = 2500; /** What a builder counted of a result it landed as one step (R7, R8). */ export interface OptimiseCounts { readonly added: number; readonly changed: number; readonly removed: number; } /** * A result the builder would not land (#1553): why, in its words — the * banner's line — such as an answer changing a duty of a published week. */ export interface OptimiseRefused { readonly refused: string; } /** * An answer that touches rows the builder does not hold (rule 8), each by its * kind and its key's text: the run reads them by their keys, and asks the * builder again as they land, the reads in its hand * ({@link OptimiseArgs.onLanded}'s `read`). The run stays out meanwhile. */ export interface OptimiseMissing { readonly missing: readonly OptimiseRowRef[]; } /** * An answer that cannot land yet for a reason of the builder's own — the * period it was asked of not read yet: the run stays out, and asks again as * the builder's {@link OptimiseArgs.settleKey} moves. */ export interface OptimiseWaiting { readonly waiting: true; } /** * A row the run has read by its key for an answer (rule 8): what a landing reads a row the page does not hold from. * * @param kind - The kind's slot, or the record's key * @param id - The row's key's text * @returns Its row; `none` for one the record does not hold; `undefined` for a row not asked for, or still being read */ export type OptimiseRowRead = (kind: string, id: string) => option | undefined; /** * The builder's sessions over the records it edits, as a run asks them * (east-workspace#1622) — and as its fit asks them (east-workspace#1624, * `use-fit.ts`): its unsaved edits as drafts, whether they stand on what the * page has read, and whether a conflict waits to be chosen. Read as the call * asks, never kept: each answer is of the sessions as they stand. */ export interface OptimiseSessions { /** * The pending Save of each session over a record the function lists in * `drafts`, as a draft (#1620): every unsaved edit that is complete, the * ones a narrowing hides and the ones off screen among them, whether or * not a check holds Save. A session with nothing unsaved hands over none. * * @param listed - The records the function lists in `drafts`, by name * @returns The drafts; or why none can be made now — a Save of a session's own still on its way, in its words */ drafts(listed: readonly string[]): readonly FuncCallDraft[] | OptimiseRefused; /** * Whether the sessions over the records named can hand their edits over * as they stand: none has a Save of its own on its way or being read * back, or is carrying its drafts over to a record that moved, or has yet * to be told its record did. What a fit's question waits on * (east-workspace#1624): its answer lands nothing, so it waits on no * session's taking a gesture. * * @param records - The records, by name */ standing(records: readonly string[]): boolean; /** * Whether the sessions over the records named stand on what the page has * read of them ({@link OptimiseSessions.standing}), and each takes a * gesture now: an answer lands as a gesture does. * * @param records - The records, by name */ settled(records: readonly string[]): boolean; /** * The conflicts the sessions over the records named hold for the person to * choose, in the refused Save's words — `2 of your changes conflict with * newer changes`; `undefined` with none. * * @param records - The records, by name */ conflicts(records: readonly string[]): string | undefined; } /** * What a run asks, and what the builder does with its answer. * * @typeParam Req - The request: the builder's `OptimiseRequest` * @typeParam Res - The result: its `OptimiseResult` */ export interface OptimiseArgs { /** The bound optimiser's name; `null` without one, and there is no eye. */ readonly name: string | null; /** What the function lists in `drafts` and reads, as its binding names them: one that lists none is called with its request alone. */ readonly binding: FuncCallBinding; /** The builder's sessions over the records it edits. */ readonly sessions: OptimiseSessions; /** * What moves as the builder's sessions do — their versions, by identity — and as whatever else a landing that waits * ({@link OptimiseWaiting}) waits on: it is tried again as this moves. The rows an answer touches that the page does * not hold are the run's to read ({@link OptimiseMissing}), and it tries again as they land. */ readonly settleKey: unknown; /** Several rows of a kind's record by their keys, in one tracked read (`ScheduleEditing.rowsByKey`): how the run reads the rows an answer touches that the page does not hold. */ readonly rowsByKey: OptimiseRowsReader; /** What the function takes. */ readonly requestType: EastType; /** What it answers. */ readonly resultType: EastType; /** The scope in view, by its key — the period: a change stops a run out (O4). */ readonly scope: string; /** The builder's key: the run counter is kept under it (D5). */ readonly storageKey: string; /** * The result landed, for the run it was asked of: the builder lands each * record's patch key by key on the rows it holds and records one step * (rules 7 and 10), answering what it counted; a refusal, recording * nothing, for an answer it would not take, naming why (#1553, rule 11); * {@link OptimiseMissing} where the answer touches rows it does not hold * (rule 8), which the run reads by their keys — `read` gives each as it * lands, and the builder is asked again as they do; {@link OptimiseWaiting} * for a wait of its own; `undefined` for an invalid response — a patch that * does not fit the rows, a row of another type — which is a failure (R10, * §12). */ readonly onLanded: (result: Res, request: Req, run: number, read: OptimiseRowRead) => OptimiseCounts | OptimiseRefused | OptimiseMissing | OptimiseWaiting | undefined; } /** What a run came to (R10): its number, the period it was asked of, and what changed — or why not, `refused` the builder's words where it refused the answer (#1553). */ export type OptimiseRunOutcome = { readonly kind: "done"; readonly run: number; readonly period: string; readonly counts: OptimiseCounts; } | { readonly kind: "none"; readonly run: number; readonly period: string; } | { readonly kind: "fail"; readonly run: number; readonly period: string; readonly message: string; readonly refused?: string; }; /** * A run of the app's optimiser (see the module docs). * * @typeParam Req - The request */ export interface OptimiseRun { /** Whether the app bound an optimiser: the eye shows only then. */ readonly offered: boolean; /** Whether a call is out: the eye runs, the footer counts, the builder holds its lock (R2). */ readonly running: boolean; /** The seconds since the call left, from 1, while a call is out. */ readonly seconds: number; /** The latest run's number: the call out, or the last that landed or stopped; 0 before any. */ readonly run: number; /** * Starts a run (R1): one call with the request the builder makes for the * run's number, over the period named. Nothing while one is out, or with * no optimiser. * * @param request - The request for the run's number * @param period - The period's label — `W11` — which the outcome names */ start(request: (run: number) => Req, period: string): void; /** Stops the run out (R3): the call cancelled, a late response ignored, nothing recorded. */ stop(): void; /** Whether the footer says `Stopped · nothing changed`: for 2.5s after Stop. */ readonly stopped: boolean; /** What the last run came to; `undefined` until one lands, after `dismiss`, and from the next `start`. */ readonly outcome: OptimiseRunOutcome | undefined; /** Moves at each run that landed with nothing to change: the eye blinks once each time. */ readonly blinks: number; /** Clears the outcome: the banner dismissed. */ dismiss(): void; } /** * A run of the app's optimiser (see the module docs). * * @typeParam Req - The request * @typeParam Res - The result * @param args - The function and what it lists, the scope, the builder's key and its sessions, and what the builder does with a result * @returns The run */ export declare function useOptimise(args: OptimiseArgs): OptimiseRun; //# sourceMappingURL=use-optimise.d.ts.map