tap icon indicating copy to clipboard operation
tap copied to clipboard

Middleware Hooks

Open thinkerbot opened this issue 15 years ago • 0 comments

It would be helpful if apps allowed middleware to have before/after hooks for a run:

def run
  synchronize do
    return self unless state == State::READY
    @state = State::RUN
  end

  middleware.each do |middleware|
    if middleware.respond_to?(:before_run)
      middleware.before_run
    end
  end

  begin
    while state == State::RUN
      break unless entry = queue.deq
      dispatch(*entry)
    end
  rescue(TerminateError)
    # gracefully fail for termination errors
    queue.unshift(*entry)
  ensure
    synchronize { @state = State::READY }
  end

  middleware.each do |middleware|
    if middleware.respond_to?(:after_run)
      middleware.after_run
    end
  end

  self
end

thinkerbot avatar Jun 20 '09 14:06 thinkerbot