• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

ably / ably-ruby / 4287920241

pending completion
4287920241

push

github

Quintin Willison
Add features workflow.

3630 of 5199 relevant lines covered (69.82%)

322.51 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

94.12
/lib/ably/modules/async_wrapper.rb
1
require 'eventmachine'
10✔
2

3
module Ably::Modules
10✔
4
  # Provides methods to convert synchronous operations into async operations through the use of
5
  # {http://www.rubydoc.info/github/eventmachine/eventmachine/EventMachine#defer-class_method EventMachine#defer}.
6
  # The async_wrap method can only be called from within an EventMachine reactor, and must be thread safe.
7
  #
8
  # @note using this AsyncWrapper should only be used for methods that are used less frequently and typically
9
  #       not run with levels of concurrency due to the limited number of threads available to EventMachine by default.
10
  #       This module requires that the method #logger is defined.
11
  #
12
  # @example
13
  #   class BlockingOperation
14
  #     include Aby::Modules::AsyncWrapper
15
  #
16
  #     def operation(&success_callback)
17
  #       async_wrap(success_callback) do
18
  #         sleep 1
19
  #         'slept'
20
  #       end
21
  #     end
22
  #   end
23
  #
24
  #   blocking_object = BlockingOperation.new
25
  #   deferrable = blocking_object.operation do |result|
26
  #     puts "Done with result: #{result}"
27
  #   end
28
  #   puts "Starting"
29
  #
30
  #   # => 'Starting'
31
  #   # => 'Done with result: slept'
32
  #
33
  module AsyncWrapper
10✔
34
    private
10✔
35

36
    # Will yield the provided block in a new thread and return an {Ably::Util::SafeDeferrable}
37
    #
38
    # @yield [Object] operation block that is run in a thread
39
    #
40
    # @return [Ably::Util::SafeDeferrable]
41
    #
42
    def async_wrap(success_callback = nil, custom_error_handling = nil)
10✔
43
      raise ArgumentError, 'Block required' unless block_given?
150✔
44

45
      Ably::Util::SafeDeferrable.new(logger).tap do |deferrable|
150✔
46
        deferrable.callback(&success_callback) if success_callback
150✔
47

48
        operation_with_exception_handling = lambda do
150✔
49
          begin
50
            yield
147✔
51
          rescue StandardError => err
52
            if custom_error_handling
25✔
53
              custom_error_handling.call err, deferrable
×
54
            else
55
              logger.error { "An exception in an AsyncWrapper block was caught. #{err.class}: #{err.message}\n#{err.backtrace.join("\n")}" }
25✔
56
              deferrable.fail err
25✔
57
            end
58
          end
59
        end
60

61
        complete_callback = lambda do |result|
150✔
62
          deferrable.succeed result
141✔
63
        end
64

65
        EventMachine.defer operation_with_exception_handling, complete_callback
150✔
66
      end
67
    end
68
  end
69
end
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc