Skip to main content

Signal

Author: dig1t

A lightweight signal implementation that allows for firing, connecting, and disconnecting.

Install with wally by adding the following to your wally.toml:

Signal = "dig1t/signal@1.0.1"

Types

Callback

type Callback = (T...) → ()

Functions

new

Signal.new(optionsBufferOptions?) → Signal<T...>

Types

interface BufferOptions {
sizeLimitnumber?
bufferTimenumber?
}

Starts a new signal

On creation, an onDestroy function can be defined to run when the signal is destroyed.

Fire

Signal:Fire(
...tuple--

The arguments to pass to the signal

) → ()

Fires the signal with the given arguments All arguments are stored in the buffer until the buffer is full or the buffer time has elapsed

Connect

Signal:Connect(
callbackCallback<T...>--

The function to call when the signal is fired

) → RBXScriptConnection--

The connection object that can be used to disconnect

Connects to the signal.

local signal = Signal.new()

signal:Connect(function(message: string)
	print(message) -- "Hello, world!"
end)

signal:Fire("Hello, world!")

Once

Signal:Once(
callbackCallback<T...>--

The function to call when the signal is fired

) → ()

Connects to the signal and disconnects after the first fire.

local signal = Signal.new()

signal:Once(function(message: string)
	print(message) -- "Hello, world!"
end)

signal:Fire("Hello, world!")

Wait

Signal:Wait() → ()

Blocks the current thread until the signal is fired.

local signal = Signal.new()

task.spawn(function()
	task.wait(1)
	signal:Fire("Hello, world!")
end)

local message = signal:Wait() -- Blocks until the signal is fired
print(message) -- "Hello, world!"

Destroy

Cleanup
Signal:Destroy() → ()

Disconnects all connections from the signal.

Destroy is not necessary to call as the signal will be garbage collected when it is no longer referenced.

Show raw api
{
    "functions": [
        {
            "name": "new",
            "desc": "Starts a new signal\n\nOn creation, an `onDestroy` function can be defined to run when the signal is destroyed.",
            "params": [
                {
                    "name": "options",
                    "desc": "",
                    "lua_type": "BufferOptions?"
                }
            ],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "Signal<T...>\n"
                }
            ],
            "function_type": "static",
            "source": {
                "line": 118,
                "path": "src/Signal/init.luau"
            }
        },
        {
            "name": "Fire",
            "desc": "Fires the signal with the given arguments\nAll arguments are stored in the buffer until the buffer is full or the buffer time has elapsed",
            "params": [
                {
                    "name": "...",
                    "desc": "The arguments to pass to the signal",
                    "lua_type": "tuple"
                }
            ],
            "returns": [],
            "function_type": "method",
            "source": {
                "line": 143,
                "path": "src/Signal/init.luau"
            }
        },
        {
            "name": "_flush",
            "desc": "Flushes the buffer and fires all events.",
            "params": [],
            "returns": [],
            "function_type": "method",
            "private": true,
            "source": {
                "line": 169,
                "path": "src/Signal/init.luau"
            }
        },
        {
            "name": "Disconnect",
            "desc": "Disconnects a connection from the signal using the connection's ID.\n\nFor advanced usage, you can store the connection's ID and disconnect it later.",
            "params": [],
            "returns": [],
            "function_type": "method",
            "private": true,
            "source": {
                "line": 223,
                "path": "src/Signal/init.luau"
            }
        },
        {
            "name": "Connect",
            "desc": "Connects to the signal.\n\n```lua\nlocal signal = Signal.new()\n\nsignal:Connect(function(message: string)\n\tprint(message) -- \"Hello, world!\"\nend)\n\nsignal:Fire(\"Hello, world!\")\n```",
            "params": [
                {
                    "name": "callback",
                    "desc": "The function to call when the signal is fired",
                    "lua_type": "Callback<T...>"
                }
            ],
            "returns": [
                {
                    "desc": "The connection object that can be used to disconnect",
                    "lua_type": "RBXScriptConnection"
                }
            ],
            "function_type": "method",
            "source": {
                "line": 245,
                "path": "src/Signal/init.luau"
            }
        },
        {
            "name": "Once",
            "desc": "Connects to the signal and disconnects after the first fire.\n\n```lua\nlocal signal = Signal.new()\n\nsignal:Once(function(message: string)\n\tprint(message) -- \"Hello, world!\"\nend)\n\nsignal:Fire(\"Hello, world!\")\n```",
            "params": [
                {
                    "name": "callback",
                    "desc": "The function to call when the signal is fired",
                    "lua_type": "Callback<T...>"
                }
            ],
            "returns": [],
            "function_type": "method",
            "source": {
                "line": 271,
                "path": "src/Signal/init.luau"
            }
        },
        {
            "name": "Wait",
            "desc": "Blocks the current thread until the signal is fired.\n\n```lua\nlocal signal = Signal.new()\n\ntask.spawn(function()\n\ttask.wait(1)\n\tsignal:Fire(\"Hello, world!\")\nend)\n\nlocal message = signal:Wait() -- Blocks until the signal is fired\nprint(message) -- \"Hello, world!\"\n```",
            "params": [],
            "returns": [],
            "function_type": "method",
            "source": {
                "line": 304,
                "path": "src/Signal/init.luau"
            }
        },
        {
            "name": "Destroy",
            "desc": "Disconnects all connections from the signal.\n\n`Destroy` is not necessary to call as the signal will be garbage collected when it is no longer referenced.",
            "params": [],
            "returns": [],
            "function_type": "method",
            "tags": [
                "Cleanup"
            ],
            "source": {
                "line": 338,
                "path": "src/Signal/init.luau"
            }
        }
    ],
    "properties": [],
    "types": [
        {
            "name": "BufferOptions",
            "desc": "",
            "fields": [
                {
                    "name": "sizeLimit",
                    "lua_type": "number?",
                    "desc": ""
                },
                {
                    "name": "bufferTime",
                    "lua_type": "number?",
                    "desc": ""
                }
            ],
            "source": {
                "line": 38,
                "path": "src/Signal/init.luau"
            }
        },
        {
            "name": "Callback",
            "desc": "",
            "lua_type": "(T...) -> ()",
            "source": {
                "line": 56,
                "path": "src/Signal/init.luau"
            }
        }
    ],
    "name": "Signal",
    "desc": "Author: dig1t\n\nA lightweight signal implementation that allows for firing, connecting, and disconnecting.\n\nInstall with wally by adding the following to your `wally.toml`:\n```toml\nSignal = \"dig1t/signal@1.0.1\"\n```",
    "source": {
        "line": 13,
        "path": "src/Signal/init.luau"
    }
}