Only refetch changed resources when receive a "refreshResource" event

This commit is contained in:
Deluan
2021-06-15 16:09:01 -04:00
parent 8a56584aed
commit 8383527aab
6 changed files with 235 additions and 39 deletions
+26 -5
View File
@@ -37,12 +37,33 @@ type KeepAlive struct {
TS int64 `json:"ts"`
}
type RefreshResource struct {
baseEvent
Resource string `json:"resource"`
}
type ServerStart struct {
baseEvent
StartTime time.Time `json:"startTime"`
}
const Any = "*"
type RefreshResource struct {
baseEvent
resources map[string][]string
}
func (rr *RefreshResource) With(resource string, ids ...string) *RefreshResource {
if rr.resources == nil {
rr.resources = make(map[string][]string)
}
for i := range ids {
rr.resources[resource] = append(rr.resources[resource], ids[i])
}
return rr
}
func (rr *RefreshResource) Data(evt Event) string {
if rr.resources == nil {
return `{"*":"*"}`
}
r := evt.(*RefreshResource)
data, _ := json.Marshal(r.resources)
return string(data)
}
+37 -12
View File
@@ -5,17 +5,42 @@ import (
. "github.com/onsi/gomega"
)
var _ = Describe("Event", func() {
It("marshals Event to JSON", func() {
testEvent := TestEvent{Test: "some data"}
data := testEvent.Data(&testEvent)
Expect(data).To(Equal(`{"Test":"some data"}`))
name := testEvent.Name(&testEvent)
Expect(name).To(Equal("testEvent"))
var _ = Describe("Events", func() {
Describe("Event", func() {
type TestEvent struct {
baseEvent
Test string
}
It("marshals Event to JSON", func() {
testEvent := TestEvent{Test: "some data"}
data := testEvent.Data(&testEvent)
Expect(data).To(Equal(`{"Test":"some data"}`))
name := testEvent.Name(&testEvent)
Expect(name).To(Equal("testEvent"))
})
})
Describe("RefreshResource", func() {
var rr *RefreshResource
BeforeEach(func() {
rr = &RefreshResource{}
})
It("should render to full refresh if event is empty", func() {
data := rr.Data(rr)
Expect(data).To(Equal(`{"*":"*"}`))
})
It("should group resources based on name", func() {
rr.With("album", "al-1").With("song", "sg-1").With("artist", "ar-1")
rr.With("album", "al-2", "al-3").With("song", "sg-2").With("artist", "ar-2")
data := rr.Data(rr)
Expect(data).To(Equal(`{"album":["al-1","al-2","al-3"],"artist":["ar-1","ar-2"],"song":["sg-1","sg-2"]}`))
})
It("should send a * for when Any is used as id", func() {
rr.With("album", Any)
data := rr.Data(rr)
Expect(data).To(Equal(`{"album":["*"]}`))
})
})
})
type TestEvent struct {
baseEvent
Test string
}