-- Compiled with roblox-ts v3.0.0 local TS = _G[script] local cleanup = TS.import(script, TS.getModule(script, "@rbxts", "vide").src).cleanup local connect = function(event, callback) local _event = event if typeof(_event) == "RBXScriptSignal" then -- With deferred events, a "hard disconnect" is necessary to avoid causing -- state updates after a component unmounts. Use 'Connected' to check if -- the connection is still valid before invoking the callback. -- https://devforum.roblox.com/t/deferred-engine-events/2276564/99 local connection connection = event:Connect(function(...) local args = { ... } if connection.Connected then return callback(unpack(args)) end end) return connection end if event.Connect ~= nil then return event:Connect(callback) end if event.connect ~= nil then return event:connect(callback) end if event.subscribe ~= nil then return event:subscribe(callback) end error("Event-like object does not have a supported connect method.") end local disconnect = function(connection) local _connection = connection if type(_connection) == "function" then connection() else local _connection_1 = connection local _condition = typeof(_connection_1) == "RBXScriptConnection" if not _condition then _condition = connection.Disconnect ~= nil end if _condition then connection:Disconnect() elseif connection.disconnect ~= nil then connection:disconnect() else error("Connection-like object does not have a supported disconnect method.") end end end local function useEvent(event, listener) local connection = connect(event, listener) cleanup(function() disconnect(connection) end) end return { useEvent = useEvent, }