#hs.watchable
A minimalistic Key-Value-Observer framework for Lua.
This module allows you to generate a table with a defined label or path that can be used to share data with other modules or code. Other modules can register as watchers to a specific key-value pair within the watchable object table and will be automatically notified when the key-value pair changes.
The goal is to provide a mechanism for sharing state information between separate and (mostly) unrelated code easily and in an independent fashion.
#API Overview
Constructors - API calls which return an object, typically one that offers API methods
Methods - API calls which can only be made on an object returned by a constructor
#API Documentation
#Constructors
| | | | --------------------------------------------|-------------------------------------------------------------------------------------| | Signature | hs.watchable.watch(path, [key], callback) -> watchableObject
| | Type | Constructor | | Description | Creates a watcher that will be invoked when the specified key in the specified path is modified. | | Parameters |
path
- a string specifying the path to watch. Ifkey
is not provided, then this should be a string of the form "path.key" where the key will be identified as the string after the last "."key
- if provided, a string specifying the specific key within the path to watch.callback
- an optional function which will be invoked when changes occur to the key specified within the path. The function should expect the following arguments:watcher
- the watcher object itselfpath
- the path being watchedkey
- the specific key within the path which invoked this callbackold
- the old value for this key, may be nilnew
- the new value for this key, may be nil
- a watchableObject
- This constructor is used by code which wishes to watch state information which is being shared by other code.
- The callback function is invoked after the new value has already been set -- the callback is a "didChange" notification, not a "willChange" notification.
- If the key (specified as a separate argument or as the final component of path) is "*", then all key-value pair changes that occur for the table specified by the path will invoke a callback. This is a shortcut for watching an entire table, rather than just a specific key-value pair of the table.
- It is possible to register a watcher for a path that has not been registered with hs.watchable.new yet. Retrieving the current value with hs.watchable:value in such a case will return nil.