Monorepo for Tangled tangled.org

appview/notify: remove reflection usage in mergedNotifier #1052

merged opened by oppi.li targeting master from op/lyvszuuqvnzs
Labels

None yet.

assignee

None yet.

Participants 1
AT URI
at://did:plc:qfpnj4og54vl56wngdriaxug/sh.tangled.repo.pull/3me6rhvz4xn22
+20 -31
Diff #0
+20 -31
appview/notify/merged_notifier.go
··· 2 3 import ( 4 "context" 5 - "log/slog" 6 - "reflect" 7 "sync" 8 9 "github.com/bluesky-social/indigo/atproto/syntax" 10 "tangled.org/core/appview/models" 11 - "tangled.org/core/log" 12 ) 13 14 type mergedNotifier struct { 15 notifiers []Notifier 16 - logger *slog.Logger 17 } 18 19 - func NewMergedNotifier(notifiers []Notifier, logger *slog.Logger) Notifier { 20 - return &mergedNotifier{notifiers, logger} 21 } 22 23 var _ Notifier = &mergedNotifier{} 24 25 // fanout calls the same method on all notifiers concurrently 26 - func (m *mergedNotifier) fanout(method string, ctx context.Context, args ...any) { 27 - ctx = log.IntoContext(ctx, m.logger.With("method", method)) 28 var wg sync.WaitGroup 29 for _, n := range m.notifiers { 30 wg.Add(1) 31 go func(notifier Notifier) { 32 defer wg.Done() 33 - v := reflect.ValueOf(notifier).MethodByName(method) 34 - in := make([]reflect.Value, len(args)+1) 35 - in[0] = reflect.ValueOf(ctx) 36 - for i, arg := range args { 37 - in[i+1] = reflect.ValueOf(arg) 38 - } 39 - v.Call(in) 40 }(n) 41 } 42 } 43 44 func (m *mergedNotifier) NewRepo(ctx context.Context, repo *models.Repo) { 45 - m.fanout("NewRepo", ctx, repo) 46 } 47 48 func (m *mergedNotifier) NewStar(ctx context.Context, star *models.Star) { 49 - m.fanout("NewStar", ctx, star) 50 } 51 52 func (m *mergedNotifier) DeleteStar(ctx context.Context, star *models.Star) { 53 - m.fanout("DeleteStar", ctx, star) 54 } 55 56 func (m *mergedNotifier) NewIssue(ctx context.Context, issue *models.Issue, mentions []syntax.DID) { 57 - m.fanout("NewIssue", ctx, issue, mentions) 58 } 59 60 func (m *mergedNotifier) NewIssueComment(ctx context.Context, comment *models.IssueComment, mentions []syntax.DID) { 61 - m.fanout("NewIssueComment", ctx, comment, mentions) 62 } 63 64 func (m *mergedNotifier) NewIssueState(ctx context.Context, actor syntax.DID, issue *models.Issue) { 65 - m.fanout("NewIssueState", ctx, actor, issue) 66 } 67 68 func (m *mergedNotifier) DeleteIssue(ctx context.Context, issue *models.Issue) { 69 - m.fanout("DeleteIssue", ctx, issue) 70 } 71 72 func (m *mergedNotifier) NewFollow(ctx context.Context, follow *models.Follow) { 73 - m.fanout("NewFollow", ctx, follow) 74 } 75 76 func (m *mergedNotifier) DeleteFollow(ctx context.Context, follow *models.Follow) { 77 - m.fanout("DeleteFollow", ctx, follow) 78 } 79 80 func (m *mergedNotifier) NewPull(ctx context.Context, pull *models.Pull) { 81 - m.fanout("NewPull", ctx, pull) 82 } 83 84 func (m *mergedNotifier) NewPullComment(ctx context.Context, comment *models.PullComment, mentions []syntax.DID) { 85 - m.fanout("NewPullComment", ctx, comment, mentions) 86 } 87 88 func (m *mergedNotifier) NewPullState(ctx context.Context, actor syntax.DID, pull *models.Pull) { 89 - m.fanout("NewPullState", ctx, actor, pull) 90 } 91 92 func (m *mergedNotifier) UpdateProfile(ctx context.Context, profile *models.Profile) { 93 - m.fanout("UpdateProfile", ctx, profile) 94 } 95 96 func (m *mergedNotifier) NewString(ctx context.Context, s *models.String) { 97 - m.fanout("NewString", ctx, s) 98 } 99 100 func (m *mergedNotifier) EditString(ctx context.Context, s *models.String) { 101 - m.fanout("EditString", ctx, s) 102 } 103 104 func (m *mergedNotifier) DeleteString(ctx context.Context, did, rkey string) { 105 - m.fanout("DeleteString", ctx, did, rkey) 106 }
··· 2 3 import ( 4 "context" 5 "sync" 6 7 "github.com/bluesky-social/indigo/atproto/syntax" 8 "tangled.org/core/appview/models" 9 ) 10 11 type mergedNotifier struct { 12 notifiers []Notifier 13 } 14 15 + func NewMergedNotifier(notifiers []Notifier) Notifier { 16 + return &mergedNotifier{notifiers} 17 } 18 19 var _ Notifier = &mergedNotifier{} 20 21 // fanout calls the same method on all notifiers concurrently 22 + func (m *mergedNotifier) fanout(callback func(Notifier)) { 23 var wg sync.WaitGroup 24 for _, n := range m.notifiers { 25 wg.Add(1) 26 go func(notifier Notifier) { 27 defer wg.Done() 28 + callback(n) 29 }(n) 30 } 31 } 32 33 func (m *mergedNotifier) NewRepo(ctx context.Context, repo *models.Repo) { 34 + m.fanout(func(n Notifier) { n.NewRepo(ctx, repo) }) 35 } 36 37 func (m *mergedNotifier) NewStar(ctx context.Context, star *models.Star) { 38 + m.fanout(func(n Notifier) { n.NewStar(ctx, star) }) 39 } 40 41 func (m *mergedNotifier) DeleteStar(ctx context.Context, star *models.Star) { 42 + m.fanout(func(n Notifier) { n.DeleteStar(ctx, star) }) 43 } 44 45 func (m *mergedNotifier) NewIssue(ctx context.Context, issue *models.Issue, mentions []syntax.DID) { 46 + m.fanout(func(n Notifier) { n.NewIssue(ctx, issue, mentions) }) 47 } 48 49 func (m *mergedNotifier) NewIssueComment(ctx context.Context, comment *models.IssueComment, mentions []syntax.DID) { 50 + m.fanout(func(n Notifier) { n.NewIssueComment(ctx, comment, mentions) }) 51 } 52 53 func (m *mergedNotifier) NewIssueState(ctx context.Context, actor syntax.DID, issue *models.Issue) { 54 + m.fanout(func(n Notifier) { n.NewIssueState(ctx, actor, issue) }) 55 } 56 57 func (m *mergedNotifier) DeleteIssue(ctx context.Context, issue *models.Issue) { 58 + m.fanout(func(n Notifier) { n.DeleteIssue(ctx, issue) }) 59 } 60 61 func (m *mergedNotifier) NewFollow(ctx context.Context, follow *models.Follow) { 62 + m.fanout(func(n Notifier) { n.NewFollow(ctx, follow) }) 63 } 64 65 func (m *mergedNotifier) DeleteFollow(ctx context.Context, follow *models.Follow) { 66 + m.fanout(func(n Notifier) { n.DeleteFollow(ctx, follow) }) 67 } 68 69 func (m *mergedNotifier) NewPull(ctx context.Context, pull *models.Pull) { 70 + m.fanout(func(n Notifier) { n.NewPull(ctx, pull) }) 71 } 72 73 func (m *mergedNotifier) NewPullComment(ctx context.Context, comment *models.PullComment, mentions []syntax.DID) { 74 + m.fanout(func(n Notifier) { n.NewPullComment(ctx, comment, mentions) }) 75 } 76 77 func (m *mergedNotifier) NewPullState(ctx context.Context, actor syntax.DID, pull *models.Pull) { 78 + m.fanout(func(n Notifier) { n.NewPullState(ctx, actor, pull) }) 79 } 80 81 func (m *mergedNotifier) UpdateProfile(ctx context.Context, profile *models.Profile) { 82 + m.fanout(func(n Notifier) { n.UpdateProfile(ctx, profile) }) 83 } 84 85 func (m *mergedNotifier) NewString(ctx context.Context, s *models.String) { 86 + m.fanout(func(n Notifier) { n.NewString(ctx, s) }) 87 } 88 89 func (m *mergedNotifier) EditString(ctx context.Context, s *models.String) { 90 + m.fanout(func(n Notifier) { n.EditString(ctx, s) }) 91 } 92 93 func (m *mergedNotifier) DeleteString(ctx context.Context, did, rkey string) { 94 + m.fanout(func(n Notifier) { n.DeleteString(ctx, did, rkey) }) 95 }

History

1 round 0 comments
sign up or login to add to the discussion
oppi.li submitted #0
1 commit
expand
appview/notify: remove reflection usage in mergedNotifier
2/3 timeout, 1/3 success
expand
expand 0 comments
pull request successfully merged