Class Sentry
Allows the table to contain events at which others can register their handlers.
The functionality is seamless, as in any key that starts with On ends up being interpreted as an event, for purposes of registering and for purposes of calling.
There is also a special handling to make setting of handlers easier -- it is only necessary to assign a function or a table of functions to the event key and they get automatically appended to the already existing event handler list.
Usage:
local MyObject = Sentry:New()
-- append a two custom event handlers to the OnAction name
MyObject.OnAction = function(v)
print("acted 1")
return v
end
MyObject.OnAction = function(v)
print("acted 2")
end
-- invoke the event handlers
MyObject.OnAction:Invoke() -- invokes both
MyObject.OnAction() -- invokes both, equivalent to using Invoke
MyObject.OnAction(true) -- invokes only the first one
-- the execution of the second one is cancelled because the first one returned true
MyObject.OnAction(false) -- invokes both and removes the first one from the list
MyObject.OnAction() -- invokes only the second one as the first one was removed
Functions
| Sentry:New (t, events) | Creates new instance of Sentry. |
Tables
| Sentry | Accessible fields. |
Class EventHandler
| EventHandler:Invoke (...) | Invokes the execution of all registered functions, until one returns a non-false result. |
Functions
- Sentry:New (t, events)
-
Creates new instance of Sentry.
Remarks:
-
The second return value, corresponding to the
eventsparameter, should not be directly exposed to the user, as its functionality is already presented in the Sentry itself.Parameters:
- t
tab
preexisting table to convert into a Sentry or
nil - events
{[string]=EventHandler}
preexisting table of event handlers or
nil
Returns:
- Sentry
-
{[string]=EventHandler}
eventsparameter or a newly created table altered in such a way, that any key starting withOncreates a new EventHandler set
- t
tab
preexisting table to convert into a Sentry or
Tables
- Sentry
-
Accessible fields.
Remarks:
-
The EventHandlers do not need to be created, they are formed when accessed for the first time.
Fields:
- On__name__ EventHandler, func or {func,...} list of event handlers when accessed, function or a list of functions that appends to the event handler list when assigned to