feat(plugin): add promise plugin cleanup

This commit is contained in:
Dax Raad 2026-07-10 11:21:43 -04:00
commit c867e66ab5
6 changed files with 74 additions and 15 deletions

View file

@ -148,7 +148,25 @@ export default Plugin.define({
```
`setup` runs each time the plugin is activated. Register long-lived behavior
during setup; do not wait there on an infinite event stream.
during setup; do not wait there on an infinite event stream. It may return a
synchronous or asynchronous cleanup function. OpenCode awaits that cleanup
when the plugin is disabled, reloaded, or shut down:
```ts
setup: async (ctx) => {
const controller = new AbortController()
const task = synchronize(ctx, controller.signal)
return async () => {
controller.abort()
await task
}
}
```
Hook registrations are released automatically with the same plugin scope. Use
the returned cleanup for resources the plugin owns, such as timers, watchers,
connections, and background tasks.
### Context
@ -216,7 +234,8 @@ export default Plugin.define({
}
await refresh()
setInterval(() => void refresh().catch(console.error), 60_000)
const timer = setInterval(() => void refresh().catch(console.error), 60_000)
return () => clearInterval(timer)
},
})
```