nvim-neotest / nvim-nio

A library for asynchronous IO in Neovim
MIT License
305 stars 9 forks source link

feat(uv): run sync version of luv when outside of tasks #4

Closed pysan3 closed 10 months ago

pysan3 commented 10 months ago

After this commit, functions defined in nio.uv do not work when called outside of a task (async environment).

Although I do agree with this design decision (which also matches the type annotations), I couldn't live without them being callable in sync from a practical standpoint.

With this PR, the function defined in add wraps the call to tasks.wrap and branches out to call sync version of vim.loop functions when not in a task, and manipulate the result to match that of wrapped calls (string?: err_msg, ...?: result).

If you @rcarriga don't agree with this approach, feel free to reject this PR tho.

I also added a few fixes.

rcarriga commented 10 months ago

This would defeat the purpose of making it strict. I don't want these functions to work without being called within a task so I'll close this. I have however, exposed the current_task function which you can use to make a small wrapper to expand as you want

local nio = require("nio")

local uv = setmetatable({}, {
  __index = function(_, key)
    if nio.current_task() then
      return nio.uv[key]
    end
    return vim.loop[key]
  end,
})

vim.print({ uv.fs_open("test.lua", "r", 438) })
pysan3 commented 10 months ago

Alright. That's a reasonable solution. Thanks a lot.

Btw don't forget to delete add("shutdown", 2) that is defined twice.