Project

General

Profile

« Previous | Next » 

Revision 5a8ebc1c

Added by Marc Dequènes about 14 years ago

  • ID 5a8ebc1cd0570a4e9d86fe9a729c41c7f5f1b175

[evol] DSL: partial implementation for wait_notification with callback (unfinished, but kept as example)

View differences:

lib/cyborghood/cyborg/botnet_dsl.rb
end
# TODO: implement settable timeout
def wait_notification(name, criterias = {})
def wait_notification(name, criterias = {}, &callback)
name = @notification_name if name == :thread
@in_notifications[name] ||= []
@in_notifications[name] << criterias
@in_notifications[name] << {
:criterias => criterias,
:callback => callback
}
@in_notifications_awaiting += 1
end
......
@in_notifications.each_pair do |name, list|
subcription_id = @conversation.bot.get_channel(name).subscribe do |msg|
old_notifications_received = @in_notifications_received
list.each do |criterias|
if _notification_criterias_match(msg, criterias)
@final_reply[:notifications][name] ||= []
@final_reply[:notifications][name] << msg
@in_notifications_received += 1
# TODO: remove events from the list when condition reached
list.each do |data|
if _notification_criterias_match(msg, data[:criterias])
stop_listening = true
if data[:callback]
stop_listening = data[:callback].call(msg)
else
# process notification later, in the on_* blocks
@final_reply[:notifications][name] ||= []
@final_reply[:notifications][name] << msg
end
@in_notifications_received += 1 if stop_listening
# do not break, the same message may satisfy multiple criterias
end
end
if @final_reply[:notifications][name].size == @in_notifications[name].size
# TODO: condition is wrong in case of custom callback
if @final_reply[:notifications][name].size >= @in_notifications[name].size
@conversation.bot.get_channel(name).unsubscribe(subcription_id)
end
_check_finished(conv_thread) if @in_notifications_received > old_notifications_received

Also available in: Unified diff