Sha256: 427263ddf8fabf33616a5d7d921690126d7e994fda5784720dac43bfeaca7c43

Contents?: true

Size: 916 Bytes

Versions: 2

Compression:

Stored size: 916 Bytes

Contents

require "async"
require "async/semaphore"
require "timeout"

module Plumbing
  module Actor
    class Async
      attr_reader :target

      def initialize target
        @target = target
        @queue = []
        @semaphore = ::Async::Semaphore.new(1)
      end

      # Send the message to the target and wrap the result
      def send_message(message_name, *, **, &)
        task = @semaphore.async do
          @target.send(message_name, *, **, &)
        end
        Result.new(task)
      end

      def safely(&)
        send_message(:perform_safely, &)
        nil
      end

      def within_actor? = true

      def stop = nil

      Result = Data.define(:task) do
        def value
          Timeout.timeout(Plumbing::Actor.timeout) do
            task.wait
          end
        end
      end
      private_constant :Result
    end

    def self.timeout
      Plumbing.config.timeout
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
standard-procedure-plumbing-0.4.3 lib/plumbing/actor/async.rb
standard-procedure-plumbing-0.4.2 lib/plumbing/actor/async.rb