rvx
Version:
A signal based rendering library
100 lines (99 loc) • 3.35 kB
TypeScript
import { Context } from "../core/context.js";
export type TaskSource = (() => unknown) | Promise<unknown> | null | undefined;
export interface TasksOptions {
/**
* If true, focus is restored on the last active element when there are no more pending tasks in this instance.
*
* By default, this is inherited from the parent or true of there is none.
*/
restoreFocus?: boolean;
}
/**
* Represents a set of pending tasks in a specific context.
*
* This is meant to be used for preventing concurrent user interaction in a specific context.
*/
export declare class Tasks {
#private;
/**
* Create a new tasks instance with the specified parent.
*
* @param parent The parent to use. Default is no parent.
*/
constructor(parent?: Tasks, options?: TasksOptions);
/**
* The parent instance or undefined if there is none.
*/
get parent(): Tasks | undefined;
/**
* True if this instance has any pending tasks.
*
* @example
* ```tsx
* <div inert={() => tasks.selfPending}>...</div>
* ```
*/
get selfPending(): boolean;
/**
* True if this instance or any of it's parents has any pending tasks.
*
* @example
* ```tsx
* <button disabled={() => tasks.pending}>...</button>
* ```
*/
get pending(): boolean;
/**
* Pretend, that there is a pending task until the current lifecycle is disposed.
*/
setPending(): void;
/**
* Wait for an async function or a promise.
*
* @param source The async function or promise to wait for.
* + If this is a function, it runs {@link isolate isolated}.
*/
waitFor(source: TaskSource): void;
/**
* Create a new tasks instance using the current instance as parent.
*/
static fork(options?: TasksOptions): Tasks;
}
/**
* Context for the current {@link Tasks} instance.
*/
export declare const TASKS: Context<Tasks | undefined>;
/**
* Check if there are any pending tasks in the current tasks instance.
*
* This can be used in conjuction with {@link Tasks.prototype.waitFor `TASKS.current.waitFor`} to indicate if there are any pending tasks.
*
* This is meant to be used for preventing concurrent user interaction in a specific context.
*
* @example
* ```tsx
* <div inert={isSelfPending}>...</div>
* ```
*/
export declare function isSelfPending(): boolean;
/**
* Check if there are any {@link Tasks.prototype.pending pending} tasks in the current tasks instance or any of it's parents.
*
* This can be used in conjunction with {@link Tasks.prototype.waitFor `TASKS.current.waitFor`} to disable inputs and buttons while there are any pending tasks.
*
* This is meant to be used for preventing concurrent user interaction in a specific context.
*
* @example
* ```tsx
* <button disabled={isPending}>...</button>
* ```
*/
export declare function isPending(): boolean;
/**
* @deprecated Use `TASKS.current.setPending()` instead. This silently fails if there is no `Tasks` instance in the current context.
*/
export declare function setPending(): void;
/**
* @deprecated Use {@link Tasks.prototype.waitFor `TASKS.current.waitFor`} insead. This silently fails if there is no `Tasks` instance in the current context.
*/
export declare function waitFor(source: TaskSource): void;