UNPKG

@rbxts/trove

Version:

Class for tracking and cleaning up objects

93 lines (92 loc) 3.15 kB
/// <reference types="@rbxts/compiler-types" /> /// <reference types="@rbxts/types" /> interface ConnectionLike { Disconnect(this: ConnectionLike): void; } interface SignalLike<T extends Callback = Callback> { Connect(this: SignalLike, callback: T): RBXScriptConnection; } export declare namespace Trove { /** * A type representing an object that a trove can track. */ type Trackable = Instance | ConnectionLike | Promise<unknown> | thread | ((...args: unknown[]) => unknown) | { destroy: () => void; } | { disconnect: () => void; } | { Destroy: () => void; } | { Disconnect: () => void; }; } /** * A Trove helps track and clean up objects. */ export declare class Trove { private objects; private cleaning; /** * Add an object to the trove. * @param obj Object to add * @param cleanupMethod Optional name of method attached to the object to invoke on cleanup * @returns Same object added */ add<T extends Trove.Trackable>(obj: T, cleanupMethod?: string): T; /** * Adds a promise to the Trove. The trove will call the `cancel()` method of the promise * if the trove is cleaned up. * @param promise The Promise to add * @returns The same Promise */ addPromise<T>(promise: Promise<T>): Promise<T>; /** * Connects to a signal, which adds the connection to the trove. * @param signal The signal to connect * @param handler The function connected to the signal * @returns Connection */ connect<T extends Callback>(signal: SignalLike<T>, handler: (...args: Parameters<T>) => void): ReturnType<typeof signal.Connect>; /** * Binds the `renderFn` to RenderStep. The function is unbound when the trove is cleaned up. * @param name Name of the RenderStep binding * @param priority Priority (see `Enum.RenderPriority`) * @param renderFn Function to bind */ bindToRenderStep(name: string, priority: number, renderFn: (dt: number) => void): void; /** * Removes an object from the trove and cleans up the object. * @param obj Object to remove * @returns `true` if object was found and removed */ remove<T extends Trove.Trackable>(obj: T): boolean; /** * Clones the given instance and adds the clone to the trove. * @param instance Instance to clone * @returns New cloned instance */ clone<T extends Instance>(instance: T): T; /** * Creates a new trove and adds it to this trove * @returns The created trove */ extend(): Trove; /** * Attaches the trove to an instance. If the instance is destroyed, * the trove will clean itself up. * @param instance * @returns */ attachToInstance(instance: Instance): RBXScriptConnection; /** * Cleans up all tracked objects in the trove. */ clean(): void; /** * Alias for `clean`. */ destroy(): void; private findAndRemoveFromObjs; private cleanupObj; } export {};