feat: support extension onDone event (#728)

This commit is contained in:
Levi authored and GitHub committed 2024-09-07 10:10:39 +08:00
1 parent 0c17093b1a
commit 51244133e0
5 files changed
+80 -6

No files matched your search

+19 -4
View File
@@ -38,7 +38,7 @@ const (
EventOnResolve ActivationEvent = "onResolve"
EventOnStart ActivationEvent = "onStart"
EventOnError ActivationEvent = "onError"
//EventOnDone ActivationEvent = "onDone"
EventOnDone ActivationEvent = "onDone"
)
func (d *Downloader) InstallExtensionByGit(url string) (*Extension, error) {
@@ -304,6 +304,17 @@ func (d *Downloader) triggerOnError(task *Task, err error) {
)
}
func (d *Downloader) triggerOnDone(task *Task) {
doTrigger(d,
EventOnDone,
task.Meta.Req,
&OnErrorContext{
Task: NewExtensionTask(d, task),
},
nil,
)
}
func doTrigger[T any](d *Downloader, event ActivationEvent, req *base.Request, ctx T, handler func(ext *Extension, gopeed *Instance, ctx T)) error {
// init extension global object
gopeed := &Instance{
@@ -611,9 +622,9 @@ func (h InstanceEvents) OnError(fn goja.Callable) {
h.register(EventOnError, fn)
}
//func (h InstanceEvents) OnDone(fn goja.Callable) {
// h.register(HookEventOnDone, fn)
//}
func (h InstanceEvents) OnDone(fn goja.Callable) {
h.register(EventOnDone, fn)
}
type ExtensionInfo struct {
Identity string `json:"identity"`
@@ -687,6 +698,10 @@ type OnErrorContext struct {
Error error `json:"error"`
}
type OnDoneContext struct {
Task *Task `json:"task"`
}
type ExtensionTask struct {
*Task