This extension provides support for setting up 'event watchers'.
For example, if you want to allow interested parties to watch for 'update'
events, you might have something like this:
local thing = {}
thing.watchers = watcher.new('update')
thing.watch(events)
return thing.watchers:watch(events)
end
thing.update(value)
thing.value = value
thing.watchers:notify('update', value)
end
Then, your other code could get notifications like so:
thing.watch({
update = function(value) print "New value is "..value end
})
Then, whenever thing.update(xxx)
is called, the watcher will output "New value is xxx"
.
- Functions - API calls offered directly by the extension
- new
- Methods - API calls which can only be made on an object returned by a constructor
- events
- getCount
- notify
- unwatch
- watch
Signature |
cp.watcher.new(...) -> watcher |
Type |
Function |
Description |
Constructs a new watcher instance. |
Parameters |
... - The list of event name strings supported by the watcher.
|
Returns |
|
Signature |
cp.watcher:events() |
Type |
Method |
Description |
Returns a list of the event names supported by this watcher. |
Parameters |
|
Returns |
- The table of event names.
|
Signature |
cp.watcher:getCount() |
Type |
Method |
Description |
Returns the number of watchers currently registered. |
Parameters |
|
Returns |
|
Signature |
cp.watcher:notify(type, ...) -> nil |
Type |
Method |
Description |
Notifies watchers of the specified event type. |
Parameters |
type - The event type to notify. Must be one of the supported events.... - These parameters are passed directly to the event watcher functions.
|
Returns |
|
Signature |
cp.watcher:unwatch(id) -> boolean |
Type |
Method |
Description |
Removes the watchers which were added with the specified ID. |
Parameters |
id - The unique ID returned from watch .
|
Returns |
true if a watcher with the specified ID exists and was successfully removed.
|
Signature |
cp.watcher:watch(events) -> id |
Type |
Method |
Description |
Adds a watcher for the specified events. |
Parameters |
events - A table of functions, one for each event to watch.
|
Returns |
- A unique ID that can be passed to
unwatch to stop watching.
|