diff --git a/apis/realtime.go b/apis/realtime.go index b4f6d425..55dad98d 100644 --- a/apis/realtime.go +++ b/apis/realtime.go @@ -159,8 +159,8 @@ func (api *realtimeApi) bindEvents() { adminTable := (&models.Admin{}).TableName() // update user/admin auth state - api.app.OnModelAfterUpdate().Add(func(data *core.ModelEvent) error { - modelTable := data.Model.TableName() + api.app.OnModelAfterUpdate().Add(func(e *core.ModelEvent) error { + modelTable := e.Model.TableName() var contextKey string switch modelTable { @@ -174,8 +174,8 @@ func (api *realtimeApi) bindEvents() { for _, client := range api.app.SubscriptionsBroker().Clients() { model, _ := client.Get(contextKey).(models.Model) - if model != nil && model.GetId() == data.Model.GetId() { - client.Set(contextKey, data.Model) + if model != nil && model.GetId() == e.Model.GetId() { + client.Set(contextKey, e.Model) } } @@ -183,8 +183,8 @@ func (api *realtimeApi) bindEvents() { }) // remove user/admin client(s) - api.app.OnModelAfterDelete().Add(func(data *core.ModelEvent) error { - modelTable := data.Model.TableName() + api.app.OnModelAfterDelete().Add(func(e *core.ModelEvent) error { + modelTable := e.Model.TableName() var contextKey string switch modelTable { @@ -198,7 +198,7 @@ func (api *realtimeApi) bindEvents() { for _, client := range api.app.SubscriptionsBroker().Clients() { model, _ := client.Get(contextKey).(models.Model) - if model != nil && model.GetId() == data.Model.GetId() { + if model != nil && model.GetId() == e.Model.GetId() { api.app.SubscriptionsBroker().Unregister(client.Id()) } } @@ -206,18 +206,20 @@ func (api *realtimeApi) bindEvents() { return nil }) - api.app.OnRecordAfterCreateRequest().Add(func(data *core.RecordCreateEvent) error { - api.broadcastRecord("create", data.Record) + api.app.OnRecordAfterCreateRequest().Add(func(e *core.RecordCreateEvent) error { + api.broadcastRecord("create", e.Record) return nil }) - api.app.OnRecordAfterUpdateRequest().Add(func(data *core.RecordUpdateEvent) error { - api.broadcastRecord("update", data.Record) + api.app.OnRecordAfterUpdateRequest().Add(func(e *core.RecordUpdateEvent) error { + api.broadcastRecord("update", e.Record) return nil }) - api.app.OnRecordAfterDeleteRequest().Add(func(data *core.RecordDeleteEvent) error { - api.broadcastRecord("delete", data.Record) + api.app.OnModelBeforeDelete().Add(func(e *core.ModelEvent) error { + if record, ok := e.Model.(*models.Record); ok { + api.broadcastRecord("delete", record) + } return nil }) } diff --git a/tools/hook/hook.go b/tools/hook/hook.go index c0fd40b3..05402802 100644 --- a/tools/hook/hook.go +++ b/tools/hook/hook.go @@ -44,7 +44,9 @@ func (h *Hook[T]) Reset() { // - any non-nil error is returned in one of the handlers func (h *Hook[T]) Trigger(data T, oneOffHandlers ...Handler[T]) error { h.mux.Lock() - handlers := append(h.handlers, oneOffHandlers...) //nolint:gocritic + handlers := make([]Handler[T], 0, len(h.handlers)+len(oneOffHandlers)) + handlers = append(handlers, h.handlers...) + handlers = append(handlers, oneOffHandlers...) // unlock is not deferred to avoid deadlocks when Trigger is called recursive by the handlers h.mux.Unlock()