Skip to content

Commit

Permalink
perf: Make CameraPositionState and MarkerState stable (#254)
Browse files Browse the repository at this point in the history
  • Loading branch information
DSteve595 authored Jan 19, 2023
1 parent 12251ff commit 52bd69e
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -110,16 +110,16 @@ public class CameraPositionState(

// Used to perform side effects thread-safely.
// Guards all mutable properties that are not `by mutableStateOf`.
private val lock = Any()
private val lock = Unit

// The map currently associated with this CameraPositionState.
// Guarded by `lock`.
private var map: GoogleMap? = null
private var map: GoogleMap? by mutableStateOf(null)

// An action to run when the map becomes available or unavailable.
// represents a mutually exclusive mutation to perform while holding `lock`.
// Guarded by `lock`.
private var onMapChanged: OnMapChangedCallback? = null
private var onMapChanged: OnMapChangedCallback? by mutableStateOf(null)

/**
* Set [onMapChanged] to [callback], invoking the current callback's
Expand All @@ -133,7 +133,7 @@ public class CameraPositionState(
// A token representing the current owner of any ongoing motion in progress.
// Used to determine if map animation should stop when calls to animate end.
// Guarded by `lock`.
private var movementOwner: Any? = null
private var movementOwner: Any? by mutableStateOf(null)

/**
* Used with [onMapChangedLocked] to execute one-time actions when a map becomes available
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import androidx.compose.runtime.Composable
import androidx.compose.runtime.ComposeNode
import androidx.compose.runtime.CompositionContext
import androidx.compose.runtime.Immutable
import androidx.compose.runtime.MutableState
import androidx.compose.runtime.currentComposer
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
Expand Down Expand Up @@ -81,13 +82,15 @@ public class MarkerState(
internal set

// The marker associated with this MarkerState.
internal var marker: Marker? = null
private val markerState: MutableState<Marker?> = mutableStateOf(null)
internal var marker: Marker?
get() = markerState.value
set(value) {
if (field == null && value == null) return
if (field != null && value != null) {
if (markerState.value == null && value == null) return
if (markerState.value != null && value != null) {
error("MarkerState may only be associated with one Marker at a time.")
}
field = value
markerState.value = value
}

/**
Expand Down

0 comments on commit 52bd69e

Please sign in to comment.