Last active
March 24, 2021 17:29
-
-
Save cxmeel/6d8fab884248487b6ca22b6231ebdba7 to your computer and use it in GitHub Desktop.
Pure Lua Signal
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--[[ | |
Signal class in pure Lua. | |
Based on Roblox's BindableEvents. | |
Signal.new(): Signal | |
-- Creates a new Signal object | |
Signal:Connect(Callback: function): SignalConnection | |
-- Binds a function to invoke when the Signal is fired | |
SignalConnection:Disconnect(): nil | |
-- Unbinds the previously bound function | |
Signal:Fire(...args: any): nil | |
-- Invokes bound functions, passing the given arguments | |
Signal:Destroy(): nil | |
-- Unbinds all functions, and prevents the Signal from | |
being fired/connected to again | |
--]] | |
local Signal = {}; do | |
math.randomseed(os.time()) | |
Signal.__index = Signal | |
function Signal.new() | |
local self = setmetatable({}, Signal) | |
self.__callbacks = {} | |
return self | |
end | |
function Signal:Connect(Callback) | |
local this = self | |
local CallbackId = math.random(1000000, 9999999) | |
this.__callbacks[CallbackId] = Callback | |
return { | |
Disconnect = function() | |
this.__callbacks[CallbackId] = nil | |
end | |
} | |
end | |
function Signal:Fire(...) | |
for _, callback in next, self.__callbacks do | |
coroutine.wrap(callback)(...) | |
end | |
end | |
function Signal:Destroy() | |
for key, _ in next, self.__callbacks do | |
self.__callbacks[key] = nil | |
end | |
self = setmetatable({}) | |
self = nil | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
local Signal={}do math.randomseed(os.time())Signal.__index=Signal;function Signal.new()local self=setmetatable({},Signal)self.__callbacks={}return self end;function Signal:Connect(b)local c=self;local d=math.random(1000000,9999999)c.__callbacks[d]=b;return{Disconnect=function()c.__callbacks[d]=nil end}end;function Signal:Fire(...)for e,f in next,self.__callbacks do coroutine.wrap(f)(...)end end;function Signal:Destroy()for g,e in next,self.__callbacks do self.__callbacks[g]=nil;end;self=setmetatable({})self=nil end end |
Author
cxmeel
commented
May 14, 2020
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment