Custom observable events?


(joule) #1

Is there a clean way of generating custom observable events (not a workaround)? Since there are some events (“context parameters”) in the API that don’t look quite native to the renoise.Document.observable list classes (like line_notifier), I thought there might be a way.

Click to view contents

I think it would make sense if renoise.Document.ObservableBang would support passing the function( argument ), like so:

local hello = renoise.Document.ObservableBang()
hello:add_notifier(function(event)
 print(event)
end)
hello:bang("something")

(joule) #2

Maybe disregard this… It’s pretty easy to make your custom observable-like class.

Here is an example acting like ObservableBang but with event table support, so imho it’s a bit better. (I’m using it to encapsulate everything needed to keep track of all note column names… everything in its right class and all that).

class 'ObservableEventBang'

function ObservableEventBang:__init()
  self._notifiers = table.create()
end

function ObservableEventBang:add_notifier(func)
  assert(type(func) == "function", "The given notifier argument is not a function.")
  self._notifiers:insert(func)
end

function ObservableEventBang:remove_notifier(rmv_func)
  local removed = false
  for k, func in pairs(self._notifiers) do
    if (func == rmv_func) then
      self._notifiers:remove(k)
      removed = true
    end
  end
  assert(removed, 'remove notifier: the given notifier function was not added.')
end

function ObservableEventBang:bang(event)
  for k, func in ipairs(self._notifiers) do
    func(event)
  end
end

-- test

local hello = ObservableEventBang()

hello:add_notifier(function(event)
 rprint(event)
end)

hello:bang { track = 1, column = 2 }