Skip to content
This repository was archived by the owner on Jul 12, 2020. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 22 additions & 4 deletions lib/Signal.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
executing an event.
]]

local assign = import("./assign")
local typeKey = import("./typeKey")

local function immutableAppend(list, ...)
Expand Down Expand Up @@ -34,6 +35,19 @@ local function immutableRemoveValue(list, removeValue)
return new
end

local connectionMetatable = {}
connectionMetatable[typeKey] = "RBXScriptConnection"

function connectionMetatable:__index(key)
local internal = getmetatable(self).internal

if internal[key] ~= nil then
return internal[key]
end

error(string.format("%s is not a valid member of RBXScriptConnection", tostring(key)), 2)
end

local Signal = {}

Signal.__index = Signal
Expand All @@ -56,11 +70,15 @@ function Signal:Connect(callback)

internal.listeners = immutableAppend(internal.listeners, callback)

local connection = {}
connection.Connected = true
local connection = newproxy(true)
local instance = getmetatable(connection)
instance.internal = {
Connected = true
}
assign(instance, connectionMetatable)

function connection.Disconnect()
connection.Connected = false
function instance.internal.Disconnect()
instance.internal.Connected = false
internal.listeners = immutableRemoveValue(internal.listeners, callback)
end

Expand Down
11 changes: 11 additions & 0 deletions lib/Signal_spec.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
local Signal = import("./Signal")
local typeof = import("./functions/typeof")

describe("Signal", function()
it("should be instantiable", function()
Expand Down Expand Up @@ -94,4 +95,14 @@ describe("Signal", function()
signal:Wait()
end)
end)

it("should have connections with the typeof RBXScriptConnection", function()
assert.equal("RBXScriptConnection", typeof(Signal.new():Connect(function() end)))
end)

it("should error when giving a bad index", function()
assert.has.errors(function()
local _ = Signal.new():Connect(function() end).Doge
end)
end)
end)