8000 Callbacks will now receive the running job as 'self' by Odie · Pull Request #9 · TravonteD/luajob · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Callbacks will now receive the running job as 'self' #9

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

Odie
Copy link
@Odie Odie commented Feb 14, 2021

Howdy!

While playing around with luajob, I found it a little strange that the callbacks do not seem to give access to the job object itself. While this is probably fine for piping output from an external process into a buffer, it makes it somewhat awkward to store & retrieve the result of the job.

For example, in the snippet in the README that uses 'git branch' to retrieve the current branch, it eventually needs to push that result into some kind of global scope via nvim_*_set_var(). It would be a lot cleaner if the result was simply stored back onto the job object itself, like so:

local luajob = require('luajob')
local git_branch = luajob:new({
  cmd = 'git branch',
  on_stdout = function(self, err, data)
    if err then
      print('error:', err)
    elseif data then
      lines = vim.fn.split(data, '\n')
      for _, line in ipairs(lines) do
        if line:find('*') then
          self.branch = (line:gsub('\n', ''))  --> store result back onto the job object
        end
      end
    end
  end,
  on_exit = function(self, err, data)
    print('job exited, branch: ', self.branch)  --> Read back the result on_exit or anywhere else where the job object is accessible
  end
})
git_branch:start()

Anyway, this little patch injects "self" into the callbacks on_stdout, on_stderr, and on_exit.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant
0