Lua based implementation of Activation Tokens for Figura (though it should mostly work for Lua in general). Activation tokens are a tool used to ensure that a boolean value isn't 'fought over'.
local myTokenSource = ActivationTokenSource.new() print(myTokenSource:IsActive()) --prints 'false'
local token = myTokenSource:RequestToken() --requesting a token will activate the source and return a token
print(myTokenSource:IsActive()) --prints 'true' local token = myTokenSource:RequestToken()
print(myTokenSource:IsActive()) --prints 'true'
token:Cancel() --cancel the token
token = nil --discard used token
print(myTokenSource:IsActive()) --prints 'false' again
An ActivationTokenSource will remain active as long as there is at least one valid ActivationToken belonging to it.
local tokenA = myTokenSource:RequestToken()
local tokenB = myTokenSource:RequestToken()
print(myTokenSource:IsActive()) --prints 'true'
tokenA:Cancel() --only cancels this token
print(myTokenSource:IsActive()) --still prints 'true', as tokenB has not been cancelled
tokenB:Cancel()
print(myTokenSource:IsActive()) --prints 'false', as all tokens have been cancelled
local DoSomethingCool = function() end
local aTable = {}
local aTable:DoSomethingDifferent = function(self) end
local DoSomethingElse = function(state) end
myTokenSource.OnActivation(nil, DoSomethingCool) --reacts when the token source is enabled, setting the first parameter blank assumes that this is a . call
myTokenSource.OnDeactivation(aTable, DoSomethingDifferent) --reacts when the token source is disabled, note that you can pass the 'self' parameter in first to perform a : call
myTokenSource.OnChange(nil, DoSomethingElse) --reacts when the token source is enabled OR disabled, providing a function with a bool parameter will allow you to easily check the state of the ActivationTokenSource