Sha256: ead21ec4f1c646d7b71862b974a33e8310cdcb3fce934ad97034641fa6df3f0b
Contents?: true
Size: 1.59 KB
Versions: 19
Compression:
Stored size: 1.59 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) raise ArgumentError, "parent must respond_to?(:unregister)" unless parent.respond_to?(:unregister) @parent = parent @callable = block @mutex = Monitor.new end def unregistered? @parent.nil? end # calls unregister on parent, then sets parent to nil def unregister obj = nil synchronize do return false unless @parent obj, @parent = @parent, nil end obj.unregister(self) end # an alias for unregister def unsubscribe unregister end # @private def call(*args) callable.call(*args) end # @private def reopen_after_fork! @mutex = Monitor.new end private def synchronize @mutex.synchronize { yield } end end end end
Version data entries
19 entries across 19 versions & 1 rubygems