lloyAlloy on GitHub
Scope
Docs

Scope

Updated
Scope
import { Scope } from "@alloy/std/async"

local part = new Instance("Part")
local function on_touch(hit: BasePart) print(hit.Name) end

local scope = new Scope()
scope:add(part.Touched:Connect(on_touch))
scope:add(function() print("bye") end)
delete scope

A cleanup bag, from the std. add takes anything delete accepts, an Instance, a connection, a thread, a table with Destroy or Disconnect, or a function, and gives it back, so local conn = scope:add(signal:Connect(f)) reads as before. clean runs the cleanups newest first and empties the bag; delete scope does the same through Destroy. A scope may hold another scope.

Statics

Scope.new

Scope.new(): Scope

An empty cleanup bag.

import { Scope } from "@alloy/std/async"

local scope = new Scope()
print(scope:len())

Methods

Scope:add

Scope:add<T>(item: T, owner: Instance?): T

Adds a cleanup and gives item straight back, so a binding reads as it did before. It takes anything delete accepts, or a function. owner names the Instance the item belongs to: delete on that Instance cleans the item before the Destroy.

import { Scope } from "@alloy/std/async"

local scope = new Scope()
local stop = scope:add(function() print("bye") end)
print(stop ~= nil)

Scope:remove

Scope:remove(item: any): boolean

Takes a cleanup out without running it, and says whether it was there.

import { Scope } from "@alloy/std/async"

local scope = new Scope()
local f = scope:add(function() end)
print(scope:remove(f))

Scope:clean

Scope:clean()

Runs every cleanup, newest first, and empties the bag. A function is called; anything else goes through delete.

import { Scope } from "@alloy/std/async"

local scope = new Scope()
scope:add(function() print("bye") end)
scope:clean()

Scope:extend

Scope:extend(): Scope

A child scope, already added to this one, so cleaning the parent cleans the child.

import { Scope } from "@alloy/std/async"

local scope = new Scope()
local child = scope:extend()
child:add(function() end)

Scope:len

Scope:len(): number

The number of cleanups the bag holds.

import { Scope } from "@alloy/std/async"

local scope = new Scope()
scope:add(function() end)
print(scope:len())

Scope:Destroy

Scope:Destroy()

The same function as clean, under the name delete calls. delete scope is the form to write.

import { Scope } from "@alloy/std/async"

local scope = new Scope()
scope:add(function() end)
delete scope