From eb973a1942c68aa7e82aa0fb8bc37a32a45200e6 Mon Sep 17 00:00:00 2001 From: xing Date: Thu, 28 Jul 2022 16:26:04 +0800 Subject: [PATCH] =?UTF-8?q?=E5=B0=8F=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.go | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/main.go b/main.go index 5406e1c..8f8abf6 100644 --- a/main.go +++ b/main.go @@ -99,16 +99,16 @@ func isContain[T comparable](i T, arr []T) bool { return false } -func setMap[T mapT](obj *mapXS[T], key string, v T) { - obj.Lock() - (*obj.mapX)[key] = v - obj.Unlock() +func (r *mapXS[T]) set(k string, v T) { + r.Lock() + (*r.mapX)[k] = v + r.Unlock() } -func delMap[T mapT](obj *mapXS[T], key string) { - obj.Lock() - delete(*obj.mapX, key) - obj.Unlock() +func (r *mapXS[T]) del(k string) { + r.Lock() + delete(*r.mapX, k) + r.Unlock() } type mapT interface { @@ -185,8 +185,8 @@ func (f *fetchHandler) receiveMsg() { switch r.msg.Action { case "search": if t, ok := r.msg.Data.(*setting); ok { - setMap[string](&f.keyword, r.conn, t.Keyword) - setMap[[]string](&f.searchSource, r.conn, t.SearchSource) + f.keyword.set(r.conn, t.Keyword) + f.searchSource.set(r.conn, t.SearchSource) f.handle(r.conn) (*f.reloadCron.mapX)[r.conn] <- t.TimeStep } @@ -324,7 +324,7 @@ func (f *fetchHandler) parseAjax(response *http.Response, source newsource.Sourc } if _, ok := (*f.hadFetchedMap.mapX)[k]; !ok { f.hadFetchData = append(f.hadFetchData, fetchData) - setMap(&f.hadFetchedMap, k, 1) + f.hadFetchedMap.set(k, 1) newF = append(newF, newFetch[i]) } } @@ -364,7 +364,7 @@ func (f *fetchHandler) parsesDom(html *http.Response, conn string, source newsou k := conn + "_" + fetchData.Url + "_" + fetchData.Title if _, ok := (*f.hadFetchedMap.mapX)[k]; !ok { f.hadFetchData = append(f.hadFetchData, fetchData) - setMap(&f.hadFetchedMap, k, 1) + f.hadFetchedMap.set(k, 1) newFetch = append(newFetch, fetchData) } }) @@ -403,7 +403,7 @@ func (f *fetchHandler) cronFetch(conn string, c chan int) { } t := time.NewTicker(step) if _, ok := (*f.cronTime.mapX)[conn]; !ok { - setMap(&f.reloadCron, conn, make(chan int)) + f.reloadCron.set(conn, make(chan int)) } defer t.Stop() for { @@ -411,7 +411,7 @@ func (f *fetchHandler) cronFetch(conn string, c chan int) { case <-t.C: f.handle(conn) case tt := <-(*f.reloadCron.mapX)[conn]: - setMap(&f.cronTime, conn, time.Duration(tt)*time.Second) + f.cronTime.set(conn, time.Duration(tt)*time.Second) go f.cronFetch(conn, c) return case <-c: @@ -471,7 +471,7 @@ func main() { }) remote := conn.RemoteAddr().String() if _, ok := (*h.connMap.mapX)[remote]; !ok { - setMap(&h.connMap, remote, conn) + h.connMap.set(remote, conn) } cc := make(chan int) go h.cronFetch(remote, cc) @@ -486,7 +486,7 @@ func main() { err := conn.ReadJSON(&msg.msg) if err != nil { if _, ok := (*h.connMap.mapX)[remote]; ok && !websocket.IsUnexpectedCloseError(err, websocket.CloseGoingAway) { - delMap(&h.connMap, remote) + h.connMap.del(remote) cc <- 1 return }