Skip to main content
Represents a trigger property that can fire events and notify listeners. Unlike other Property types, a trigger does not store a persistent value. It emits an event when fire() is called.

Fields

addListener

Registers a listener that is invoked when the trigger fires. This function has two signatures:
  • addListener(callback) — Simple form, just pass the callback function.
  • addListener(anchor, callback) — Pass an anchor object and a callback. The anchor is stored alongside the listener and passed to the callback when invoked. This is useful for preventing garbage collection of objects you need to keep alive while the listener is active.
Important: If you obtain a ViewModel or property using a local variable (e.g., local vm = context:viewModel()) and add listeners without storing the ViewModel elsewhere, it may be garbage collected after the function returns. To prevent this, either:
  • Store the ViewModel on self (e.g., self.vm = context:viewModel())
  • Pass the ViewModel as the anchor parameter to addListener
Using the anchor parameter to keep the ViewModel alive:

removeListener

Removes a previously registered listener. Always remove listeners when they are no longer needed to avoid leaks. This function has two signatures:
  • removeListener(callback) — Pass the callback function to remove.
  • removeListener(anchor, callback) — Pass an anchor and the callback. The anchor parameter is accepted for API symmetry with addListener, but only the callback is used for matching.

Methods

fire

Fires the trigger and notifies all registered listeners.