stravant / goodsignal

A Roblox Lua Signal implementation that has full API and behavioral parity with Roblox' RBXScriptSignal
MIT License
46 stars 12 forks source link

Add Signal:Once #5

Closed Baileyeatspizza closed 2 years ago

Baileyeatspizza commented 2 years ago

Overview and reasoning

This pull request aims to put GoodSignal on par with what Roblox is bringing in a future update image

goodSignal:Once(function(...)
    -- Do stuff
end)

essentially this method is just syntax sugar for

local conn; conn = goodSignal:Connect(function(...)
    conn:Disconnect()
    -- Do stuff
end)

Performance Comparison

image (consider opening the results in fullscreen since it's hard to read in the preview 😞) benchmarked using a Ryzen 9 3700X, your results may differ slightly

What do these results mean?

The benchmark directly compares Signal:Once and what was called syntax sugar previously. Demonstrating that the difference is negligible in most use cases for the added functionality and readability. Unfortunately, at the time of making this Roblox's implementation hasn't been released publically. So there is no way to reasonably compare it against it.

Changes are welcome

If you feel as if some part of the code can be changed to better suit the overall style of the module. Feel free to mention it in the comments or request an edit!

Closing

having a method to connect a signal and instantly disconnect has been a pretty standard feature in a few signal libraries here's an example SleitNicks implimentation now that Roblox has decided to adopt the implementation it only seems fitting that GoodSignal should as well.

stravant commented 2 years ago

Thanks for the PR but I implemented this myself as there were other changes needed and I wanted to keep the source code exactly in sync with the code in the Models published on Roblox and in my gists linked in the post.

Specifically I also had to lift the restriction that you can only disconnect a connection once because it's mostly incompatible with the Once API's semantics (it makes using the connection returned by Once difficult).