Module lib.container.observable
observable - an observable value
Functions
new (initial) | Create an Observable value (object). |
set (value, quiet) | Set the value |
__call () | Return the current value (short hand for the 'value' method) |
value () | Return the current value |
register (observer, handler) | Register an observer of value changes
An observer is identified by a key which can be any type (number, string, table, etc). |
unregister (observer) | Unregister an observer of value changes |
notify_observers () | Notify registered observers of current value (normally called by set). |
Functions
- new (initial)
-
Create an Observable value (object).
An observable value notifies registered observers each time the value is set.
Parameters:
- initial anything Initial value, defaults to nil
Returns:
-
Observable
instance.
- set (value, quiet)
-
Set the value
Parameters:
- value anything New value for observable.
- quiet boolean If true, do not notify observers.
- __call ()
-
Return the current value (short hand for the 'value' method)
Returns:
-
anything
- value ()
-
Return the current value
Returns:
-
anything
- register (observer, handler)
-
Register an observer of value changes
An observer is identified by a key which can be any type (number, string, table, etc). Registering an observer replaces any previous registration associated with that key.
The handler is a function which is passed one argument, the new value. If
handler
is not provided it is assumed that theobserver
is an object with anotify
method and thenotify
method is called with the new value.Parameters:
- observer anything Identity of observer.
- handler function New value callback, optional.
- unregister (observer)
-
Unregister an observer of value changes
Parameters:
- observer anything Identity of observer.
Returns:
-
boolean
true if the observer was known and unregistered.
- notify_observers ()
- Notify registered observers of current value (normally called by set).