Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@rbxts/trove

Package Overview
Dependencies
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@rbxts/trove

Class for tracking and cleaning up objects

  • 1.3.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

Trove

A Maid-like pattern for tracking and cleaning up objects.

API

Constructor

const trove = new Trove();

Trove.Trackable

The following type describes the objects that Trove can track.

type Trackable =
	| Instance
	| ConnectionLike
	| Promise<unknown>
	| thread
	| ((...args: unknown[]) => unknown)
	| { destroy: () => void }
	| { disconnect: () => void }
	| { Destroy: () => void }
	| { Disconnect: () => void };

Trove.add

add<T extends Trove.Trackable>(
	obj: T,
	cleanupMethod?: string,
): T

Add an item into the trove to be tracked.

Trove.clean / Trove.destroy

clean(): void
destroy(): void // alias to clean

Cleans up the trove. All tracked objects will be cleaned up and removed from the trove.

Trove.remove

remove<T extends Trove.Trackable>(
	obj: T,
): boolean

Removes and cleans up the given object from the trove. Will return true if the object was found and cleaned up.

Trove.connect

connect<T extends Callback = Callback>(
	signal: SignalLike<T>,
	fn: (...args: Parameters<T>) => void,
): ReturnType<typeof signal.Connect>

Connects to a signal. Shorthand for trove.add(signal.Connect(fn)). Returns the connection.

Trove.addPromise

addPromise<T>(promise: Promise<T>): Promise<T>

Adds the given promise to the trove. If the trove is cleaned up, the promise's cancel() method will be called. The returned promise is the same one passed to the function.

Trove.bindToRenderStep

bindToRenderStep(
	name: string,
	priority: number,
	renderFn: (dt: number) => void,
): void

Binds the renderFn to RenderStep. The function is unbound when the trove is cleaned up.

Trove.clone

clone<T extends Instance>(
	instance: T,
): T

Clones an instance and adds it to the clone. Shorthand for trove.add(instance:Clone()).

Trove.extend

extend(): Trove

Constructs a new trove and adds it to the current trove. Shorthand for trove:Add(new Trove()).

Trove.attachToInstance

attachToInstance(
	instance: Instance,
): RBXScriptConnection

Attaches the trove to the given instance. Once the instance is destroyed (using the instance.Destroying event internally), the trove will destroy itself.

Example

class MyClass {
	private trove = new Trove();

	constructor() {

		// Add folder:
		const folder = this.trove.add(new Instance("Folder"));

		// Add function:
		this.trove.add(() => {
			print("Cleanup");
		});

		// Add event:
		this.trove.connect(Workspace.ChildAdded, (child) => {
			print(`${child} added to Workspace`);
		});

		// Add RenderStep binding:
		this.trove.bindToRenderStep("Hello", Enum.RenderPriority.First.Value, (dt) => {
			// Do something
		});

		// Add promise:
		this.trove.addPromise(new Promise(resolve, reject, onCancel) => {
			// Trove will cancel a promise if it is still in-flight when the trove is cleaned up
			onCancel(() => print("Cancelled!"));
			task.wait(5);
			resolve();
		});

		// Add another trove. This creates a new trove and puts the new one into the current one.
		const nestedTrove = this.trove.extend();

		// Add another folder (note: doesn't have to be an instance already in trove):
		const anotherFolder = this.trove.clone(folder);

		// Remove the above folder (which also destroys it):
		this.trove.remove(anotherFolder);

	}

	public destroy() {
		this.trove.destroy();
	}
}

Keywords

FAQs

Package last updated on 24 Jul 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc