Skip to content

Commit

Permalink
streamline :Fire implementation when used outside of Roblox
Browse files Browse the repository at this point in the history
  • Loading branch information
Aspecky committed Jan 22, 2024
1 parent f76a0a5 commit 0ea9183
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 29 deletions.
15 changes: 0 additions & 15 deletions docs/classes/signal.md
Original file line number Diff line number Diff line change
Expand Up @@ -141,21 +141,6 @@ local connection = signal:Connect(print, "Test:")
signal:Fire("Hello world!") --> Test: Hello world!
```

:::danger Behavior outside of Roblox
When ran outside of Roblox, `:Fire` will halt its execution when a connection errors.
```lua
local signal = LemonSignal.new()

signal:Connect(function()
error("error")
end)

signal:Fire()

print("eof") -- this line wont be reached
```
:::

### DisconnectAll
Disconnects all connections currently connected to the signal. They may be reconnected later.

Expand Down
23 changes: 10 additions & 13 deletions src/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export type Connection<U...> = {

export type Signal<T...> = {
RBXScriptConnection: RBXScriptConnection?,

Connect: <U...>(self: Signal<T...>, fn: (...any) -> (), U...) -> Connection<U...>,
Once: <U...>(self: Signal<T...>, fn: (...any) -> (), U...) -> Connection<U...>,
Wait: (self: Signal<T...>) -> T...,
Expand All @@ -21,11 +21,9 @@ export type Signal<T...> = {
}

local freeThreads: { thread } = {}
local freeThreadsCount = 0

local function runCallback(callback, thread, ...)
callback(...)
freeThreadsCount += 1
table.insert(freeThreads, thread)
end

Expand Down Expand Up @@ -145,15 +143,15 @@ local wait = if task
return coroutine.yield()
end

local task = nil
local fire = if task
then function<T...>(self: Signal<T...>, ...: any)
local cn = self._head
while cn do
local thread
if freeThreadsCount > 0 then
thread = freeThreads[1]
freeThreads[1], freeThreads[freeThreadsCount] = freeThreads[freeThreadsCount], nil
freeThreadsCount -= 1
if #freeThreads > 0 then
thread = freeThreads[#freeThreads]
freeThreads[#freeThreads] = nil
else
thread = coroutine.create(yielder)
coroutine.resume(thread)
Expand Down Expand Up @@ -184,10 +182,9 @@ local fire = if task
local cn = self._head
while cn do
local thread
if freeThreadsCount > 0 then
thread = freeThreads[1]
freeThreads[1], freeThreads[freeThreadsCount] = freeThreads[freeThreadsCount], nil
freeThreadsCount -= 1
if #freeThreads > 0 then
thread = freeThreads[#freeThreads]
freeThreads[#freeThreads] = nil
else
thread = coroutine.create(yielder)
coroutine.resume(thread)
Expand All @@ -196,7 +193,7 @@ local fire = if task
if not cn._varargs then
local passed, message = coroutine.resume(thread, cn._fn, thread, ...)
if not passed then
error(message, 0)
print(string.format("%s\nstacktrace:\n%s", message, debug.traceback()))
end
else
local args = cn._varargs
Expand All @@ -209,7 +206,7 @@ local fire = if task

local passed, message = coroutine.resume(thread, cn._fn, thread, table.unpack(args))
if not passed then
error(message, 0)
print(string.format("%s\nstacktrace:\n%s", message, debug.traceback()))
end

for i = count, len + 1, -1 do
Expand Down
2 changes: 1 addition & 1 deletion src/wally.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "data-oriented-house/lemonsignal"
version = "1.8.1"
version = "1.9.0"
registry = "https://github.com/UpliftGames/wally-index"
licence = "MIT"
authors = ["Aspecky", "Sona"]
Expand Down

0 comments on commit 0ea9183

Please sign in to comment.