#cp.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:

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".


#API Overview

Functions - API calls offered directly by the extension

Methods - API calls which can only be made on an object returned by a constructor


#API Documentation

#Functions

Signaturecp.watcher.new(...) -> watcher
TypeFunction
DescriptionConstructs a new watcher instance.
Parameters
  • ... - The list of event name strings supported by the watcher.
Returns
  • a new watcher instance
NotesNone
ExamplesNone
Sourcesrc/extensions/cp/watcher/init.lua line 44

#Methods

Signaturecp.watcher:events()
TypeMethod
DescriptionReturns a list of the event names supported by this watcher.
Parameters
  • None
Returns
  • The table of event names.
NotesNone
ExamplesNone
Sourcesrc/extensions/cp/watcher/init.lua line 62

Signaturecp.watcher:getCount()
TypeMethod
DescriptionReturns the number of watchers currently registered.
Parameters
  • None
Returns
  • The number of watchers.
NotesNone
ExamplesNone
Sourcesrc/extensions/cp/watcher/init.lua line 151

Signaturecp.watcher:notify(type, ...) -> nil
TypeMethod
DescriptionNotifies 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.
NotesNone
ExamplesNone
Sourcesrc/extensions/cp/watcher/init.lua line 131

Signaturecp.watcher:unwatch(id) -> boolean
TypeMethod
DescriptionRemoves 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.
NotesNone
ExamplesNone
Sourcesrc/extensions/cp/watcher/init.lua line 111

Signaturecp.watcher:watch(events) -> id
TypeMethod
DescriptionAdds 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.
NotesNone
ExamplesNone
Sourcesrc/extensions/cp/watcher/init.lua line 95