Commit Graph

4 Commits

Author SHA1 Message Date
Adrian Perez de Castro
7952f15053 eventable: Fix unhooking of handlers 2016-07-03 22:55:28 +03:00
Adrian Perez de Castro
4e9899dbb5 Simplify matrix.eventable for the common use cases
This removes the support for passing values to eventable.functions() and
eventable.object() which are bound to be passed to handlers. On top of
simplifying the code overall, this also avoids having three different
implementations of the fire() function.

In the existing code, the only case in which a handler argument is fixed
is for objects which pass themselves. This use case is covered by
eventable.object(), so the removed code was not realy ever used.
2016-07-03 22:21:33 +03:00
Adrian Perez de Castro
fc99710658 Improvements to matrix.eventable
* Support debug-logging to stderr by defining the MATRIX_EVENTABLE_DEBUG_LOG
  environment variable.
* Support for unhooking handlers from events.
* Simplify the low-level interface: eventable.functions() now returns the
  created functions directly, cutting down on an intermediate table.
* A new eventable.object() allows adding :hook(), :unhook() and :fire()
  methods to any table. Firing events always sends the table itself as first
  event parameter when firing any event.
2016-07-01 04:55:16 +03:00
Adrian Perez de Castro
99fbf8eb28 First pass at an event-based notification facilty
The matrix.eventable module/function returns a table with a pair of functions
which can be used to connect event handlers to events, and to generate them.
Basic usage is as follows:

  obj = eventable()
  obj.hook("event-name", print)  -- Handle the event with "print"
  obj.fire("event-name", "Hello, world")  -- Prints "Hello, world"

This initial implementation has the following limitations:

* Event handlers are always invoked in the same order in which they have been
  connected using the .hook() function.
* Individual event handlers cannot be disconnected. Only disconnecting all
  handlers for en event at once is possible via .hook("event", nil).
2016-06-30 00:24:24 +03:00