Sha256: 59d957b1b6c1fd2d23acb4642bc27d81f2c91650504663d58f323273bb4f2343

Contents?: true

Size: 868 Bytes

Versions: 3

Compression:

Stored size: 868 Bytes

Contents

# encoding: utf-8

require 'eventmachine' unless defined? EventMachine
require 'mysql2' unless defined? Mysql2

module Mysql2
  module EM
    class Client < ::Mysql2::Client
      module Watcher
        def initialize(client, deferable)
          @client = client
          @deferable = deferable
        end

        def notify_readable
          detach
          begin
            @deferable.succeed(@client.async_result)
          rescue Exception => e
            @deferable.fail(e)
          end
        end
      end

      def query(sql, opts={})
        if ::EM.reactor_running?
          super(sql, opts.merge(:async => true))
          deferable = ::EM::DefaultDeferrable.new
          ::EM.watch(self.socket, Watcher, self, deferable).notify_readable = true
          deferable
        else
          super(sql, opts)
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
ghazel-mysql2-0.2.6.3 lib/mysql2/em.rb
ghazel-mysql2-0.2.6.2 lib/mysql2/em.rb
ghazel-mysql2-0.2.6.1 lib/mysql2/em.rb