import { WatchCompatibleQuery, WatchedQuery, WatchedQueryListener, WatchedQueryOptions } from '../WatchedQuery.js'; import { AbstractQueryProcessor, AbstractQueryProcessorOptions, LinkQueryOptions } from './AbstractQueryProcessor.js'; /** * Represents an updated row in a differential watched query. * It contains both the current and previous state of the row. */ export interface WatchedQueryRowDifferential { readonly current: RowType; readonly previous: RowType; } /** * Represents the result of a watched query that has been diffed. * {@link DifferentialWatchedQueryState#diff} is of the {@link WatchedQueryDifferential} form. */ export interface WatchedQueryDifferential { readonly added: ReadonlyArray>; /** * The entire current result set. * Array item object references are preserved between updates if the item is unchanged. * * e.g. In the query * ```sql * SELECT name, make FROM assets ORDER BY make ASC; * ``` * * If a previous result set contains an item (A) `{name: 'pc', make: 'Cool PC'}` and * an update has been made which adds another item (B) to the result set (the item A is unchanged) - then * the updated result set will be contain the same object reference, to item A, as the previous result set. * This is regardless of the item A's position in the updated result set. */ readonly all: ReadonlyArray>; readonly removed: ReadonlyArray>; readonly updated: ReadonlyArray>>; readonly unchanged: ReadonlyArray>; } /** * Row comparator for differentially watched queries which keys and compares items in the result set. */ export interface DifferentialWatchedQueryComparator { /** * Generates a unique key for the item. */ keyBy: (item: RowType) => string; /** * Generates a token for comparing items with matching keys. */ compareBy: (item: RowType) => string; } /** * Options for building a differential watched query with the {@link Query} builder. */ export interface DifferentialWatchedQueryOptions extends WatchedQueryOptions { /** * Initial result data which is presented while the initial loading is executing. */ placeholderData?: RowType[]; /** * Row comparator used to identify and compare rows in the result set. * If not provided, the default comparator will be used which keys items by their `id` property if available, * otherwise it uses JSON stringification of the entire item for keying and comparison. * @defaultValue {@link DEFAULT_ROW_COMPARATOR} */ rowComparator?: DifferentialWatchedQueryComparator; } /** * Settings for differential incremental watched queries using. */ export interface DifferentialWatchedQuerySettings extends DifferentialWatchedQueryOptions { /** * The query here must return an array of items that can be differentiated. */ query: WatchCompatibleQuery; } export interface DifferentialWatchedQueryListener extends WatchedQueryListener>> { onDiff?: (diff: WatchedQueryDifferential) => void | Promise; } export type DifferentialWatchedQuery = WatchedQuery>, DifferentialWatchedQuerySettings, DifferentialWatchedQueryListener>; /** * @internal */ export interface DifferentialQueryProcessorOptions extends AbstractQueryProcessorOptions> { rowComparator?: DifferentialWatchedQueryComparator; } type DataHashMap = Map; /** * An empty differential result set. * This is used as the initial state for differential incrementally watched queries. */ export declare const EMPTY_DIFFERENTIAL: { added: never[]; all: never[]; removed: never[]; updated: never[]; unchanged: never[]; }; /** * Default implementation of the {@link DifferentialWatchedQueryComparator} for watched queries. * It keys items by their `id` property if available, alternatively it uses JSON stringification * of the entire item for the key and comparison. */ export declare const DEFAULT_ROW_COMPARATOR: DifferentialWatchedQueryComparator; /** * Uses the PowerSync onChange event to trigger watched queries. * Results are emitted on every change of the relevant tables. * @internal */ export declare class DifferentialQueryProcessor extends AbstractQueryProcessor>, DifferentialWatchedQuerySettings> implements DifferentialWatchedQuery { protected options: DifferentialQueryProcessorOptions; protected comparator: DifferentialWatchedQueryComparator; constructor(options: DifferentialQueryProcessorOptions); protected differentiate(current: RowType[], previousMap: DataHashMap): { diff: WatchedQueryDifferential; map: DataHashMap; hasChanged: boolean; }; protected linkQuery(options: LinkQueryOptions>): Promise; } export {};