feat: WebSocket based implementation of server sent events for cache busting (#527)

* rough implementation of WS based event system for server side notifications of mutation

* fix test construction

* fix deadlock on event bus

* disable linter error

* add item mutation events

* remove old event bus code

* refactor event system to use composables

* refresh items table when new item is added

* fix create form errors

* cleanup unnecessary calls

* fix importer erorrs + limit fn calls on import
This commit is contained in:
Hayden 2023-08-02 13:00:57 -08:00 committed by GitHub
parent cceec06148
commit 2cbcc8bb1d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
31 changed files with 458 additions and 208 deletions

View file

@ -5,6 +5,7 @@ import (
"time"
"github.com/google/uuid"
"github.com/hay-kot/homebox/backend/internal/core/services/reporting/eventbus"
"github.com/hay-kot/homebox/backend/internal/data/ent"
"github.com/hay-kot/homebox/backend/internal/data/ent/group"
"github.com/hay-kot/homebox/backend/internal/data/ent/item"
@ -13,8 +14,10 @@ import (
)
type LabelRepository struct {
db *ent.Client
db *ent.Client
bus *eventbus.EventBus
}
type (
LabelCreate struct {
Name string `json:"name" validate:"required,min=1,max=255"`
@ -65,6 +68,12 @@ func mapLabelOut(label *ent.Label) LabelOut {
}
}
func (r *LabelRepository) publishMutationEvent(GID uuid.UUID) {
if r.bus != nil {
r.bus.Publish(eventbus.EventLabelMutation, eventbus.GroupMutationEvent{GID: GID})
}
}
func (r *LabelRepository) getOne(ctx context.Context, where ...predicate.Label) (LabelOut, error) {
return mapLabelOutErr(r.db.Label.Query().
Where(where...).
@ -105,6 +114,7 @@ func (r *LabelRepository) Create(ctx context.Context, groupdId uuid.UUID, data L
}
label.Edges.Group = &ent.Group{ID: groupdId} // bootstrap group ID
r.publishMutationEvent(groupdId)
return mapLabelOut(label), err
}
@ -121,25 +131,19 @@ func (r *LabelRepository) update(ctx context.Context, data LabelUpdate, where ..
Save(ctx)
}
func (r *LabelRepository) Update(ctx context.Context, data LabelUpdate) (LabelOut, error) {
_, err := r.update(ctx, data, label.ID(data.ID))
if err != nil {
return LabelOut{}, err
}
return r.GetOne(ctx, data.ID)
}
func (r *LabelRepository) UpdateByGroup(ctx context.Context, GID uuid.UUID, data LabelUpdate) (LabelOut, error) {
_, err := r.update(ctx, data, label.ID(data.ID), label.HasGroupWith(group.ID(GID)))
if err != nil {
return LabelOut{}, err
}
r.publishMutationEvent(GID)
return r.GetOne(ctx, data.ID)
}
func (r *LabelRepository) Delete(ctx context.Context, id uuid.UUID) error {
// delete removes the label from the database. This should only be used when
// the label's ownership is already confirmed/validated.
func (r *LabelRepository) delete(ctx context.Context, id uuid.UUID) error {
return r.db.Label.DeleteOneID(id).Exec(ctx)
}
@ -149,6 +153,11 @@ func (r *LabelRepository) DeleteByGroup(ctx context.Context, gid, id uuid.UUID)
label.ID(id),
label.HasGroupWith(group.ID(gid)),
).Exec(ctx)
if err != nil {
return err
}
return err
r.publishMutationEvent(gid)
return nil
}