Skip to main content

Signal

A Signal is a class that is used to substitute the use of Bindable Events, this implementation tries to handle most of the edge cases that could happen, while being as performant as possible, with additional features for Advanced users such as selectable disconnect mode.

This implementation tries to have similar API to the standard signal module, GoodSignal, with some additional features.

Quick example:

local NewSignal = require(path.to.NewSignal)

-- Create a signal
local Signal = NewSignal.new()

-- Connect a listener
local Connection = Signal:Connect(function(Param)
    print(`The Signal Got fired, param: {Param}`)
end)

-- Fire the signal with the default method
Signal:Fire("Hello")

-- Disconnect the listener
Connection:Disconnect()

Functions

New

Signal.New() → Signal

Creates a new Signal object

local Signal = NewSignal.new()

Get

Signal.Get(Idstring) → Signal

Gets an existing Signal object with the given Id, or creates a new one if it doesn't exist.

local Signal = NewSignal.new()

Connect

Signal:Connect(Callback(...) → ()) → Connection

Connects a function to the signal, which will be called everytime the signal is fired.

Signal:Connect(function(Message, Number)
	print(Message, Number)
end)

Signal:Fire("Hello", 25)

Once

Signal:Once(Callback(...) → ()) → Connection

Connects a function to the signal, which will be called once and disconnected immediatly.

Signal:Once(function(Message, Number)
	print(Message, Number)
end)

Signal:Fire("Hello", 25)
Signal:Fire("This message will not get printed", 10)

Wait

This is a yielding function. When called, it will pause the Lua thread that called the function until a result is ready to be returned, without interrupting other scripts. Yields
Signal:Wait() → ...any

Yields the current thread until :Fire occurs, and returns the arguments fired from the signal.

task.spawn(function()
	print(Signal:Wait()) --> "Test", 89, nil, false
end)
Signal:Fire("Test", 89, nil, false)

FireAsync

Signal:FireAsync(...any) → ()

Fires a Signal object Asynchronously with the arguments passed.

Signal:Connect(function(Text)
	print(Text) --> "Something..."
end)

Signal:FireAsync("Something...")

FireDeferred

Signal:FireDeferred(...any) → ()

Fires a Signal object with the arguments passed with a task.defer. Doesn't take use of the thread recicling protocol.

Signal:Connect(function(Text)
	print(Text) --> "Something..."
end)

Signal:FireDeferred("Something...")

FireSafe

Signal:FireSafe(...any) → ()

Fires a Signal object asynchronously with the arguments passed using a xpcall.

Signal:Connect(function(Text)
	print(Text) --> "Something..."
end)

Signal:FireSafe("Something...")

DisconnectAll

Signal:DisconnectAll() → ()

Disconnects all current Connections from the Signal.

Signal:Connect(function(Args) end)

Signal:DisconnectAll()

Destroy

Signal:Destroy() → ()

Destroys a Signal object, disconnecting all connections and making it unusable.

Signal:Connect(function(Args) end)

Signal:Destroy() -- Destroys the signal and disconnects all connections

Fire

Signal:Fire() → ()

Alias for FireAsync by default. Can be changed to any of the other fire methods by modiying the DefaultFireMethod variable.

Signal:Connect(function(Text)
	print(Text) --> "Something..."
end)

Signal:Fire("Something...") -- Same behaviour as `FireAsync` by default
Show raw api
{
    "functions": [
        {
            "name": "New",
            "desc": "Creates a new Signal object\n\n```lua\nlocal Signal = NewSignal.new()\n```",
            "params": [],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "Signal"
                }
            ],
            "function_type": "static",
            "source": {
                "line": 457,
                "path": "src/NewSignal.luau"
            }
        },
        {
            "name": "Get",
            "desc": "Gets an existing Signal object with the given Id, or creates a new one if it doesn't exist.\n\n```lua\nlocal Signal = NewSignal.new()\n```",
            "params": [
                {
                    "name": "Id",
                    "desc": "",
                    "lua_type": "string"
                }
            ],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "Signal"
                }
            ],
            "function_type": "static",
            "source": {
                "line": 470,
                "path": "src/NewSignal.luau"
            }
        },
        {
            "name": "Connect",
            "desc": "Connects a function to the signal, which will be called everytime the signal is fired.\n\n```lua\nSignal:Connect(function(Message, Number)\n\tprint(Message, Number)\nend)\n\nSignal:Fire(\"Hello\", 25)\n```",
            "params": [
                {
                    "name": "Callback",
                    "desc": "",
                    "lua_type": "(...) -> ()"
                }
            ],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "Connection"
                }
            ],
            "function_type": "method",
            "source": {
                "line": 495,
                "path": "src/NewSignal.luau"
            }
        },
        {
            "name": "Once",
            "desc": "Connects a function to the signal, which will be called once and disconnected immediatly.\n\n```lua\nSignal:Once(function(Message, Number)\n\tprint(Message, Number)\nend)\n\nSignal:Fire(\"Hello\", 25)\nSignal:Fire(\"This message will not get printed\", 10)\n```",
            "params": [
                {
                    "name": "Callback",
                    "desc": "",
                    "lua_type": "(...) -> ()"
                }
            ],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "Connection"
                }
            ],
            "function_type": "method",
            "source": {
                "line": 513,
                "path": "src/NewSignal.luau"
            }
        },
        {
            "name": "Wait",
            "desc": "Yields the current thread until `:Fire` occurs, and returns the arguments fired from the signal.\n\n```lua\ntask.spawn(function()\n\tprint(Signal:Wait()) --> \"Test\", 89, nil, false\nend)\nSignal:Fire(\"Test\", 89, nil, false)\n```",
            "params": [],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "... any"
                }
            ],
            "function_type": "method",
            "yields": true,
            "source": {
                "line": 538,
                "path": "src/NewSignal.luau"
            }
        },
        {
            "name": "FireAsync",
            "desc": "Fires a Signal object Asynchronously with the arguments passed.\n\n```lua\nSignal:Connect(function(Text)\n\tprint(Text) --> \"Something...\"\nend)\n\nSignal:FireAsync(\"Something...\")\n```",
            "params": [
                {
                    "name": "...",
                    "desc": "",
                    "lua_type": "any"
                }
            ],
            "returns": [],
            "function_type": "method",
            "source": {
                "line": 587,
                "path": "src/NewSignal.luau"
            }
        },
        {
            "name": "FireDeferred",
            "desc": "Fires a Signal object with the arguments passed with a `task.defer`.\nDoesn't take use of the thread recicling protocol.\n\n```lua\nSignal:Connect(function(Text)\n\tprint(Text) --> \"Something...\"\nend)\n\nSignal:FireDeferred(\"Something...\")\n```",
            "params": [
                {
                    "name": "...",
                    "desc": "",
                    "lua_type": "any"
                }
            ],
            "returns": [],
            "function_type": "method",
            "source": {
                "line": 618,
                "path": "src/NewSignal.luau"
            }
        },
        {
            "name": "FireSafe",
            "desc": "Fires a Signal object asynchronously with the arguments passed using a `xpcall`.\n\n```lua\nSignal:Connect(function(Text)\n\tprint(Text) --> \"Something...\"\nend)\n\nSignal:FireSafe(\"Something...\")\n```",
            "params": [
                {
                    "name": "...",
                    "desc": "",
                    "lua_type": "any"
                }
            ],
            "returns": [],
            "function_type": "method",
            "source": {
                "line": 647,
                "path": "src/NewSignal.luau"
            }
        },
        {
            "name": "DisconnectAll",
            "desc": "Disconnects all current Connections from the Signal.\n\n```lua\nSignal:Connect(function(Args) end)\n\nSignal:DisconnectAll()\n```",
            "params": [],
            "returns": [],
            "function_type": "method",
            "source": {
                "line": 674,
                "path": "src/NewSignal.luau"
            }
        },
        {
            "name": "Destroy",
            "desc": "Destroys a Signal object, disconnecting all connections and making it unusable.\n\n```lua\nSignal:Connect(function(Args) end)\n\nSignal:Destroy() -- Destroys the signal and disconnects all connections\n```",
            "params": [],
            "returns": [],
            "function_type": "method",
            "source": {
                "line": 704,
                "path": "src/NewSignal.luau"
            }
        },
        {
            "name": "Fire",
            "desc": "Alias for FireAsync by default.\nCan be changed to any of the other fire methods by modiying the `DefaultFireMethod` variable.\n\n```lua\nSignal:Connect(function(Text)\n\tprint(Text) --> \"Something...\"\nend)\n\nSignal:Fire(\"Something...\") -- Same behaviour as `FireAsync` by default\n```",
            "params": [],
            "returns": [],
            "function_type": "method",
            "source": {
                "line": 775,
                "path": "src/NewSignal.luau"
            }
        }
    ],
    "properties": [],
    "types": [],
    "name": "Signal",
    "desc": "A Signal is a class that is used to substitute the use of Bindable Events, this implementation\ntries to handle most of the edge cases that could happen, while being as performant as possible,\nwith additional features for Advanced users such as selectable disconnect mode.\n\nThis implementation tries to have similar API to the standard signal module, [GoodSignal](https://devforum.roblox.com/t/lua-signal-class-comparison-optimal-goodsignal-class/1387063),\nwith some additional features.\n\nQuick example:\n```lua\nlocal NewSignal = require(path.to.NewSignal)\n\n-- Create a signal\nlocal Signal = NewSignal.new()\n\n-- Connect a listener\nlocal Connection = Signal:Connect(function(Param)\n    print(`The Signal Got fired, param: {Param}`)\nend)\n\n-- Fire the signal with the default method\nSignal:Fire(\"Hello\")\n\n-- Disconnect the listener\nConnection:Disconnect()\n```",
    "source": {
        "line": 108,
        "path": "src/NewSignal.luau"
    }
}