diff --git a/arn/AnimeListItemAPI.go b/arn/AnimeListItemAPI.go index 35d2eb6b..09a2cbe6 100644 --- a/arn/AnimeListItemAPI.go +++ b/arn/AnimeListItemAPI.go @@ -7,6 +7,7 @@ import ( "time" "github.com/aerogo/aero" + "github.com/aerogo/aero/event" "github.com/aerogo/api" ) @@ -46,10 +47,8 @@ func (item *AnimeListItem) Edit(ctx aero.Context, key string, value reflect.Valu // Broadcast event to all users so they can reload the activity page if needed. for receiver := range StreamUsers() { - receiver.BroadcastEvent(&aero.Event{ - Name: "activity", - Data: receiver.IsFollowing(user.ID), - }) + activityEvent := event.New("activity", receiver.IsFollowing(user.ID)) + receiver.BroadcastEvent(activityEvent) } } } else if newEpisodes >= lastActivity.FromEpisode { diff --git a/arn/User.go b/arn/User.go index a53920d9..32dc195a 100644 --- a/arn/User.go +++ b/arn/User.go @@ -10,7 +10,7 @@ import ( "sync" "time" - "github.com/aerogo/aero" + "github.com/aerogo/aero/event" "github.com/aerogo/http/client" "github.com/animenotifier/ffxiv" "github.com/animenotifier/notify.moe/arn/autocorrect" @@ -61,7 +61,7 @@ type User struct { eventStreams struct { sync.Mutex - value []*aero.EventStream + value []*event.Stream } } @@ -219,10 +219,8 @@ func (user *User) SendNotification(pushNotification *PushNotification) { // } // Send an event to the user's open tabs - user.BroadcastEvent(&aero.Event{ - Name: "notificationCount", - Data: userNotifications.CountUnseen(), - }) + notificationCount := event.New("notificationCount", userNotifications.CountUnseen()) + user.BroadcastEvent(notificationCount) } // RealName returns the real name of the user. diff --git a/arn/UserEvents.go b/arn/UserEvents.go index 90cbc0a5..001ae5dd 100644 --- a/arn/UserEvents.go +++ b/arn/UserEvents.go @@ -1,14 +1,11 @@ package arn -import "github.com/aerogo/aero" - -// // EventStreams returns the user's active event streams. -// func (user *User) EventStreams() []*aero.EventStream { -// return user.eventStreams -// } +import ( + "github.com/aerogo/aero/event" +) // AddEventStream adds an event stream to the given user. -func (user *User) AddEventStream(stream *aero.EventStream) { +func (user *User) AddEventStream(stream *event.Stream) { user.eventStreams.Lock() defer user.eventStreams.Unlock() @@ -17,7 +14,7 @@ func (user *User) AddEventStream(stream *aero.EventStream) { // RemoveEventStream removes an event stream from the given user // and returns true if it was removed, otherwise false. -func (user *User) RemoveEventStream(stream *aero.EventStream) bool { +func (user *User) RemoveEventStream(stream *event.Stream) bool { user.eventStreams.Lock() defer user.eventStreams.Unlock() @@ -32,14 +29,14 @@ func (user *User) RemoveEventStream(stream *aero.EventStream) bool { } // BroadcastEvent sends the given event to all event streams for the given user. -func (user *User) BroadcastEvent(event *aero.Event) { +func (user *User) BroadcastEvent(evt *event.Event) { user.eventStreams.Lock() defer user.eventStreams.Unlock() for _, stream := range user.eventStreams.value { // Non-blocking send because we don't know if our listeners are still active. select { - case stream.Events <- event: + case stream.Events <- evt: default: } } diff --git a/arn/Utils.go b/arn/Utils.go index d05d239a..3665f08a 100644 --- a/arn/Utils.go +++ b/arn/Utils.go @@ -13,6 +13,7 @@ import ( "time" "github.com/aerogo/aero" + "github.com/aerogo/aero/event" "github.com/aerogo/mirror" "github.com/akyoto/color" "github.com/animenotifier/kitsu" @@ -201,9 +202,9 @@ func DateToSeason(date time.Time) string { } // BroadcastEvent sends the given event to the event streams of all users. -func BroadcastEvent(event *aero.Event) { +func BroadcastEvent(evt *event.Event) { for user := range StreamUsers() { - user.BroadcastEvent(event) + user.BroadcastEvent(evt) } } diff --git a/go.mod b/go.mod index fc6160da..7d2629a2 100644 --- a/go.mod +++ b/go.mod @@ -6,7 +6,7 @@ require ( cloud.google.com/go v0.97.0 // indirect github.com/PuerkitoBio/goquery v1.8.0 // indirect github.com/StackExchange/wmi v1.2.1 // indirect - github.com/aerogo/aero v1.3.58 + github.com/aerogo/aero v1.3.59 github.com/aerogo/api v0.2.3 github.com/aerogo/crawler v0.2.5 github.com/aerogo/flow v0.1.5 diff --git a/go.sum b/go.sum index f46fdc6b..bd191d74 100644 --- a/go.sum +++ b/go.sum @@ -54,8 +54,8 @@ github.com/StackExchange/wmi v1.2.1 h1:VIkavFPXSjcnS+O8yTq7NI32k0R5Aj+v39y29VYDO github.com/StackExchange/wmi v1.2.1/go.mod h1:rcmrprowKIVzvc+NUiLncP2uuArMWLCbu9SBzvHz7e8= github.com/aerogo/aero v1.3.29/go.mod h1:nHuG9q4VrzH7ZtiE1TEIt9Lazp/w3WpC3nqAKREkg0Q= github.com/aerogo/aero v1.3.30/go.mod h1:iAzV2JRnKAFOo/c+4KIgf35JGS82qMU4tI06ocHeRrk= -github.com/aerogo/aero v1.3.58 h1:LmQ8TYRwIaiMmUQlfD85bKNb+2LRm7ueXIoaw1EClLs= -github.com/aerogo/aero v1.3.58/go.mod h1:ehwj+mb117xQRTvp11jlnrRNPgbcYL6s6aBk9wbIZ0o= +github.com/aerogo/aero v1.3.59 h1:5yu+kk/uIXAXADKSLCFKhxAzThCehvpbF6gst+G32Fw= +github.com/aerogo/aero v1.3.59/go.mod h1:ehwj+mb117xQRTvp11jlnrRNPgbcYL6s6aBk9wbIZ0o= github.com/aerogo/api v0.2.3 h1:REQR3a6WXzaHpNSF9NCjj4HjvSQdcHrMWGnp/xIpCh4= github.com/aerogo/api v0.2.3/go.mod h1:cClK+FXNc0IRGdDxAH5XmtibBxwUXCM2lLKMF7jKB+8= github.com/aerogo/cluster v0.1.8 h1:N/jU2t7kQfKjXzQIArBQvtNkJCiaP9+jgdEid3P2Omc= diff --git a/pages/notifications/api.go b/pages/notifications/api.go index 29364dcc..f9e2b8c4 100644 --- a/pages/notifications/api.go +++ b/pages/notifications/api.go @@ -6,6 +6,7 @@ import ( "strconv" "github.com/aerogo/aero" + "github.com/aerogo/aero/event" "github.com/animenotifier/notify.moe/arn" "github.com/animenotifier/notify.moe/assets" ) @@ -39,10 +40,7 @@ func MarkNotificationsAsSeen(ctx aero.Context) error { } // Update the counter on all clients - user.BroadcastEvent(&aero.Event{ - Name: "notificationCount", - Data: 0, - }) + user.BroadcastEvent(event.New("notificationCount", 0)) return nil } diff --git a/pages/sse/sse.go b/pages/sse/sse.go index ae5f3044..cf6e748b 100644 --- a/pages/sse/sse.go +++ b/pages/sse/sse.go @@ -4,6 +4,7 @@ import ( "net/http" "github.com/aerogo/aero" + "github.com/aerogo/aero/event" "github.com/animenotifier/notify.moe/arn" "github.com/animenotifier/notify.moe/components/css" "github.com/animenotifier/notify.moe/components/js" @@ -22,35 +23,29 @@ func Events(ctx aero.Context) error { return ctx.Error(http.StatusUnauthorized, "Not logged in") } - stream := aero.NewEventStream() + stream := event.NewStream() user.AddEventStream(stream) go func() { defer user.RemoveEventStream(stream) // Send the ETag for the scripts - stream.Events <- &aero.Event{ - Name: "etag", - Data: struct { - URL string `json:"url"` - ETag string `json:"etag"` - }{ - URL: "/scripts", - ETag: scriptsETag, - }, - } + stream.Events <- event.New("etag", struct { + URL string `json:"url"` + ETag string `json:"etag"` + }{ + URL: "/scripts", + ETag: scriptsETag, + }) // Send the ETag for the styles - stream.Events <- &aero.Event{ - Name: "etag", - Data: struct { - URL string `json:"url"` - ETag string `json:"etag"` - }{ - URL: "/styles", - ETag: stylesETag, - }, - } + stream.Events <- event.New("etag", struct { + URL string `json:"url"` + ETag string `json:"etag"` + }{ + URL: "/styles", + ETag: stylesETag, + }) // Wait until the user closes the tab or disconnects <-stream.Closed