Internationalisation
Bundled Plugins API
watcher
​
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:
1
local thing = {}
2
​
3
thing.watchers = watcher.new('update')
4
​
5
thing.watch(events)
6
return thing.watchers:watch(events)
7
end
8
​
9
thing.update(value)
10
thing.value = value
11
thing.watchers:notify('update', value)
12
end
Copied!
Then, your other code could get notifications like so:
1
thing.watch({
2
update = function(value) print "New value is "..value end
3
})
Copied!
Then, whenever thing.update(xxx) is called, the watcher will output "New value is xxx".

API Overview

  • 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​

API Documentation

Functions

​new​

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
  • a new watcher instance

Methods

​events​

Signature
cp.watcher:events()
Type
Method
Description
Returns a list of the event names supported by this watcher.
Parameters
  • None
Returns
  • The table of event names.

​getCount​

Signature
cp.watcher:getCount()
Type
Method
Description
Returns the number of watchers currently registered.
Parameters
  • None
Returns
  • The number of watchers.

​notify​

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
  • Nothing.

​unwatch​

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.

​watch​

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.
Export as PDF
Copy link