UNPKG

@threlte/core

Version:

A 3D framework for the web, built on top of Svelte and Three.js

110 lines (109 loc) 3.14 kB
import { type Readable } from 'svelte/store'; import { type Key, type Stage, type Task } from '../frame-scheduling/index.js'; export interface ThrelteUseTask { task: Task; /** * @deprecated pass the `running` option to `useTask` instead. * * To stop the task, set the options.running state variable to false * * ```ts * let running = $state(true) * * useTask((delta) => { * // do something * }, { * running: () => running * }) * * running = false * ``` */ stop: () => void; /** * @deprecated pass the `running` option to `useTask` instead. * * To start the task, set the options.running state variable to true * * ```ts * let running = $state(false) * * useTask((delta) => { * // do something * }, { * running: () => running * }) * * running = true * ``` */ start: () => void; started: Readable<boolean>; } export interface ThrelteUseTaskOptions { /** * @deprecated pass the `running` option to `useTask` instead. * * ```ts * let running = $state(true) * * useTask((delta) => { * // do something * }, { * running: () => running * }) * ``` * * If false, the task will not be started automatically and must be started * by invoking the `start` function. Defaults to true. */ autoStart?: boolean; /** * If false, the task handler will not automatically invalidate the task. * This is useful if you want to manually invalidate the task. Defaults to * true. */ autoInvalidate?: boolean; /** * The task will be added to the stage after the specified task. */ after?: (Key | Task) | (Key | Task)[]; /** * The task will be added to the stage before the specified task. */ before?: (Key | Task) | (Key | Task)[]; /** * The stage to add the task to. Defaults to the main stage. If a task object * is provided to `after` or `before`, the stage of that task will be used. */ stage?: Key | Stage; /** * Provide a function that returns a rune to toggle running this task. * * ```ts * let running = $state(true) * * useTask((delta) => { * // do something * }, { * running: () => running * }) * ``` */ running?: () => boolean; } /** * Adds a handler to threltes unified render loop. * * `start` and `stop` functions are returned and the options allow setting the * handler to not start automatically. * * Use the options `after` and `before` to control the order of execution. Add * the task to a specific stage with the option `stage`. * * @param {(delta: number) => void} fn callback function * @param {ThrelteUseTaskOptions} options options * @returns {ThrelteUseTask} */ export declare function useTask(fn: (delta: number) => void, options?: ThrelteUseTaskOptions): ThrelteUseTask; export declare function useTask(key: Key, fn: (delta: number) => void, options?: ThrelteUseTaskOptions): ThrelteUseTask;