Skip to content

Implementation of Roblox RBXScriptSignal with easy creation and usage

License

bogotolec/custom-signal

Repository files navigation

custom-signal

Latest release Build and release status Build and release status

Custom implementation of Roblox RBXScriptSignal with easy creation and usage.

Differences with RBXScriptSignal

1. Yielding events

Calling signal:Fire(...) yields the thread until all connected functions completed. Be careful with functions you connect, you might accidentally lock the calling thread.

2. Pass variables by reference

Tables you pass as arguments to signal:Fire(...) might be changed by connected handlers. You can use it to retrieve a result of calling several handlers one by one.

Installation

Using wally

Custom Signal can be added as a dependency in Wally project:

[dependencies]
CustomSignal = "bogotolec/custom-signal@0.1.0"

Downloading from releases

  1. Go to releases page
  2. Download CustomSignal.rbxm of preferred version
  3. Insert the file into project of Roblox Studio

Usage

Creation

Signals are created simply by requiring the library and calling CustomSignal.new():

local CustomSignal = require("path/to/custom-signal")

local signal = CustomSignal.new()

Connection

There are 2 supported types of connections: permanent and once.

Type Permanent connection Once connection
Creation signal:Connect(func) signal:Once(func)
# of invokes 1
Order of call First Second

Order of call means that when you fire an event, firstly all the permanent connections will be invoked.

local onceConnection = signal:Once(function(v)
	print(`Called once: {v}`)
end)

local permanentConnection = signal:Connect(function(v)
	print(`Called permanent: {v}`)
end)

signal:Fire(1)
-- Output:
-- Called permanent: 1
-- Called once: 1

signal:Fire(2)
-- Output:
-- Called permanent: 2

Connections preserve order during fire in which they were connected.

local connection1 = signal:Connect(function()
	print(`Called 1`)
end)

local connection2 = signal:Connect(function()
	print(`Called 2`)
end)

local connection3 = signal:Connect(function()
	print(`Called 3`)
end)

signal:Fire()
-- Output:
-- Called 1
-- Called 2
-- Called 3

Disconnection

Connections can be disconnected, which stops invoking connected function

local connection1 = signal:Connect(function()
	print(`Called 1`)
end)

local connection2 = signal:Connect(function()
	print(`Called 2`)
end)

signal:Fire()
-- Output:
-- Called 1
-- Called 2

connection1:Disconnect()

signal:Fire()
-- Output:
-- Called 2

Event buffering

If signal:Fire(...) is called before any connection was created, event is stored and will be fired to invoke the next connected function. This means that function can be invoked during signal:Connect(), yielding the thread.

signal:Fire("Hello world!")

local connection = signal:Connect(function(v)
	print(v)
end)
-- Output:
-- Hello world!

About

Implementation of Roblox RBXScriptSignal with easy creation and usage

Resources

License

Stars

Watchers

Forks