API
The official API for Tome giving basic examples for constructors, embedded systems and methods.
Constructors
Tome uses a single constructor to create a Tome object
Tome.new
Arguments
metaprops: Metaprops?— The Metaprops object to use when instantiating the Tome.
Returns
tome: Tome— Returns a new Tome object.
Creating a Tome is very easy.
Functions
Functions in Tome can be used directly from the module without instantiation.
Tome.Is
Arguments
object: any— The object to check.
Returns
isATome: boolean— Whether the object is a Tome.
Returns whether the provided object is a Tome object. This function will check against the direct metatable, and Tome:BindRenderStepped method (to track earlier versions)
Tome.schedule
Arguments
object: any— The object to schedule.lifeTime: number— The life time to give the object.destroyMethod: DestroyMethod?— The DestroyMethod to use when destroying the object.
Returns
object: object— The same object that was passed in.
Adds the provided object into the Scheduler. The object will be destroyed after the provided life time has elapsed.
Tome.unschedule
Arguments
object: any— The object to unschedule.
Returns
object: object— The same object that was passed in.
Removes the provided object from the Scheduler. The object will not be destroyed.
Tome.group
Arguments
Tuple: ...any— The tuple set of objects to group.
Returns
objects: Group<any>— The same objects that were passed in, in array form.
Groups together the provided tuple set of objects and puts them inside an array attached to a Tome group metatable to use as a symbol.
Grouping is currently only used for Tome.schedule and can be used to destroy objects as a group, rather than one by one. If you find yourself calling Tome.schedule a lot, you may benefit from this.
You can also call the group like a function, and it will destroy the objects as well.
Variables
Get access to Tome internals to speed up certain methods manually.
Tome.FunctionType
Providing this as a DestroyMethod for methods like Tome:Add can significantly speed up adding functions into the Tome.
Tome.ThreadType
Providing this as a DestroyMethod for methods like Tome:Add can significantly speed up adding threads into the Tome.
Tome.TweenType
Providing this as a DestroyMethod for methods like Tome:Add can significantly speed up adding tweens into the Tome.
This DestroyMethod will first call Tween:Cancel and then Tween:Destroy.
Tome.Guess
This is usually only used in pair with Tome:AddFromDictionary. This is used as a value in the dictionary to let Tome know that it should calculate the DestroyMethod.
Tome.Scheduler
Returns the Scheduler that Tome is using. This can be (but not recommended) used to modify certain aspects of the Scheduler.
The majority of these functions should not be called outside debugging purposes. Changes to the Scheduler will affect the entire environment, not only the scope that made the changes.
At some point an API may be created to allow for multiple Schedulers to be created and used within a single scope.
Scheduler.startScheduler
This starts up the Scheduler (if it isn't already running) this is usually only called within Tome.
Scheduler.stopScheduler
This suspends the Scheduler in place. Meaning objects that are currently inside will not get destroyed, even if their life time is exceeded.
Scheduler.stepScheduler
Steps the Scheduler forward. This will check the first object within the Scheduler, and if it's ready to be destroyed, then it will be.
Scheduler.isScheduleEmpty
A more practical function that returns whether the Scheduler is empty; whether it has no objects.
Scheduler.isSchedulerRunning
Returns whether the Scheduler is currently running.
Scheduler.DefaultSchedulerSignalName
This determines what RunService signal to use for the Scheduler. The only options are:
Every time the signal fires, the Scheduler will step.
If this variable is changed, the Scheduler must be reconciled with:
Tome.VERSION
Returns the version of the Tome being used.
Metamethods
Since Tome is a metatable-based library, it comes with some useful metamethods that help with debugging and general use. Some of these can also be found in other libraries, so woo! Extra transition support.
Tome.__call
Arguments
Tuple: ...any— The arguments to pass into Tome:Destroy.
Destroys the Tome just like Tome:Destroy. Will also take in a tuple of arguments to pass into destroy callbacks for Tome:OnDestroy.
Tome.__add
Arguments
object: any— The object to add into the Tome.
Returns
object: object— Returns the same object passed in.
Adds in an object just like Tome:Add and returns it.
Tome.__sub
Arguments
object: any— The object to remove from the Tome.
Returns
object: object— Returns the same object passed in.
Removes an object just like Tome:Remove and returns it.
Tome.__len
Returns
objectCount: number— The amount of objects.
Returns the amount of objects inside the Tome.
Tome.__iter
Returns
iterator: () -> (any, DestroyMethod)— A custom iterator.
Returns a custom iterator to use in for x in y do that will iterate through all the objects within the Tome.
Methods
Tome offers a variety of methods to use on a Tome object
Tome:Add
Arguments
object: any— The object to add into the Tome. Usually anInstance,RBXScriptConnectionor a class.destroyMethod: DestroyMethod?— Destroy method to use instead of Tome finding the destroy method.
Returns
object: any— Returns the same object provided in argument #1.
The primary method of adding an object into the Tome.
Optimization & Finer control
Now if you need finer control over how to destroy the object, you can use the second argument. When using the second argument, Tome will skip over finding the destroy method and use what's provided instead.
Internally Tome will conclude "Destroy" as the destroy method for Instances. Tome is always unaware of the type of object you provide, so it has to come up with the destroy method at run time. Giving Tome a destroy method improves write speed signficantly in a lot of cases:
Tome:AddTuple
Arguments
Tuple: ...any— Any number of tuple objects to add into the Tome.
Returns
Tuple: ...any— Returns the same tuple (and retaining order) provided.
For adding any tuple amount of objects into the Tome. The objects will be returned in the same order they were provided in.
Tome:AddFromArray
Arguments
array: {any}— The array of objects to add into the Tome.
Returns
array: {any}— Returns the same array that was provided in argument #1.
For adding an array of objects into the Tome. Useful in cases where it's preferable to call this instead of iterating through the array manually.
Tome:AddFromDictionary
Arguments
dictionary: {[any]: DestroyMethod | Tome.Guess}— The dictionary of objects to add into the Tome.
Returns
dictionary: {[any]: DestroyMethod | Tome.Guess}— Returns the same dictionary that was provided in argument #1.
In some cases you may want to optimize adding objects into the Tome. One way you can do this is by having a dictionary on a:
basis.In some scenarios you may not know what destroy method to put as the value in the pair, this is where Tome.Guess comes in. Using this as the value will let Tome know to find the destroy method for the object.
Imagine having to add hundreds of newly created objects into the Tome. Adding them one by one with Tome:Add will work fine, however adding them as a dictionary will improve performance significantly:
Tome:AddPromise
Arguments
promise: Promise— The Promise to add into the Tome.
Returns
promise: Promise— Returns the same Promise that was provided in argument #1.
Adds in a standard Promise object into the Tome. The Promise must have :cancel, :finally and :getStatus as methods. Moreover, :getStatus must return "started" as a status when provided, otherwise an error will be thrown.
Tome:AddPage
Arguments
name: string?— Providing a name will open up the ability to use other methods such asTome:GetPage. Internally helps with debugging as well.metaprops: Metaprops?— Providing Metaprops will apply them to the new Page when constructing.
Returns
page: Tome— Returns a new Page (Tome)
Instantiates a new Page (alias for Tome when referring to a Tome within a Tome) Unlike a regular object, a Page gets added to a seperate table internally, which allows for faster querying, and gives you control over how Pages are handled.
When the Tome that created the Page is destroyed, the Page will also destroy; cleaning up all the objects inside of it, and any sub-Pages.
Pages are not released on destruction
When a Page is added into the Tome, Tome doesn't see it as just another regular object, Tome will keep the Page alive until manually removed with Tome:RipPage or Tome:RipPages. Not freeing the Page will not result in a memory leak, at some point once the Tome is done being used, luau's garbage collector will automatically free it.
If you absolutely need a Page to be removed on destruction, you can manually pass it into the Tome as though it were an object:
This is not recommended, as you lose control over time in more complex systems. One-off cases are usually safe.Tome:Attach
Arguments
object: Tome | Instance | RBXScriptSignal | {Connect: () -> ()}— The object that the Tome will attach to.
Returns
attachment: Attachment— Returns an attachment which can be cleaned up to unattach the attachment.
Attaches the Tome to one of the few object types. Once attached, when either triggers are fired, the Tome will destroy itself.
Attachments are not cleaned up when the Tome is destroyed. For example, if a Signal is provided, and the Signal gets fired n times, the Tome will also be destroyed n times until the Signal is either destroyed, or the attachment is unattached.
It's also important to note that if an Instance that doesn't have a parent is attached, no attachment will be made and nothing is returned.
In this example, an attachment is stored in the variable attachmentCleanup. This attachment can be "cleaned up" at any time, and once cleaned, will prevent the object from invoking Tome:Destroy again.
Tome:AttachTuple
Arguments
Tuple: ...(Tome | Instance | RBXScriptSignal | {Connect: () -> ()})— The tuple of objects that the Tome will attach to.
Returns
cleanUp: () -> ()— The clean up function.attachments: {Attachment}— The array of attachments (shouldn't be mutated)
Works just like Tome:Attach however any amount of objects can be provided, and once all the objects are successfully connected, a single clean up function is returned, which when called, will free all the attachments at once.
It's important to note that this method internally calls Tome:Attach. The same warning(s) apply from Tome:Attach, to here.
Tome:BindRenderStepped
Arguments
name: string— The name of the render step binding.renderPriority: number— The name of the render step binding.listener: (deltaTime: number) -> ()— The name of the render step binding.
Returns
unbind: () -> ()— The unbind function (manual clean up)
Calls the RunService:BindToRenderStep method. Tome adds a function inside of itself to unbind the render binding once the Tome is destroyed.
Optionally you can call the unbind() function returned to manually clean up the binding.
In this example, the binding will output the delta time each frame until 2 seconds have elapsed.
Tome:CanDestroy
Returns
canDestroy: boolean— Whether the Tome can be destroyed at this moment in time.
Returns whether the Tome can be destroyed in this current moment of time. You only need to call this before using methods that throw an error when attempting to mutate the Tome during its destroy life cycle.
In this example, the Tome is running with SpawnDestroy = false, hence the task.defer. Adding a yielding function will temporarily halt the destroy thread, keeping the Tome in a destroying state.
Tome:Clone
Arguments
object: any— The object to cloneextendFunctionName: string?— The method to call on the object to clone it.
Returns
clonedObject: any— The cloned object.
Clones the provided object using the object:Clone() as the clone method (unless overridden by the 2nd argument)
Tome:Connect
Arguments
Signal: Signal— The Signal to connect to.listener: (...any) -> ()— The listener function to connect with.
Returns
Connection: {Disconnect: () -> ()} | RBXScriptConnection— The Connection object.
Connects to a Signal, and adding it into the Tome. Can be a custom-made Signal or an RBXScriptSignal.
Tome:Once
Arguments
Signal: Signal— The Signal to connect to.listener: (...any) -> ()— The listener function to connect with.
Returns
Connection: {Disconnect: () -> ()} | RBXScriptConnection— The Connection object.
Works exactly the same as Tome:Connect, however only connecting to a Signal once, and adding it into the Tome. Can be a custom-made Signal or an RBXScriptSignal.
Tome:Construct
Arguments
class: {new: () -> ()} | () -> ()— The class to construct.Tuple: ...any— The arguments to pass into the constructor.
Returns
Class: any— The constructed class.
Constructs a class containing a Class.new() constructor function. Optionally you can pass in the constructor function if your class uses another name for construction.
Any amount of arguments can be passed in after the class/constructor, which will be passed into it. After construction, the class object is added into the Tome.
Tome:Delay
Arguments
duration: number— The amount of time to delay.listener: (...any) -> ...any— The listener function to call after the delay.Tuple: ...any— The params to pass into the listener.
Returns
thread: thread— The delayed thread.
Calls task.delay, adds the delayed thread into the Tome, and returns it.
Tome:extend
Duplicate of Tome:AddPage
Tome:Extend
Duplicate of Tome:AddPage
Tome:FastAdd
Arguments
object: any— The object to add into the Tome.destroyMethod: DestroyMethod?— Destroy method to use instead of Tome finding the destroy method.
Returns
object: object— The same object passed in.
The same as Tome:Add but executes without the majority of features in Tome. This method will skip over sanity checks like whether the Tome is currently being destroyed, tagging, and recursion mistakes with nested Tomes.
If you prioritize speed over features, then using this will benefit you.
Tome:Spawn
Arguments
listener: (...any) -> ...any— The listener function to call when spawning.Tuple: ...any— The params to pass into the listener.
Returns
thread: thread— The spawned thread.
Calls task.spawn, adds the spawned thread into the Tome, and returns it.
Tome:Defer
Arguments
listener: (...any) -> ...any— The listener function to call after the defer.Tuple: ...any— The params to pass into the listener.
Returns
thread: thread— The deferred thread.
Calls task.defer, adds the deferred thread into the Tome, and returns it.
Tome:DelayDestroy
Arguments
duration: number— The amount of time (in seconds) to delay the destroy.
Returns
thread: thread— The thread responsible for destroying the Tome.
Calls task.delay, with the 1st argument being 'duration'. Once that time has elapsed successfully, the thread will call Tome:Destroy internally.
Tome:Destroy
Arguments
Tuple: ...any— Any amount of params to pass into theTome:OnDestroycallbacks.
Internally calls Tome:DestroyAllObjects and Tome:DestroyAllPages. During both states of destruction, the Tome will enter a destroying state, where most attempts at mutation e.g. Tome:Add will throw an error. This state is usually very short lived (<0.00001s)
Tome:DestroyAllObjects
Arguments
__ignoreDestroyingProperty: boolean?— Internal argument to skip over checking whether the Tome is currently destroying.
Destroys all the objects inside the Tome. Doesn't destroy Pages.
Tome:DestroyAllPages
Arguments
__ignoreDestroyingProperty: boolean?— Internal argument to skip over checking whether the Tome is currently destroying.
Destroys all the Pages inside the Tome. Doesn't destroy objects.
Tome:DestroyObject
Arguments
object: any— The object to destroy.
Destroys the given object, only if it exists inside the Tome. Destroying it will remove it from the Tome as well.
Tome:DestroyTuple
Arguments
Tuple: ...any— Any amount of objects to destroy.
Destroys the given object(s), only if they exist inside the Tome. Destroying them will remove them from the Tome as well.
Tome:DestroyObjectsWithTag
Arguments
tag: string— The tag to use when querying Tome.
Destroys all objects with the given tag. This only works for Instances and the objects must be inside the Tome to be destroyed.
Tome:DestroyObjectsOfType
Arguments
typeName: string— The type of object(s) to destroy.
Destroys all objects that match the provided type. During querying Tome will search for the following in these types of objects
Tome:Contains
Arguments
object: any— The object to check.
Returns
exists: boolean— Whether the object exists.
Returns whether the provided object exists inside the Tome
Tome:Has
Arguments
object: any— The object to check.
Returns
exists: boolean— Whether the object exists.
The exact same as Tome:Contains.
Tome:FromExisting
Arguments
instance: Instance— The object to Instance against.destroyMethod: DestroyMethod?— An optional override for the destroy method to use.
Returns
object: Instance— The instantiated instance created from the 1st argument.
Creates an Instance from an existing one. See Instance.fromExisting for more information.
If the Instance is created successfully, the Instance will be added into the Tome.
Tome:GetObjects
Returns
objects: {[any]: DestroyMethod}— The objects.
Returns the internal dictionary Tome uses to store objects. This table shouldn't be mutated freely, but if you know what you're doing, feel free.
Tome:GetObjectsWithTag
Arguments
tag: string— The tag to use.
Returns
objects: {any}— The tagged objects.
Returns objects that have the provided tag. Only works for Instances
Tome:GetObjectsOfType
Arguments
objectType: string— The object type to query with.
Returns
objects: {any}— The objects that match the type provided.
Returns objects that have the provided type.
Tome:GetPage
Arguments
name: string— The name of the Page to get.
Returns
page: Tome?— The Page that was fetched.
Returns a Page within the Tome from a given name. If the Page with the name doesn't exist, then nil is returned.
Tome:GetParent
Returns
parent: Tome?— The parent of the Tome.
Returns the parent Tome of the Tome that had this method invoked from. A Tome that has a parent is also referred to as a 'Page'.
If a parent doesn't exist, nil is returned.
Tome:GetTag
Returns
tag: string?— The tag Tome uses for Tagging.
Returns the tag the Tome uses for tracking Instances when Tagging is enabled. If Tome:SetTag was not called to alter the tag, then a standard GUID is usually returned.
Tome:GivePage
Arguments
pageName: string— The name of the Page to give.newParent: Tome— The new parent of the Page.
Gives a Page from the Tome that was invoked, to another Tome. If the Page doesn't exist, nothing will happen.
Tome:HookRunServiceSignal
Arguments
signalName: RunServiceSignalName— The name of the Signal to hook to.listener: (deltaTime: number) -> () | (time: number, deltaTime: number) -> ()— The listener function to call every step.
Returns
connection: RBXScriptConnection— The connection between the Signal and listener.
Hooks to a RunService Signal, which can be any one of the following:
When the RBXScriptConnection is created, it will be added to the Tome; disconnecting it once the Tome is destroyed.
Tome:Instance
Arguments
instanceName: string— The name of the Instance to create.properties: {[string]: any}— The dictionary of properties to apply to the Instance.destroyMethod: DestroyMethod?— Optional DestroyMethod to use instead of `#!luau "Destroy".
Returns
instance: Instance— The created Instance.
Creates a new Instance from a given name. The Instance will automatically be added into the Tome.
Optionally you can provide properties to apply to the Instance before returning it.
Properties are applied within a safe call. This means if you incorrectly apply a property, the error will be supressed. To avoid this, ensure you create the Tome with the metaprop Warnings set to true.
Optionally you can also provide a custom destroy method.
DEV-TODO
Currently there's no guarantee that the property properties.Parent will be set after all the other properties. This is important because setting properties after parenting is slower than setting them during creation.
This will be changed in the near future.
Tome:IsDestroying
Returns
destroying: boolean— Whether the Tome is currently destroying.
Returns whether the Tome is currently being destroyed.
Tome:Move
Arguments
object: any— The object to move from the Tome.tome: Tome— The Tome to move the object into.
Returns
object: object— The object object passed in.
Moves the provided object into another Tome. Removing it from the Tome that this method was called from as well.
During the move, the object will keep the exact same destroy method inside the new Tome.
Tome:Parent
Arguments
tome: Tome— The new parent to live under.
Returns
tome: Tome— Returns the Tome this method was called from.
Parents the Tome to another Tome. If the Tome already has a parent, then it will unparent from it first.
If the Tome doesn't have a name, the Tome will not be parented. To name a Tome you can do the following:
All Tomes that are created with Tome:AddPage, Tome:extend, Tome:Extend will come with a standard GUID name.
Tome:Remove
Arguments
object: any— The object to remove from the Tome.
Returns
object: object— The same object that was passed in.
Safely removes the provided object from the Tome, if it exists inside it. Removing an object does not destroy it.
If the Tome has Tagging enabled and the object is an Instance, the object will have the Tome's tag removed as well.
Tome:RemoveTuple
Arguments
Tuple: ...any— The objects to remove from the Tome.
Returns
Tuple: ...object— The same objects that were passed in.
The same as Tome:Remove with the one change of being able to provide a tuple of objects, instead of just one.
Tome:RemoveFromArray
Arguments
arrayOfObjects: {any}— The objects to remove from the Tome.
Returns
arrayOfObjects: {any}— The same objects that were passed in.
The same as Tome:Remove with the one change of being able to provide an array of objects, instead of just one.
Tome:RemoveObjectsWithTag
Arguments
tag: string— The tag to use for getting the objects to remove.
Removes all objects from the Tome that have the specified tag. This only works for Instances.
Tome:RemoveObjectsOfType
Arguments
type: string— The type to use for getting the objects to remove.
Removes all objects from the Tome that are of the specified type.
Tome:Rename
Arguments
name: string— The new name for the Tome.
Renames the Tome internally. This is usually used for debugging. But can be used for other purposes too.
Tome:GetName
Returns
name: string?— The name of the Tome.
Returns the name of the Tome. If the Tome doesn't have a name, nil is returned.
Tome:RipPage
Arguments
nameOrPage: string | Tome— The name of a Page/Tome or the Page/Tome itself.
Returns
tome: Tome— The Tome itself (for chaining purposes)
Removes the Page from the Tome permanently. Destroying itself before being removed.
If a Page doesn't exist, an error will be thrown. To avoid this, you can call Tome:GetPage before attempting to remove a Page.
Tome:RipPages
Destroys all Pages inside the Tome, and removes them.
Tome:SetTag
Arguments
tag: string— The new tag to apply.
Returns
tome: Tome— The Tome itself (for chaining purposes)
Sets a new tag for the Tome. This will remove the previous tag, which means all other objects that were tagged, will have their tag replaced with the new one. This will have to add and remove the tags for instances, which can be slow when used frequently, in mass.
This is mainly used for debugging, or very case-specific situations with Tagging.
Tome:Signal
Returns
signal: Signal— The created Signal
Creates a standard Signal implementation, and adds it to the Tome. This is from earlier versions of Tome.
Tome:UnbindRenderStepped
Arguments
name: string— The name of the binding.
Manually unbinds the render step RunService binding.
Tome:Tween
Arguments
instance: Instance— The instance to tween.tweenInfo: TweenInfo— The tween info to use to mutate the instance.propertyTable: {[string]: any}— The properties to tween to.metadata: Metadata?— The metadata to mutate how to tween works.
Returns
tween: Tween— The created tween.
Creates a Tween and adds it into the Tome. This method comes with some extra benefits, such as metadata:
Metadata changes how the tween works. See Tween Metadata for more information.
In this example, once the tween completes, the Tome gets destroyed. This is useful for creating timed events without creating new threads.
Tome:Table
Arguments
table: {[any]: any}?— The table to use instead of creating a new one.
Returns
table: table | {[any]: any}— The same table passed in, or the created table.
Adds (or creates) a table into the Tome. The table will have its destroy method as table.clear. Once the Tome gets destroyed, the table will clear itself.
This is useful in semi-round systems where you may track players within a sub-round, and need to clear them after the sub-round ends.
Tome:OnDestroy
Arguments
callback: (...any) -> ()— The callback to add once the Tome destroys.onDestroyParams: OnDestroyParams— The params to mutate how the callback works.
Returns
cleanUp: () -> ()— A clean up function to remove the callback.
Adds a callback function into the Tome that listens for when the Tome gets destroyed. The callback will recieve the same arguments that were passed in Tome:Destroy. This can act as a sort of Signal.
Optionally you can provide OnDestroyParams that affect when and how the callback gets called.
Currently the supported parameters are:
Synchronous: Will call the callback using task.spawn instead of directly calling it. This is useful if you know the callback will yield.
Deferred: Will call the callback using task.defer. This takes priority over Synchronous, but Synchronous must be defined as true.
RemoveOnDestroy: Will call the callback, and then remove it from the Tome. In cases like this, it's better to use Tome:Add which does the same thing, while being more inline with Tome.
By default, Synchronous is set to true. This is to prevent hard yielding. If for whatever reason you need the Tome callback thread to yield, you can manually set it to false.
In this example, the callback will be deferred via !#luau task.defer.
In this example, nothing happens because the callback was removed after it was called once. Hence the second time we call Tome:Destroy, nothing happens.
Tome:Yield
Arguments
thread: thread?— The thread to yield.
Adds the provided thread into the Tome (or the current thread of a thread isn't provided) and uses coroutine.resume as the destroy method.
The current thread will then yield. Keep in mind, if the main thread is not resumed, you may encounter issues.
This will result in the thread resuming once the Tome is destroyed.