File size: 1,336 Bytes
7107f0b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package aria2

import (
	"github.com/alist-org/alist/v3/pkg/aria2/rpc"
	"github.com/alist-org/alist/v3/pkg/generic_sync"
)

const (
	Downloading = iota
	Paused
	Stopped
	Completed
	Errored
)

type Notify struct {
	Signals generic_sync.MapOf[string, chan int]
}

func NewNotify() *Notify {
	return &Notify{Signals: generic_sync.MapOf[string, chan int]{}}
}

func (n *Notify) OnDownloadStart(events []rpc.Event) {
	for _, e := range events {
		if signal, ok := n.Signals.Load(e.Gid); ok {
			signal <- Downloading
		}
	}
}

func (n *Notify) OnDownloadPause(events []rpc.Event) {
	for _, e := range events {
		if signal, ok := n.Signals.Load(e.Gid); ok {
			signal <- Paused
		}
	}
}

func (n *Notify) OnDownloadStop(events []rpc.Event) {
	for _, e := range events {
		if signal, ok := n.Signals.Load(e.Gid); ok {
			signal <- Stopped
		}
	}
}

func (n *Notify) OnDownloadComplete(events []rpc.Event) {
	for _, e := range events {
		if signal, ok := n.Signals.Load(e.Gid); ok {
			signal <- Completed
		}
	}
}

func (n *Notify) OnDownloadError(events []rpc.Event) {
	for _, e := range events {
		if signal, ok := n.Signals.Load(e.Gid); ok {
			signal <- Errored
		}
	}
}

func (n *Notify) OnBtDownloadComplete(events []rpc.Event) {
	for _, e := range events {
		if signal, ok := n.Signals.Load(e.Gid); ok {
			signal <- Completed
		}
	}
}