Sha256: 426481bc67115de30e83c0c9865b4716fe82faa1babe8ba10a1e6223e25629c6

Contents?: true

Size: 665 Bytes

Versions: 1

Compression:

Stored size: 665 Bytes

Contents

module Space
  module Events
    class Sources
      attr_reader :events, :sources

      def initialize(events)
        @events  = events
        @sources = []
      end

      def registered
        register(Thread.current.object_id)
        yield.tap do
          unregister(Thread.current.object_id)
        end
      end

      def register(source)
        Thread.exclusive do
          events.notify(:start) if sources.empty?
          sources << source
        end
      end

      def unregister(source)
        Thread.exclusive do
          sources.delete(source)
          events.notify(:finish) if sources.empty?
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
space-0.0.6 lib/space/events/sources.rb