Skip to content

Commit 8ee06be

Browse files
committed
doc: add a section on subscribing to task events
1 parent 2a0d41b commit 8ee06be

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ A task runner and job management plugin for Neovim
1515
- [Custom tasks](doc/guides.md#custom-tasks)
1616
- [Actions](doc/guides.md#actions)
1717
- [Custom components](doc/guides.md#custom-components)
18+
- [Task events](doc/guides.md#task-events)
1819
- [Customizing built-in tasks](doc/guides.md#customizing-built-in-tasks)
1920
- [Customizing the task appearance in the task list](doc/guides.md#customizing-the-task-appearance-in-the-task-list)
2021
- [Parsing output](doc/guides.md#parsing-output)
@@ -178,6 +179,7 @@ If you want to define custom tasks for your project, I'd recommend starting with
178179
- [Custom components](doc/guides.md#custom-components)
179180
- [Component aliases](doc/guides.md#component-aliases)
180181
- [Task result](doc/guides.md#task-result)
182+
- [Task events](doc/guides.md#task-events)
181183
- [Customizing built-in tasks](doc/guides.md#customizing-built-in-tasks)
182184
- [Customizing the task appearance in the task list](doc/guides.md#customizing-the-task-appearance-in-the-task-list)
183185
- [Parsing output](doc/guides.md#parsing-output)

doc/guides.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
- [Custom components](#custom-components)
1010
- [Component aliases](#component-aliases)
1111
- [Task result](#task-result)
12+
- [Task events](#task-events)
1213
- [Customizing built-in tasks](#customizing-built-in-tasks)
1314
- [Customizing the task appearance in the task list](#customizing-the-task-appearance-in-the-task-list)
1415
- [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
290291
**diagnostics**: This key is used for diagnostics. It should be a list of quickfix items (see `:help setqflist`) \
291292
**error**: This key will be set when there is an internal overseer error when running the task
292293

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+
293325
## Customizing built-in tasks
294326

295327
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

Comments
 (0)