Skip to content

Commit

Permalink
feat: support extension onDone event (#728)
Browse files Browse the repository at this point in the history
  • Loading branch information
monkeyWie authored Sep 7, 2024
1 parent 0c17093 commit 5124413
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 6 deletions.
1 change: 1 addition & 0 deletions pkg/download/downloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -761,6 +761,7 @@ func (d *Downloader) watch(task *Task) {
d.emit(EventKeyDone, task)
d.emit(EventKeyFinally, task, err)
d.notifyRunning()
d.triggerOnDone(task)

if e, ok := task.Meta.Opts.Extra.(*http.OptsExtra); ok {
downloadFilePath := task.Meta.SingleFilepath()
Expand Down
23 changes: 19 additions & 4 deletions pkg/download/extension.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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{
Expand Down Expand Up @@ -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"`
Expand Down Expand Up @@ -687,6 +698,10 @@ type OnErrorContext struct {
Error error `json:"error"`
}

type OnDoneContext struct {
Task *Task `json:"task"`
}

type ExtensionTask struct {
*Task

Expand Down
41 changes: 39 additions & 2 deletions pkg/download/extension_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -247,8 +247,45 @@ func TestDownloader_Extension_OnError(t *testing.T) {
}
// extension on error modify url and continue download
task := downloader.GetTask(id)
if task.Meta.Req.URL != "https://github.com" {
t.Fatalf("except url: https://github.com, actual: %s", task.Meta.Req.URL)
if task.Status != base.DownloadStatusDone {
t.Fatalf("except status is done, actual: %s", task.Status)
}
})
}

func TestDownloader_Extension_OnDone(t *testing.T) {
setupDownloader(func(downloader *Downloader) {
if _, err := downloader.InstallExtensionByFolder("./testdata/extensions/on_done", false); err != nil {
t.Fatal(err)
}
errCh := make(chan error, 1)
downloader.Listener(func(event *Event) {
if event.Key == EventKeyFinally {
errCh <- event.Err
}
})
id, err := downloader.CreateDirect(&base.Request{
URL: "https://github.com",
}, nil)
if err != nil {
t.Fatal(err)
}
select {
case err = <-errCh:
break
case <-time.After(time.Second * 10):
err = errors.New("timeout")
}
// wait for script execution
time.Sleep(time.Millisecond * 3000)

if err != nil {
panic("extension on done download error: " + err.Error())
}
// extension on error modify url and continue download
task := downloader.GetTask(id)
if task.Meta.Req.Labels["modified"] != "true" {
t.Fatalf("except label: modified=true, actual: %s", task.Meta.Req.Labels["modified"])
}
if task.Status != base.DownloadStatusDone {
t.Fatalf("except status is done, actual: %s", task.Status)
Expand Down
5 changes: 5 additions & 0 deletions pkg/download/testdata/extensions/on_done/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
gopeed.events.onDone(async function (ctx) {
gopeed.logger.info("url", ctx.task.meta.req.url);
ctx.task.meta.req.labels['modified'] = 'true';
});

16 changes: 16 additions & 0 deletions pkg/download/testdata/extensions/on_done/manifest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"name": "on-done",
"title": "gopeed extension on done event test",
"version": "0.0.1",
"scripts": [
{
"event": "onDone",
"match": {
"urls": [
"*://github.com/*"
]
},
"entry": "index.js"
}
]
}

0 comments on commit 5124413

Please sign in to comment.