Skip to content

Lua based implementation of Activation Tokens for Figura

Notifications You must be signed in to change notification settings

Pengegg/FiggyActivationTokens

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 

Repository files navigation

FiggyActivationTokens

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'.

How to use

Create a new activation token source.

local myTokenSource = ActivationTokenSource.new()

Requesting an ActivationToken will 'activate' the ActivationTokenSource.

  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'

Tokens can be cancelled to notify the ActivationTokenSource.

  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

You can assign callbacks to the ActivationTokenSource to be notified when it is enabled/disabled

  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
  

About

Lua based implementation of Activation Tokens for Figura

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages