feat(plugin): add promise plugin cleanup
This commit is contained in:
parent
278c510549
commit
c867e66ab5
6 changed files with 74 additions and 15 deletions
23
packages/docs/build/plugins.mdx
vendored
23
packages/docs/build/plugins.mdx
vendored
|
|
@ -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)
|
||||
},
|
||||
})
|
||||
```
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue