Skip to main content

Observer

The Observer class allows for the construction of tables with observational capabilities.

Functions

new

Observer.new() → {[any]:any}--

Returns a table with observational capabilities.

This function allows you to create a new observational table.

note

The returned data type is actually a userdata. In order to performantly emit table read / write events, we need to use this setup.

local Observer = require(game:GetService("ReplicatedStorage"):WaitForChild("Commons")).Observer
local MyTable = Observer.new()

MyTable.onRead(function(key)
	print("Someone read " .. key .. " from MyTable!")
end)

MyTable.abc = 123

print(MyTable.abc) -- Triggers the pre-defined onRead function

cleanup

Observer.cleanup(
proxy{[any]:any}--

The observational table you want to clean up.

) → ()

This function cleans up an observational table.

local Observer = require(game:GetService("ReplicatedStorage"):WaitForChild("Commons")).Observer
local MyTable = Observer.new()

MyTable.onRead(function(key)
	print("Someone read " .. key .. " from MyTable!")
end)

MyTable.abc = 123

print(MyTable.abc) -- Triggers the pre-defined onRead function

Observer.cleanup(MyTable)
Show raw api
{
    "functions": [
        {
            "name": "new",
            "desc": "This function allows you to create a new observational table.\n\n:::note\nThe returned data type is actually a `userdata`.  In order to performantly emit table read / write events, we need to use this setup.\n\n```lua\nlocal Observer = require(game:GetService(\"ReplicatedStorage\"):WaitForChild(\"Commons\")).Observer\nlocal MyTable = Observer.new()\n\nMyTable.onRead(function(key)\n\tprint(\"Someone read \" .. key .. \" from MyTable!\")\nend)\n\nMyTable.abc = 123\n\nprint(MyTable.abc) -- Triggers the pre-defined onRead function\n```",
            "params": [],
            "returns": [
                {
                    "desc": "Returns a table with observational capabilities.",
                    "lua_type": "{[any]:any}"
                }
            ],
            "function_type": "static",
            "source": {
                "line": 75,
                "path": "src/Commons/Observer.luau"
            }
        },
        {
            "name": "cleanup",
            "desc": "This function cleans up an observational table.\n\n```lua\nlocal Observer = require(game:GetService(\"ReplicatedStorage\"):WaitForChild(\"Commons\")).Observer\nlocal MyTable = Observer.new()\n\nMyTable.onRead(function(key)\n\tprint(\"Someone read \" .. key .. \" from MyTable!\")\nend)\n\nMyTable.abc = 123\n\nprint(MyTable.abc) -- Triggers the pre-defined onRead function\n\nObserver.cleanup(MyTable)\n```",
            "params": [
                {
                    "name": "proxy",
                    "desc": "The observational table you want to clean up.",
                    "lua_type": "{[any]:any}"
                }
            ],
            "returns": [],
            "function_type": "static",
            "source": {
                "line": 122,
                "path": "src/Commons/Observer.luau"
            }
        }
    ],
    "properties": [],
    "types": [],
    "name": "Observer",
    "desc": "The Observer class allows for the construction of tables with observational capabilities.",
    "source": {
        "line": 24,
        "path": "src/Commons/Observer.luau"
    }
}