Callback
Callback
s are used to monitor the existence of a specific event and if that specific event occurs, some other special jobs are invoked. Callback
s are intended to provide additional monitoring capability to any user-defined composite types. As such, Callback
s are generally fields of user defined composite types. When a Callback
is called, if the Callback
is enabled and its condition
function returns true, then its action
function is invoked.
A Simple Example
Let's define a test object first that has a field named x
of type Int
and named callback
of type Callback
.
julia> mutable struct TestObject
x::Int
callback::Callback
end
To construct an instance of TestObject
, we need to construct a Callback
. For that purpose, the condition
and action
function must be defined. For this example, condition
checks whether the x
field is positive, and action
prints a simple message saying that the x
field is positive.
julia> condition(testobject) = testobject.x > 0
condition (generic function with 1 method)
julia> action(testobject) = println("testobject.x is greater than zero")
action (generic function with 1 method)
Now a test object can be constructed
julia> testobject = TestObject(-1, Callback(condition, action))
TestObject(-1, Callback{typeof(condition),typeof(action)}(condition, action, true, "dac6f9eb-6daa-4622-a8fa-623f0f88780c"))
If the callback is called, no action is performed since the condition
function returns false. Note the argument sent to the callback. The instance of the TestObject
to which the callback is bound.
julia> testobject.callback(testobject)
Now mutate the test object so that condition
returns true.
julia> testobject.x = 3
3
Now, if the callback is called, since the condition
returns true and the callback is enabled
, the action
is invoked.
julia> testobject.callback(testobject)
testobject.x is greater than zero
Full API
Causal.Callback
— Typemutable struct Callback{CN, AC}
Constructs a Callback
from condition
and action
. The condition
and action
must be a single-argument function. The condition
returns true
if the condition it checks occurs, otherwise, it returns false
. action
performs the specific action for which the Callback
is constructed. A Callback
can be called by passing its single argument which is mostly bound to the Callback
.
Fields
condition::Any
Condition function of callback. Expected to be a single-argument function
action::Any
Action of the callback. Expected to be a single-argument fucntion
enabled::Bool
If true, callback is activated
id::Base.UUID
Unique identifier
Example
julia> struct Object # Define a dummy type.
x::Int
clb::Callback
end
julia> cond(obj) = obj.x > 0; # Define callback condition.
julia> action(obj) = println("obj.x = ", obj.x); # Define callback action.
julia> obj = Object(1, Callback(condition=cond, action=action))
Object(1, Callback(condition:cond, action:action))
julia> obj.clb(obj) # Call the callback bound `obj`.
obj.x = 1
Causal.applycallbacks
— Methodapplycallbacks(obj)
Calls the callbacks of obj
if the callbacks are not nothing.
Example
julia> mutable struct MyType{CB}
x::Int
callbacks::CB
end
julia> obj = MyType(5, Callback(condition=obj -> obj.x > 0, action=obj -> println("x is positive")));
julia> applycallbacks(obj)
x is positive
julia> obj.x = -1
-1
julia> applycallbacks(obj)
Causal.disable!
— Methoddisable!(clb)
Disables clb
.
Causal.enable!
— Methodenable!(clb)
Enables clb
.
Causal.isenabled
— Methodisenabled(clb)
Returns true
if clb
is enabled. Otherwise, returns false
.