Lua-Janitor

TEMPLATE NAME: Lua-Janitor
CORE VERSION: 1.0.285
TEMPLATE VERSION: 1.0.0

TEMPLATE DESCRIPTION:

Plain-Lua implementation of a janitor, for managing disposal of event connections. A concept popularized by game development on the Roblox platform.

TEMPLATE ROADMAP:

  • (no current plans)

TEMPLATE VERSION NOTES:

Version 1.0.0

  • First stable release.

ADDITIONAL INFO:


Getting Started

Import

  1. In Community Content, search for "descendent".
  2. Browse to the listing for "Lua-Janitor", and click "Import"; then click "Yes".
  3. In Core Content, under the "Community Content" category, select the "Imported Content > Lua-Janitor" group.
  4. Add the "Lua-Janitor" template to the Hierarchy (in Default Context); then delete the new template instance from the Hierarchy.
    • This step is necessary to import this package’s assets into Project Content.

How-To Guides

Use the Janitor Module in a Script

  1. In Project Content, browse to the script that will be using the "Janitor" module, and select it.
  2. In the Properties panel, add a custom property of type "Asset Reference", and name it "Janitor"; then set its default value to reference the "Janitor" script.
  3. Edit the script that will be using the "Janitor" module; at or near the top of the code, add…
    local Janitor = require(script:GetCustomProperty("Janitor"))
    
  4. Create and use Janitor instances as-needed. (See Janitor API reference.)

Update

  1. In Project Content, right-click the "Imported Content > Lua-Janitor" group, and select "Download Latest"; then click "Yes"; then click "Continue with save: I accept the risk".

Delete

  1. In Project Content, right-click the "Imported Content > Lua-Janitor" group, and select "Delete Assets"; then, if asked, click "Delete All and Save" each time; then click "Delete All" each time.
  2. In Core Content, under the "Community Content" category, right-click the "Imported Content > Lua-Janitor" group, and select "Delete Assets"; then click "Delete All" each time.

API Reference

Janitor

Constructors

Janitor New()
Janitor New(function thunk)

Creates and returns a new Janitor instance. If thunk is given, it will be used later as the Janitor instance's default cleanup function. thunk must be a function that accepts a dynamic parameter.

Methods

dynamic Add(dynamic key, dynamic value)
dynamic Add(dynamic key, dynamic value, function thunk)

Adds an entry for value value to this Janitor instance, with key key for later reference. If thunk is given, it will be used later as the entry's cleanup function. thunk must be a function that accepts a dynamic parameter. Returns value.

dynamic AddValue(dynamic value)
dynamic AddValue(dynamic value, function thunk)

Adds an entry for value value to this Janitor instance. If thunk is given, it will be used later as the entry's cleanup function. thunk must be a function that accepts a dynamic parameter. Returns value.

nil Cleanup(dynamic key)

Removes the entry with key key from this Janitor instance. If the entry has a cleanup function, that function will be called, with the entry's value as the argument; otherwise, this Janitor instance's default cleanup function will be called, with the entry's value as the argument.

nil CleanupAll()

Removes all entries from this Janitor instance. For each entry: if the entry has a cleanup function, that function will be called, with the entry's value as the argument; otherwise, this Janitor instance's default cleanup function will be called, with the entry's value as the argument.

Examples

Usage

ExampleClient (Client Context)

Custom Properties

AssetReference Janitor

Reference to "Janitor" script.

Code

local Janitor = require(script:GetCustomProperty("Janitor"))

local _janitor = Janitor.New(EventListener.Disconnect)

local function OnExampleEvent()
    print("OnExampleEvent")
end

_janitor:AddValue(Events.Connect("ExampleEvent", OnExampleEvent))

print("Broadcast ExampleEvent")
Events.Broadcast("ExampleEvent")

_janitor:CleanupAll()

print("Broadcast ExampleEvent")
Events.Broadcast("ExampleEvent")

:receipt: (Event Log)

Broadcast ExampleEvent
OnExampleEvent
Broadcast ExampleEvent