ConradIrwin / em-imap

An event machine based IMAP client
MIT License
69 stars 31 forks source link

How to Re-IDLE ? #4

Closed gkrcode closed 12 years ago

gkrcode commented 12 years ago

Hi,

idler = client.idle

idler.listen do |response| if (response.name == "EXISTS" rescue nil) puts "Ooh, new emails!" idler.stop idler.callback do

... process new emails

end

end end.errback do |e| puts "Idler recieved an error: #{e}" end

How to re-IDLE after processing idler.callback in a right way ?

Thanks

ConradIrwin commented 12 years ago

@rewinfrey may have a better idea, but here's what I suggest.

Let's assume that your process_new_emails function is going to return a Deferrable (because you're probably going to want to do something async in there — if not, just make the function return DG::success).

Then it's just a case of saying "after you've processed the new emails, idle again"; which is (in Deferrable speak): .callback{ idle_loop(client) }.

That gives you a function (untested) that works like this:

def idle_loop(client)
  idler = client.idle

  idler.listen do |response|
    if (response.name == "EXISTS" rescue nil)
      puts "Ooh, new emails!"
      idler.stop
      idler.callback do
        process_new_emails.callback{ idle_loop(client) }
      end
    end
  end.errback do |e|
    puts "Idler recieved an error: #{e}"
  end
end

I think I should add an example like this to the README. Will wait for feedback from you and @rewinfrey first.

gkrcode commented 12 years ago

Thanks, It works :)