ref: refs/heads/backgroud_patches_bnc550934 commit c6072e33538883c90e5284c067ddfe7195eecd33 Author: Ladislav Slezak <lslezak@novell.com> Date: Thu Dec 17 09:41:12 2009 +0100 BackgroundStatus - use Observable design pattern for reporting changes in progress --- plugins/patches/scripts/list_patches.rb | 25 ++++++++++++++++++++----- webservice/lib/background_status.rb | 21 ++++++++++----------- 2 files changed, 30 insertions(+), 16 deletions(-) diff --git a/plugins/patches/scripts/list_patches.rb b/plugins/patches/scripts/list_patches.rb index 25d0daf..e1d692c 100644 --- a/plugins/patches/scripts/list_patches.rb +++ b/plugins/patches/scripts/list_patches.rb @@ -4,14 +4,29 @@ # ruby-dbus is NOT thread safe and therefore patches cannot be safely read # in a separate thread, workaround is to use a separate process (this script) -bs = BackgroundStatus.new do - # print the progress in XML format - puts bs.to_xml +# this an observer class which prints changes in the progress +# in XML format on stdout +class ProgressPrinter + def initialize(prog) + # watch changes in a progress status object + prog.add_observer(self) + end - # print it immediately, flush the output buffer - $stdout.flush + # this is the callback method + def update(progress) + # print the progress in XML format + puts progress.to_xml + + # print it immediately, flush the output buffer + $stdout.flush + end end +bs = BackgroundStatus.new + +# register a progress printer for the progress object +ProgressPrinter.new(bs) + what = ARGV[0] || :available patches = Patch.do_find(what, bs) diff --git a/webservice/lib/background_status.rb b/webservice/lib/background_status.rb index 7c666cb..12afbc6 100644 --- a/webservice/lib/background_status.rb +++ b/webservice/lib/background_status.rb @@ -2,36 +2,41 @@ class BackgroundStatus + # use Observable design pattern for reporting changes + include Observable + attr_reader :status, :progress, :subprogress - def initialize(stat = 'unknown', progress = 0, subprogress = -1, &block) + def initialize(stat = 'unknown', progress = 0, subprogress = -1) @status = stat @progress = progress @subprogress = subprogress - @callback = block_given? ? block : nil end def status=(stat) if @status != stat + changed @status = stat - trigger_callback + notify_observers self end end def progress=(p) if @progress != p + changed @progress = p - trigger_callback + notify_observers self end end # returns -1 if there is no subprogress def subprogress=(s) if @subprogress != s + changed @subprogress = s - trigger_callback + notify_observers self end end @@ -46,10 +51,4 @@ class BackgroundStatus end end - private - - def trigger_callback - @callback.try(:call) - end - end -- To unsubscribe, e-mail: yast-commit+unsubscribe@opensuse.org For additional commands, e-mail: yast-commit+help@opensuse.org