a57d75cd11
This allows using listener pattern instead of coroutines within the recommended repository pattern and avoid the jank due to coroutines Bug: 390572144 Test: atest ListenableRefTest Flag: com.android.launcher3.model_repository Change-Id: I9f74166e01e1bdb4cb8afce3213022ea10137230
38 lines
1.2 KiB
Kotlin
38 lines
1.2 KiB
Kotlin
/*
|
|
* Copyright (C) 2025 The Android Open Source Project
|
|
*
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
* you may not use this file except in compliance with the License.
|
|
* You may obtain a copy of the License at
|
|
*
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
*
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
* See the License for the specific language governing permissions and
|
|
* limitations under the License.
|
|
*/
|
|
|
|
package com.android.launcher3.util
|
|
|
|
import androidx.lifecycle.DefaultLifecycleObserver
|
|
import androidx.lifecycle.Lifecycle
|
|
import androidx.lifecycle.LifecycleOwner
|
|
|
|
/** Extensions for [SafeCloseable] */
|
|
object SafeCloseableEx {
|
|
|
|
/** Cleans up this closable when the [lifecycle] is destroyed */
|
|
fun SafeCloseable.cleanupWith(lifecycle: Lifecycle) {
|
|
lifecycle.addObserver(
|
|
object : DefaultLifecycleObserver {
|
|
override fun onDestroy(owner: LifecycleOwner) {
|
|
this@cleanupWith.close()
|
|
lifecycle.removeObserver(this)
|
|
}
|
|
}
|
|
)
|
|
}
|
|
}
|