<commit_before>module EventMachine
  module CometIO
    class Client
      include EventEmitter
      attr_reader :url, :session
      attr_accessor :timeout

      def initialize(url)
        raise ArgumentError, "invalid URL (#{url})" unless url.kind_of? String and url =~ /^https?:\/\/.+/
        @url = url
        @session = nil
        @running = false
        @timeout = 120
      end

      def push(type, data)
        that = self
        post_data = {:session => @session, :events => [:type => type, :data => data]}
        http = EM::HttpRequest.new(@url, :connect_timeout => 10).
          post(:body => post_data)
        http.callback do |res|
        end
        http.errback do |err|
          self.emit :error, "CometIO push error"
        end
      end

      def connect
        return self if @running
        self.on :__session_id do |session|
          @session = session
          self.emit :connect, @session
        end
        @running = true
        get
        return self
      end

      def close
        @running = false<commit_msg>use queue on push  #3<commit_after>module EventMachine
  module CometIO
    class Client
      include EventEmitter
      attr_reader :url, :session
      attr_accessor :timeout

      def initialize(url)
        raise ArgumentError, "invalid URL (#{url})" unless url.kind_of? String and url =~ /^https?:\/\/.+/
        @url = url
        @session = nil
        @running = false
        @timeout = 120
        @post_queue = []
        EM::add_periodic_timer 1 do
          flush
        end
      end

      private
      def flush
        return if !@running or @post_queue.size < 1
        post_data = {:session => @session, :events => @post_queue}
        that = self
        http = EM::HttpRequest.new(@url, :connect_timeout => 10).
          post(:body => post_data)
        @post_queue = []
        http.callback do |res|
        end
        http.errback do |err|
          self.emit :error, "CometIO push error"
        end
      end

      public
      def push(type, data)
        if !@running or !@session
          emit :error, "CometIO not connected"
          return
        end
        @post_queue.push :type => type, :data => data
      end

      def connect
        return self if @running
        self.on :__session_id do |session|
          @session = session
          self.emit :connect, @session
        end
        @running = true
        get
        return self
      end

      def close
        @running = false
{'fraction_non_alphanumeric': 0.09556177528988405, 'fraction_numerical': 0.005197920831667333, 'mean_word_length': 1.6702241195304162, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3604934', 'n_tokens_mistral': 725, 'n_tokens_neox': 700, 'n_words': 222}