<commit_before>local ev = require'ev'

local tinsert = table.insert
local tremove = table.remove

local create_next_tick = function(loop)
  if ev.Idle then
    local on_idle
    local idle_io = ev.Idle.new(
      function(loop,idle_io)
        idle_io:stop(loop)
        on_idle()
      end)
    return function(f)
      on_idle = f
      idle_io:start(loop)
    end
  else
    local eps = 2^-40
    local once
    local on_timeout
    local timer_io = ev.Timer.new(
      function(loop,timer_io)
        once = true
        timer_io:stop(loop)
        on_timeout()
      end,eps)
    return function(f)
      if once then
        timer_io:again(loop)
      else
        timer_io:start(loop)
      end
    end
  end
end

local new = function()
  local self = {}
  local listeners = {}
  local max_listeners = 10
  
  self.add_listener = function(_,event,listener)
    listeners[event] = listeners[event] or {}
    if #listeners[event] > max_listeners then
      error('max_listeners limit reached for event '..event)
    end
    tinsert(listeners[event],listener)
    return self
  end
  
  self.on = self.add_listener
  
  self.remove_listener = function(_,event,oldlistener)
    if listeners[event] then
      for i,listener in ipairs(listeners[event]) do
        if listener == oldlistener then
          tremove(listeners[event],i)<commit_msg>fix next_tick with timer implementation<commit_after>local ev = require'ev'

local tinsert = table.insert
local tremove = table.remove

local create_next_tick = function(loop)
  if ev.Idle then
    local on_idle
    local idle_io = ev.Idle.new(
      function(loop,idle_io)
        idle_io:stop(loop)
        on_idle()
      end)
    return function(f)
      on_idle = f
      idle_io:start(loop)
    end
  else
    local eps = 2^-40
    local once
    local on_timeout
    local timer_io = ev.Timer.new(
      function(loop,timer_io)
        once = true
        timer_io:stop(loop)
        on_timeout()
      end,eps,eps)
    return function(f)
      on_timeout = f
      if once then
        timer_io:again(loop)
      else
        timer_io:start(loop)
      end
    end
  end
end

local new = function()
  local self = {}
  local listeners = {}
  local max_listeners = 10
  
  self.add_listener = function(_,event,listener)
    listeners[event] = listeners[event] or {}
    if #listeners[event] > max_listeners then
      error('max_listeners limit reached for event '..event)
    end
    tinsert(listeners[event],listener)
    return self
  end
  
  self.on = self.add_listener
  
  self.remove_listener = function(_,event,oldlistener)
    if listeners[event] then
      for i,listener in ipairs(listeners[event]) do
        if listener == oldlistener then
          tremove(listeners[event],i)
{'fraction_non_alphanumeric': 0.08816029143897997, 'fraction_numerical': 0.0036429872495446266, 'mean_word_length': 2.5755208333333335, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, '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': '570394', 'n_tokens_mistral': 892, 'n_tokens_neox': 896, 'n_words': 217}