lib/strategies/proc.rb in motion_bindable-0.2.5 vs lib/strategies/proc.rb in motion_bindable-0.3.0
- old
+ new
@@ -1,14 +1,14 @@
module MotionBindable::Strategies
-
class Proc < ::MotionBindable::Strategy
+ WATCH_TICK = 0.2
- def refresh_bound
+ def bound_value
bound.call
end
- def refresh_object
+ def object_value
attribute
end
def on_bound_change(new = nil)
self.attribute = new || bound.call
@@ -17,8 +17,30 @@
def unbind
@watching = false
super
end
- end
+ def start_observing
+ @watching = true
+ watch
+ end
+ private
+
+ def watch
+ dispatcher.async do
+ if @watching
+ new = bound_value
+ on_bound_change(new) if new != @old_bound_value
+ @old_bound_value = nil
+ dispatcher.after(WATCH_TICK) { watch }
+ end
+ end
+ end
+
+ def dispatcher
+ @dispatcher ||= begin
+ Dispatch::Queue.concurrent 'org.motion.bindable'
+ end
+ end
+ end
end