Skip to content
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

Introduces a range of and_then and after function that will handle callbacks #68

Merged
merged 1 commit into from
Feb 9, 2021

Conversation

Conni2461
Copy link
Collaborator

@Conni2461 Conni2461 commented Feb 9, 2021

Builds on top of your previous work for and_then. We might also wanna change chain i my opinion it has the even better interface to do that.

None of these additions are necessary but after i saw @tami5 code it might be helpful. tami is currently in the on_exit callback hell which makes maintaining his work a nightmare, no offense tami :). This might help writing jobs that go from job to job easier.
Example at the end. For me that is easier to read and i actually know when which job does what and what the conditions are for that case to happen. Does this make sense? We do not need to integrate it :)
I only want that sync returns the result code. Thats the only thing that interests me tbh 🤣 Saves me one line in telescope.

Adds:

  • sync will now return code as second return value
  • and_then(job): will always run job
  • and_then_wrap(job): will always run job (schedule_wrapped)
  • and_then_on_success(job): will only run job if code == 0
  • and_then_on_success_wrap(job): will only run job if code == 0 (schedule_wrapped)
  • and_then_on_failure(job): will only run job if code ~= 0
  • and_then_on_failure_wrap(job): will only run job if code ~= 0 (schedule_wrapped)
  • after(fn): will always run function
  • after_success(fn): will only run function if code == 0
  • after_failure(fn): will only run function if code ~= 0

Example:

local gen_j1 = function()
  -- only generate the job outline no callbacks
end
-- ... gen_j2 -> gen_j4 exist

local run = function()
  local j1 = gen_j1()
  local j2 = gen_j2()
  local j3 = gen_j3()
  
  j1:after_failure(function(j, code) log.trace(j:result, code) end)
  j1:and_then_on_success(j2)
  j2:after_failure(function() ... end)
  j2:and_then_on_success(j3)
  -- etc 
  j3:start()
end

…e callbacks

Adds:
  - sync will now return code as second return value

  - and_then(job): will always run job
  - and_then_wrap(job): will always run job (schedule_wrapped)
  - and_then_on_success(job): will only run job if code == 0
  - and_then_on_success_wrap(job): will only run job if code == 0
    (schedule_wrapped)
  - and_then_on_failure(job): will only run job if code ~= 0
  - and_then_on_failure_wrap(job): will only run job if code ~= 0
    (schedule_wrapped)

  - after(fn): will always run function
  - after_success(fn): will only run function if code == 0
  - after_failure(fn): will only run function if code ~= 0
@kkharji
Copy link
Contributor

kkharji commented Feb 9, 2021

Oh yah this soooooooo useful. no more callbacks

@tjdevries tjdevries merged commit e53cbe1 into nvim-lua:master Feb 9, 2021
@tjdevries
Copy link
Member

Cool 👍

@Conni2461 Conni2461 deleted the more_job_work branch February 9, 2021 19:45
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.

3 participants