OpenComputers

OpenComputers

46M Downloads

Event API - timeout ignores filters

DragDen opened this issue ยท 1 comments

commented

While spending a couple of days banging my head against the desk I noticed a rather suspicious part in an OpenOS event lib.
In the file /lib/event.lua on the lines 143-150 we have this code:

  repeat
    local signal = table.pack(computer.pullSignal(seconds))
    if signal.n > 0 then
      if not (seconds or filter) or filter == nil or filter(table.unpack(signal, 1, signal.n)) then
        return table.unpack(signal, 1, signal.n)
      end
    end
  until signal.n == 0

The 'seconds' variable here contains the amount of seconds for a timeout.
However, if I understand correctly, the timeout will only lapse if there is not a single event in computer's queue. Regardless of filters.

For example, if we're pulling for a modem_message with a timeout of two seconds, and repeatedly mash a key on the keyboard, the pullEvent will keep waiting indefinitely (until we leave the keyboard alone).

commented

My suggestion for a fix is something like this:

  local startSeconds = computer.uptime()
  repeat
    local waitTime = seconds - (computer.uptime() - startSeconds)
    local signal = table.pack(computer.pullSignal(waitTime))
    if signal.n > 0 then
      if not (seconds or filter) or filter == nil or filter(table.unpack(signal, 1, signal.n)) then
        return table.unpack(signal, 1, signal.n)
      end
    end
  until signal.n == 0