Skip to content
This repository has been archived by the owner on Dec 13, 2023. It is now read-only.

Better error message for invalid changed hook name #216

Merged
merged 9 commits into from
Jul 25, 2019
14 changes: 12 additions & 2 deletions src/SingleEventManager.lua
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,17 @@ function SingleEventManager:connectEvent(key, listener)
end

function SingleEventManager:connectPropertyChange(key, listener)
local event = self._instance:GetPropertyChangedSignal(key)
local success, event = pcall(function()
return self._instance:GetPropertyChangedSignal(key)
end)

if not success then
error(("Cannot subscribe to changed signal: %q is not a property of %s"):format(
jeparlefrancais marked this conversation as resolved.
Show resolved Hide resolved
tostring(key),
self._instance.ClassName
), 0)
end

self:_connect(CHANGE_PREFIX .. key, event, listener)
end

Expand Down Expand Up @@ -134,4 +144,4 @@ function SingleEventManager:resume()
self._suspendedEventQueue = {}
end

return SingleEventManager
return SingleEventManager
9 changes: 9 additions & 0 deletions src/SingleEventManager.spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -226,5 +226,14 @@ return function()
instance.Name = "baz"
expect(eventSpy.callCount).to.equal(2)
end)

it("should throw an error if the property is invalid", function()
local instance = Instance.new("Folder")
local manager = SingleEventManager.new(instance)

expect(function()
manager:connectPropertyChange("foo", function() end)
end).to.throw()
end)
end)
end