Sha256: 169ac5390965c6f59253017480c26dd64fa28d2e48f4f70ec850bb18adcf411e

Contents?: true

Size: 1.1 KB

Versions: 1

Compression:

Stored size: 1.1 KB

Contents

require "thread"
require "thwait"

module Shamu
  module Events
    module InMemory

      # An asynchronous version of {Service}. Event subscribers should be able
      # to handle events coming in on a separate thread.
      class AsyncService < InMemory::Service

        def initialize
          ObjectSpace.define_finalizer self do
            threads = mutex.synchronize do
              channels.map do |_, state|
                state[:queue].close
                state[:thread]
              end
            end

            ThreadsWait.all_waits( *threads )
          end

          super
        end

        # (see Service#dispatch)
        def dispatch
          # No-op since messages are immediately dispatched on background threads.
        end

        private

          def create_channel( _ )
            state = super
            state[:thread] = channel_thread( state )
            state[:queue]  = Queue.new
            state
          end

          def channel_thread( state )
            Thread.new do
              dispatch_channel( state )
            end
          end

      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
shamu-0.0.24 lib/shamu/events/in_memory/async_service.rb