From 2873c1499b1a88847b30ce93e8da3a8891754e3d Mon Sep 17 00:00:00 2001 From: mitchell <70453897+667e-11@users.noreply.github.com> Date: Mon, 14 Jun 2010 18:14:25 -0400 Subject: Added events.disconnect() function; core/events.lua --- core/events.lua | 15 +++++++++++++++ 1 file changed, 15 insertions(+) (limited to 'core') diff --git a/core/events.lua b/core/events.lua index d80857f6..3313d919 100644 --- a/core/events.lua +++ b/core/events.lua @@ -151,6 +151,8 @@ local events = events -- anywhere. -- @param f The Lua function to add. -- @param index Optional index to insert the handler into. +-- @return Index of handler. +-- @see disconnect function connect(event, f, index) local plural = event..'s' if not events[plural] then events[plural] = {} end @@ -160,6 +162,19 @@ function connect(event, f, index) else handlers[#handlers + 1] = f end + return index or #handlers +end + +--- +-- Disconnects a handler function from an event. +-- @param event The string event name. +-- @param index Index of the handler (returned by events.connect). +-- @see connect +function disconnect(event, index) + local plural = event..'s' + if not events[plural] then return end + local handlers = events[plural] + table.remove(handlers, index) end --- -- cgit v1.2.3