Use new event API

This commit is contained in:
Eduard Urbach 2021-11-20 20:52:08 +09:00
parent df5f0c0f65
commit 9368689019
Signed by: akyoto
GPG Key ID: C874F672B1AF20C0
8 changed files with 38 additions and 50 deletions

View File

@ -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 {

View File

@ -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.

View File

@ -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:
}
}

View File

@ -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)
}
}

2
go.mod
View File

@ -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

4
go.sum
View File

@ -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=

View File

@ -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
}

View File

@ -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