Kirill Zhukov
11/25/2024, 11:10 PMkotlin-inject-anvil
, should I expect FeatureFlagService
and FeatureFlagSyncWorker
to be singletons here?
@Origin(value = FeatureFlagServiceImpl::class)
@ContributesTo(scope = AppScope::class)
public interface FeatureFlagServiceImplComponent {
@Provides
@SingleIn(scope = AppScope::class)
public fun provideFeatureFlagService(implementation: FeatureFlagServiceImpl): FeatureFlagService = implementation
@Provides
@SingleIn(scope = AppScope::class)
public fun provideFeatureFlagSyncWorker(implementation: FeatureFlagServiceImpl): FeatureFlagSyncWorker = implementation
}
Kirill Zhukov
11/25/2024, 11:11 PMprivate val featureFlagServiceImpl: FeatureFlagServiceImpl
get() = FeatureFlagServiceImpl(
featureFlags = listFeatureFlagFeatureFlagValue,
Kirill Zhukov
11/25/2024, 11:13 PMfeatureFlagSyncWorker = _scoped.get("build.wallet.feature.FeatureFlagSyncWorker") {
provideFeatureFlagSyncWorker(
implementation = featureFlagServiceImpl
)
},
Kirill Zhukov
11/25/2024, 11:14 PM@Inject
@ContributesSingleton(AppScope::class)
class FeatureFlagServiceImpl(
private val featureFlags: List<FeatureFlag<out FeatureFlagValue>>,
private val featureFlagSyncer: FeatureFlagSyncer,
) : FeatureFlagService, FeatureFlagSyncWorker
Kirill Zhukov
11/25/2024, 11:14 PMFeatureFlagServiceImpl
itself is not annotated with @SingleIn
?Kirill Zhukov
11/25/2024, 11:25 PMKirill Zhukov
11/25/2024, 11:26 PMralf
11/26/2024, 4:35 AMFeatureFlagServiceImpl
. It should take precedence over the inject constructor and not result in a duplicate binding. At least this was the case with Dagger.
In our custom annotation we avoid adding @Inject
to these class altogether, e.g. we have:
@ContributesRobot(AppScope::class)
class MyRobot : Robot
Notice that there’s no @Inject
and we generate the provider method instead with the right scope.Kirill Zhukov
11/26/2024, 4:42 AM@Provides
fun provideFeatureFlagServiceImpl(
a: ...,
b: ...
): FeatureFlagServiceImpl = FeatureFlagServiceImpl(a, b)
Kirill Zhukov
11/26/2024, 5:41 AM@Origin(value = FeatureFlagServiceImpl::class)
@ContributesTo(scope = AppScope::class)
public interface FeatureFlagServiceImplComponent {
@Provides
@SingleIn(scope = AppScope::class)
public fun provideFeatureFlagServiceImpl(featureFlags: List<FeatureFlag<out FeatureFlagValue>>, featureFlagSyncer: FeatureFlagSyncer): FeatureFlagServiceImpl = FeatureFlagServiceImpl(featureFlags, featureFlagSyncer)
@Provides
public fun provideFeatureFlagService(implementation: FeatureFlagServiceImpl): FeatureFlagService = implementation
@Provides
public fun provideFeatureFlagSyncWorker(implementation: FeatureFlagServiceImpl): FeatureFlagSyncWorker = implementation
}
Kirill Zhukov
11/26/2024, 5:41 AM