rvx
Version:
A signal based rendering library
76 lines (75 loc) • 3.14 kB
TypeScript
/**
* A function that is called to dispose something.
*/
export type TeardownHook = () => void;
/**
* A function that is called after something has been synchronously created. E.g. after rendering a tree of elements.
*/
export type CreatedHook = () => void;
export type LeakHook = (hook: TeardownHook) => void;
/**
* Register a hook to be called when any teardown hooks are registered outside of any capture calls.
*
* Errors thrown from the leak hook will be thrown by the **teardown** calls.
*/
export declare function onLeak(hook: LeakHook): void;
/**
* Run a function while capturing teardown hooks.
*
* + If an error is thrown by the specified function, teardown hooks are called in reverse registration order and the error is re-thrown.
* + If an error is thrown by a teardown hook, remaining ones are not called and the error is re-thrown.
*
* @param fn The function to run.
* @returns A function to run all captured teardown hooks in reverse registration order.
*/
export declare function capture(fn: () => void): TeardownHook;
/**
* Run a function while capturing teardown hooks.
*
* + When disposed before the specified function finishes, teardown hooks are called in reverse registration order immediately after the function finishes.
* + If an error is thrown by the specified function, teardown hooks are called in reverse registration order and the error is re-thrown.
* + If an error is thrown by a teardown hook, remaining ones are not called and the error is re-thrown.
*
* @param fn The function to run.
* @returns The function's return value.
*/
export declare function captureSelf<T>(fn: (dispose: TeardownHook) => T): T;
/**
* Run a function without capturing any teardown hooks.
*
* This is the opposite of {@link capture}.
*
* @param fn The function to run.
* @returns The function's return value.
*/
export declare function uncapture<T>(fn: () => T): T;
/**
* Run a function and explicitly un-support teardown hooks.
*
* Teardown hooks are still supported when using {@link capture}, {@link captureSelf} or {@link uncapture} inside of the function.
*
* This should be used in places where lifecycle side are never expected.
*
* @param fn The function to run.
* @returns The function's return value.
*/
export declare function nocapture<T>(fn: () => T): T;
/**
* Run a function and immediately call teardown hooks if it throws an error.
*
* + If an error is thrown, teardown hooks are immediately called in reverse registration order and the error is re-thrown.
* + If no error is thrown, teardown hooks are registered in the outer context.
*
* @param fn The function to run.
* @returns The function's return value.
*/
export declare function teardownOnError<T>(fn: () => T): T;
/**
* Register a teardown hook to be called when the current lifecycle is disposed.
*
* This has no effect if teardown hooks are not captured in the current context.
*
* @param hook The hook to register. This may be called multiple times.
* @throws An error if teardown hooks are {@link nocapture explicitly un-supported}.
*/
export declare function teardown(hook: TeardownHook): void;