--[=[
	A boolean which determines if a ScriptConnection is active or not.

	@prop Connected boolean
	@within ScriptConnection

	@readonly
	@ignore
]=]


export type Class = typeof(setmetatable({
    _active = true,
    _head = nil :: ScriptConnectionNode?,
}, ScriptSignal))

export type ScriptConnection = typeof(setmetatable({
    Connected = true,
    _node = nil :: ScriptConnectionNode?,
}, ScriptConnection))

type ScriptConnectionNode = {
    _signal: Class,
    _connection: ScriptConnection?,
    _handler: (...any) -> (),

    _next: ScriptConnectionNode?,
    _prev: ScriptConnectionNode?,
}

local FreeThread: thread? = nil

local function RunHandlerInFreeThread(handler, ...)
    local thread = FreeThread :: thread
    FreeThread = nil

    handler(...)

    FreeThread = thread
end

local function CreateFreeThread()
    FreeThread = coroutine.running()

    while true do
        RunHandlerInFreeThread(coroutine.yield())
    end
end
