Skip to content

Commit 7f969a8

Browse files
authored
Merge pull request #349 from anyproto/go-3508-decouple-approach2
GO-3508 Add dependencies tracing
2 parents 6037687 + badb332 commit 7f969a8

File tree

2 files changed

+29
-11
lines changed

2 files changed

+29
-11
lines changed

app/app.go

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -57,14 +57,15 @@ type ComponentStatable interface {
5757
// App is the central part of the application
5858
// It contains and manages all components
5959
type App struct {
60-
parent *App
61-
components []Component
62-
mu sync.RWMutex
63-
startStat Stat
64-
stopStat Stat
65-
deviceState int
66-
versionName string
67-
anySyncVersion string
60+
parent *App
61+
components []Component
62+
mu sync.RWMutex
63+
startStat Stat
64+
stopStat Stat
65+
deviceState int
66+
versionName string
67+
anySyncVersion string
68+
componentListener func(comp Component)
6869
}
6970

7071
// Name returns app name
@@ -130,9 +131,10 @@ func VersionDescription() string {
130131
// It doesn't call Start on any of the parent's components
131132
func (app *App) ChildApp() *App {
132133
return &App{
133-
parent: app,
134-
deviceState: app.deviceState,
135-
anySyncVersion: app.AnySyncVersion(),
134+
parent: app,
135+
deviceState: app.deviceState,
136+
anySyncVersion: app.AnySyncVersion(),
137+
componentListener: app.componentListener,
136138
}
137139
}
138140

@@ -159,6 +161,7 @@ func (app *App) Component(name string) Component {
159161
for current != nil {
160162
for _, s := range current.components {
161163
if s.Name() == name {
164+
app.onComponent(s)
162165
return s
163166
}
164167
}
@@ -184,6 +187,7 @@ func MustComponent[i any](app *App) i {
184187
for current != nil {
185188
for _, s := range current.components {
186189
if v, ok := s.(i); ok {
190+
app.onComponent(s)
187191
return v
188192
}
189193
}
@@ -384,3 +388,9 @@ func (app *App) AnySyncVersion() string {
384388
})
385389
return app.anySyncVersion
386390
}
391+
392+
func (app *App) onComponent(s Component) {
393+
if app.componentListener != nil {
394+
app.componentListener(s)
395+
}
396+
}

app/apptrace.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
//go:build appdebug
2+
// +build appdebug
3+
4+
package app
5+
6+
func (app *App) SetOnComponentListener(listener func(comp Component)) {
7+
app.componentListener = listener
8+
}

0 commit comments

Comments
 (0)