|
9 | 9 | - [Custom components](#custom-components) |
10 | 10 | - [Component aliases](#component-aliases) |
11 | 11 | - [Task result](#task-result) |
| 12 | +- [Task events](#task-events) |
12 | 13 | - [Customizing built-in tasks](#customizing-built-in-tasks) |
13 | 14 | - [Customizing the task appearance in the task list](#customizing-the-task-appearance-in-the-task-list) |
14 | 15 | - [Parsing output](#parsing-output) |
@@ -290,6 +291,37 @@ A note on the Task result table: there is technically no schema for it, as the o |
290 | 291 | **diagnostics**: This key is used for diagnostics. It should be a list of quickfix items (see `:help setqflist`) \ |
291 | 292 | **error**: This key will be set when there is an internal overseer error when running the task |
292 | 293 |
|
| 294 | +## Task events |
| 295 | + |
| 296 | +A lighter-weight alternative to custom components is directly subscribing to task events. Once you create a task you can call `task:subscribe("event", function() ... end)` to process the same events that get handled by components. For example, to run a function when a task completes: |
| 297 | + |
| 298 | +```lua |
| 299 | +local task = overseer.new_task({ cmd = {"echo", "hello", "world"} }) |
| 300 | +-- on_complete gets called with the same arguments as it does for components |
| 301 | +task:subscribe("on_complete", function(_task, status, result) |
| 302 | + print("Task", task.name, "finished with status", status) |
| 303 | +end) |
| 304 | +task:start() |
| 305 | +``` |
| 306 | + |
| 307 | +To unsubscribe from an event, you can either pass the same function in to `task:unsubscribe()` or you can return a truthy value from the function. |
| 308 | + |
| 309 | +```lua |
| 310 | +local task = overseer.new_task({ cmd = { "build_and_serve.sh" } }) |
| 311 | +task:subscribe("on_output_lines", function(_task, lines) |
| 312 | + for _, line in ipairs(lines) do |
| 313 | + local address = line:match("^Serving at (http.*)") |
| 314 | + if address then |
| 315 | + vim.ui.open(address) |
| 316 | + return true |
| 317 | + end |
| 318 | + end |
| 319 | +end) |
| 320 | +task:start() |
| 321 | +``` |
| 322 | + |
| 323 | +Note that when a task is serialized it cannot save the subscriptions. |
| 324 | + |
293 | 325 | ## Customizing built-in tasks |
294 | 326 |
|
295 | 327 | You may wish to customize the built-in task definitions, or tasks from another plugin. The simplest way to do this is using the [add_template_hook](reference.md#add_template_hookopts-hook) function. This allows you to run a function on the task definition (the arguments passed to [new_task](reference.md#new_taskopts)) and process it however you like. A common use case would be to add a component or modify the environment variables while in a specific project: |
|
0 commit comments