Skip to content

Commit

Permalink
Use a more JS idiomatic way for having atomic operations
Browse files Browse the repository at this point in the history
  • Loading branch information
mmartosdev committed Jan 9, 2024
1 parent f2ce4fb commit b0f486d
Showing 1 changed file with 8 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package com.bumble.appyx.navigation.platform

import androidx.compose.runtime.AtomicReference

interface Cancellable {
/**
* Cancel the subscription. This call should be idempotent, making it safe to
Expand Down Expand Up @@ -31,15 +29,14 @@ abstract class OnBackPressedCallback(
*
* @return Whether this callback should be considered enabled.
*/
private val cancellablesReference: AtomicReference<List<Cancellable>> =
AtomicReference(emptyList())
private val cancellables: MutableList<Cancellable> = mutableListOf()

/**
* Removes this callback from any [OnBackPressedDispatcher] it is currently
* added to.
*/
fun remove() {
for (cancellable in cancellablesReference.get()) {
for (cancellable in cancellables) {
cancellable.cancel()
}
}
Expand All @@ -49,10 +46,14 @@ abstract class OnBackPressedCallback(
*/
abstract fun handleOnBackPressed()
fun addCancellable(cancellable: Cancellable) {
cancellablesReference.set(cancellablesReference.get() + cancellable)
run {
cancellables.add(cancellable)
}
}

fun removeCancellable(cancellable: Cancellable) {
cancellablesReference.set(cancellablesReference.get() - cancellable)
run {
cancellables.remove(cancellable)
}
}
}

0 comments on commit b0f486d

Please sign in to comment.