Sha256: 3d812f1358a791e1d07c2382cfefaeb54621ff57c95541921c78484ca7583e45

Contents?: true

Size: 808 Bytes

Versions: 1

Compression:

Stored size: 808 Bytes

Contents

module MotionBindable::Strategies
  class Proc < ::MotionBindable::Strategy
    WATCH_TICK = 0.2

    def bound_value
      bound.call
    end

    def object_value
      attribute
    end

    def on_bound_change(new = nil)
      self.attribute = new || bound.call
    end

    def unbind
      @watching = false
      super
    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

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
motion_bindable-0.3.0 lib/strategies/proc.rb