<commit_before>local table_remove, tostring, unpack =
      table.remove, tostring, unpack
local object = require "moonhowl.object"
local signal = require "moonhowl.signal"
local ui = require "moonhowl.ui"

local cb_handler = object:extend()

function cb_handler:_init(http)
    self.http = http
    self.__call = self.add
    self._update = self:bind(self.update)
end

function cb_handler:update()
    self.http:update()
    for i = #self, 1, -1 do
        local fut, callback = unpack(self[i])
        local ready, res, err = fut:peek(true)
        if ready then
            table_remove(self, i)
            if res == nil then
                self.on_error(err)
            else
                callback(res, err)
            end
        end
    end
    return #self > 0
end

function cb_handler:add(fut, cb)
    local n = #self
    self[n + 1] = { fut, cb }
    if n == 0 then
        return ui.timer.add(200, self._update)
    end
end

function cb_handler.on_error(err)
    signal.emit("ui_message", tostring(err), true)
end

return cb_handler<commit_msg>cb_handler: add support for table callbacks with extra ".error" field<commit_after>local table_remove, tostring, type, unpack =
      table.remove, tostring, type, unpack
local object = require "moonhowl.object"
local signal = require "moonhowl.signal"
local ui = require "moonhowl.ui"

local cb_handler = object:extend()

function cb_handler:_init(http)
    self.http = http
    self.__call = self.add
    self._update = self:bind(self.update)
end

function cb_handler:update()
    self.http:update()
    for i = #self, 1, -1 do
        local fut, callback = unpack(self[i])
        local ready, res, err = fut:peek(true)
        if ready then
            table_remove(self, i)
            local is_table = type(callback) == "table"
            if res ~= nil then
                (is_table and callback.ok or callback)(res, err)
            else
                (is_table and callback.error or self.on_error)(err)
            end
        end
    end
    return #self > 0
end

function cb_handler:add(fut, cb)
    local n = #self
    self[n + 1] = { fut, cb }
    if n == 0 then
        return ui.timer.add(200, self._update)
    end
end

function cb_handler.on_error(err)
    return signal.emit("ui_message", tostring(err), true)
end

return cb_handler
{'fraction_non_alphanumeric': 0.10246433203631647, 'fraction_numerical': 0.006917423259835711, 'mean_word_length': 2.4849397590361444, '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': '7362579', 'n_tokens_mistral': 758, 'n_tokens_neox': 736, 'n_words': 215}