Sha256: 61b569f19294c7ae4fa1873a20424e4fb0fefc572c8d24d422aaad42f8d5a24b

Contents?: true

Size: 1.98 KB

Versions: 4

Compression:

Stored size: 1.98 KB

Contents

module ZK
  # Basic pattern for objects that have the concept of a parent (the thing that 
  # granted this subscription), a callback, and that can unregister (so the
  # callback no longer receives events). 
  #
  # expects the 'parent' to respond_to? the 'unregister' method, and will
  # be passed the subscription instance 
  module Subscription
    class Base
      include ZK::Logging

      # the object from which we will attempt to #unregister on
      # XXX: need a better name for this
      attr_reader :parent
      
      # the user-supplied callback block, used to create a ThreadedCallback
      attr_reader :callable

      def initialize(parent, block)
        raise ArgumentError, "block must repsond_to?(:call)" unless block.respond_to?(:call)
        @parent = parent
        @callable = block
        reopen_after_fork!
      end

      def unregister
        parent.unregister(self)
      end
      alias unsubscribe unregister

      # @private
      def call(*args)
        callable.call(*args)
      end

      # @private
      def reopen_after_fork!
        @mutex = Monitor.new
      end

      protected
        def synchronize
          @mutex.synchronize { yield }
        end
    end

    module ActorStyle
      extend Concern

      included do
        alias_method_chain :unsubscribe, :threaded_callback
        alias_method_chain :callable, :threaded_callback_wrapper
        alias_method_chain :reopen_after_fork!, :threaded_refresh
      end

      def unsubscribe_with_threaded_callback
        synchronize do
          @threaded_callback.shutdown
          unsubscribe_without_threaded_callback
        end
      end

      def reopen_after_fork_with_threaded_refresh!
        reopen_after_fork_without_threaded_refresh!
        @threaded_callback = ThreadedCallback.new(@callable)
      end
    
      def callable_with_threaded_callback_wrapper(*args)
        synchronize do 
          @threaded_callback ||= ThreadedCallback.new(@callable) 
        end
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
zk-1.4.1 lib/zk/subscription.rb
zk-1.4.0 lib/zk/subscription.rb
zk-1.3.1 lib/zk/subscription.rb
zk-1.3.0 lib/zk/subscription.rb