Skip to content

Commit

Permalink
fix: avoid eager initialization of data sources
Browse files Browse the repository at this point in the history
  • Loading branch information
fractalwrench committed Jul 3, 2024
1 parent 9e231ca commit abc47dd
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ internal class DataSourceState<T : DataSource<*>>(
onSessionTypeChange()
}

private val enabledDataSource by lazy(factory)
private val factoryRef = lazy(factory)

var dataSource: T? = null
private set
Expand All @@ -53,7 +53,9 @@ internal class DataSourceState<T : DataSource<*>>(
*/
private fun onSessionTypeChange() {
updateDataSource()
enabledDataSource?.resetDataCaptureLimits()
if (factoryRef.isInitialized()) {
factoryRef.value?.resetDataCaptureLimits()
}
}

/**
Expand All @@ -68,7 +70,7 @@ internal class DataSourceState<T : DataSource<*>>(
currentSessionType != null && currentSessionType != disabledSessionType && configGate()

if (enabled && dataSource == null) {
dataSource = enabledDataSource?.apply {
dataSource = factoryRef.value?.apply {
enableDataCapture()
}
} else if (!enabled && dataSource != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,27 +135,27 @@ internal class DataSourceStateTest {
// data capture is always disabled by default.
assertEquals(0, source.enableDataCaptureCount)
assertEquals(0, source.disableDataCaptureCount)
assertEquals(1, source.resetCount)
assertEquals(0, source.resetCount)

// new session should enable data capture
state.currentSessionType = SessionType.FOREGROUND
state.currentSessionType = SessionType.FOREGROUND
assertEquals(1, source.enableDataCaptureCount)
assertEquals(0, source.disableDataCaptureCount)
assertEquals(3, source.resetCount)
assertEquals(2, source.resetCount)

// extra payload types should not re-register listeners
state.currentSessionType = SessionType.BACKGROUND
state.currentSessionType = SessionType.BACKGROUND
assertEquals(1, source.enableDataCaptureCount)
assertEquals(1, source.disableDataCaptureCount)
assertEquals(5, source.resetCount)
assertEquals(4, source.resetCount)

// functions can be called multiple times without issue
state.currentSessionType = SessionType.FOREGROUND
state.currentSessionType = SessionType.BACKGROUND
assertEquals(2, source.enableDataCaptureCount)
assertEquals(2, source.disableDataCaptureCount)
assertEquals(7, source.resetCount)
assertEquals(6, source.resetCount)
}
}

0 comments on commit abc47dd

Please sign in to comment.