Skip to content

Commit

Permalink
return Promise
Browse files Browse the repository at this point in the history
  • Loading branch information
luozhiya committed Feb 7, 2025
1 parent ab68367 commit ac634b0
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 29 deletions.
34 changes: 6 additions & 28 deletions lua/fittencode/async_hash/engines/purelua/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -56,35 +56,13 @@ function M.hash(algorithm, data, options)
(type(data) == 'string' and vim.fn.filereadable(data) == 1)

if is_file then
return Promise.new(function(resolve, reject)
local hasher = M.create_hasher(algorithm)
local chunk_size = 4096
local path = data

FS.read_chunked(path, chunk_size, function(chunk)
hasher:update(chunk)
end):forward(function() hasher:finalize() end)

-- local function _read_next(offset)
-- vim.uv.fs_read(fd, chunk_size, offset, function(err, chunk)
-- if err then
-- vim.uv.fs_close(fd)
-- return reject(err)
-- end
local hasher = M.create_hasher(algorithm)
local chunk_size = 4096
local path = data

-- if chunk and #chunk > 0 then
-- hasher:update(chunk)
-- _read_next(offset + #chunk)
-- else
-- vim.uv.fs_close(fd, function(close_err)
-- if close_err then return reject(close_err) end
-- resolve(hasher:finalize())
-- end)
-- end
-- end)
-- end
-- _read_next(0)
end)
return FS.read_chunked(path, chunk_size, function(chunk)
hasher:update(chunk)
end):forward(function() return hasher:finalize() end)
else
local hasher = M.create_hasher(algorithm)
hasher:update(data)
Expand Down
3 changes: 2 additions & 1 deletion lua/fittencode/uv/fs.lua
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ uv_fs.read_file('test.txt')

local _Promise = require('fittencode.uv._promise')
local Promise = require('fittencode.concurrency.promise')
local Fn = require('fittencode.fn')

-- local fs_functions = {
-- 'fs_close', 'fs_open', 'fs_read', 'fs_write', 'fs_unlink', 'fs_mkdir',
Expand Down Expand Up @@ -113,7 +114,7 @@ function M.read_chunked(path, chunk_size, on_chunk)
:forward(function(packed)
local chunk = packed[1]
if chunk and #chunk > 0 then
on_chunk(chunk)
Fn.check_call(on_chunk, chunk)
return _read_next_chunk()
else
return M.close(fd)
Expand Down

0 comments on commit ac634b0

Please sign in to comment.