diff --git a/lib/Signal.lua b/lib/Signal.lua index 87fe40e..3b806a1 100644 --- a/lib/Signal.lua +++ b/lib/Signal.lua @@ -5,6 +5,7 @@ executing an event. ]] +local assign = import("./assign") local typeKey = import("./typeKey") local function immutableAppend(list, ...) @@ -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 @@ -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 diff --git a/lib/Signal_spec.lua b/lib/Signal_spec.lua index 38c5f72..8577539 100644 --- a/lib/Signal_spec.lua +++ b/lib/Signal_spec.lua @@ -1,4 +1,5 @@ local Signal = import("./Signal") +local typeof = import("./functions/typeof") describe("Signal", function() it("should be instantiable", function() @@ -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) \ No newline at end of file