-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Lifecycle coroutine scope leak fixes (#141)
* - automatically cancel LifecycleCoroutineScope when lifecycle reaches DESTROYED (fixes #135) - automatically remove lifecycleScope extension property from cache when lifecycle reaches DESTROYED (fixes #136) * dispatch-internal-test-android module * update lifecycle handling * update lifecycle handling * update lifecycle handling * convert LifecycleScopeExtensionTest from Kotest to JUnit5 to fix some weird recursive behavior * convert LifecycleScopeExtensionTest from Kotest to JUnit5 to fix some weird recursive behavior * remove workspace.xml backup which shouldn't have been added to git
- Loading branch information
Showing
35 changed files
with
922 additions
and
74 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
...-android-lifecycle-extensions/src/main/java/dispatch/android/lifecycle/internal/atomic.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
/* | ||
* Copyright (C) 2020 Rick Busarow | ||
* 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 dispatch.android.lifecycle.internal | ||
|
||
import androidx.lifecycle.* | ||
import dispatch.android.lifecycle.* | ||
import dispatch.core.* | ||
import kotlinx.coroutines.* | ||
import java.util.concurrent.* | ||
|
||
/** | ||
* Normal [getOrPut] is guaranteed to only return a single instance for a given key, | ||
* but the [defaultValue] lambda may be invoked unnecessarily. This would create a new instance | ||
* of [LifecycleCoroutineScope] without actually returning it. | ||
* | ||
* This extra [LifecycleCoroutineScope] would still be active and observing the [lifecycle][key]. | ||
* | ||
* This function compares the result of the lambda to the result of [getOrPut], | ||
* and cancels the lambda's result if it's different. | ||
* | ||
* @see getOrPut | ||
*/ | ||
internal inline fun ConcurrentMap<Lifecycle, LifecycleCoroutineScope>.atomicGetOrPut( | ||
key: Lifecycle, | ||
defaultValue: () -> LifecycleCoroutineScope | ||
): LifecycleCoroutineScope { | ||
|
||
var newInstance: LifecycleCoroutineScope? = null | ||
|
||
val existingOrNew = getOrPut(key) { | ||
newInstance = defaultValue.invoke() | ||
newInstance!! | ||
} | ||
|
||
// If the second value is different, that means the first was never inserted into the map. | ||
// Cancel the observer for `newInstance` and cancel the coroutineContext. | ||
newInstance?.let { new -> | ||
if (new != existingOrNew) { | ||
existingOrNew.launchMainImmediate { | ||
|
||
new.coroutineContext[Job]?.cancel() | ||
} | ||
} | ||
} | ||
|
||
return existingOrNew | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.