Skip to content

Commit b3e2353

Browse files
authored
Conflate map events by default (#3000)
1 parent e648757 commit b3e2353

File tree

4 files changed

+47
-49
lines changed

4 files changed

+47
-49
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ Mapbox welcomes participation and contributions from everyone.
1010
* Add experimental `*UseTheme` support for annotations and `LocationPuck3D`.
1111
* Introduce experimental `Style.setImportColorTheme`, which allows changing the color theme of the style import.
1212
* [compose] Introduce experimental `StyleImportState.styleColorTheme` which allows changing the color theme of the style import.
13+
* Conflate `MapboxMap.mapLoadedEvents`, `MapboxMap.mapLoadingErrorEvents`, `MapboxMap.styleLoadedEvents`, `MapboxMap.styleDataLoadedEvents`, `MapboxMap.cameraChangedEvents`, `MapboxMap.mapIdleEvents`, `MapboxMap.sourceAddedEvents`, `MapboxMap.sourceRemovedEvents`, `MapboxMap.sourceDataLoadedEvents`, `MapboxMap.styleImageMissingEvents`, `MapboxMap.styleImageRemoveUnusedEvents`, `MapboxMap.renderFrameStartedEvents`, `MapboxMap.renderFrameFinishedEvents`, `MapboxMap.resourceRequestEvents` by default to avoid blocking main thread due to slow collectors.
1314

1415
# 11.11.0-beta.1 March 03, 2025
1516
## Features ✨ and improvements 🏁

app/src/main/java/com/mapbox/maps/testapp/examples/coroutines/featurestate/FeatureStateActivity.kt

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ import com.mapbox.maps.extension.style.style
3333
import com.mapbox.maps.logD
3434
import com.mapbox.maps.plugin.scalebar.scalebar
3535
import com.mapbox.maps.testapp.databinding.ActivityFeatureStateBinding
36-
import kotlinx.coroutines.flow.conflate
3736
import kotlinx.coroutines.flow.distinctUntilChangedBy
3837
import kotlinx.coroutines.flow.fold
3938
import kotlinx.coroutines.flow.map
@@ -101,9 +100,6 @@ class FeatureStateActivity : AppCompatActivity() {
101100

102101
// Observe camera changes and query the rendered features under the crosshair.
103102
mapboxMap.cameraChangedEvents
104-
// Conflate the flow to only process the latest event and don't block the main
105-
// thread when queryRenderedFeatures is slow.
106-
.conflate()
107103
.map { _ ->
108104
mapboxMap
109105
.queryRenderedFeatures(

extension-compose/src/main/java/com/mapbox/maps/extension/compose/MapState.kt

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -116,106 +116,106 @@ public class MapState internal constructor(initialGesturesSettings: GesturesSett
116116
}
117117

118118
/**
119-
* [Flow] of [MapLoaded] updates from [MapboxMap.subscribeMapLoaded].
119+
* Conflated [Flow] of [MapLoaded] updates from [MapboxMap.subscribeMapLoaded].
120120
*/
121121
public val mapLoadedEvents: Flow<MapLoaded> = mapboxMapFlow.flatMapLatest {
122122
it?.mapLoadedEvents ?: emptyFlow()
123123
}
124124

125125
/**
126-
* [Flow] of [MapLoadingError] updates from [MapboxMap.subscribeMapLoadingError].
126+
* Conflated [Flow] of [MapLoadingError] updates from [MapboxMap.subscribeMapLoadingError].
127127
*/
128128
public val mapLoadingErrorEvents: Flow<MapLoadingError> = mapboxMapFlow.flatMapLatest {
129129
it?.mapLoadingErrorEvents ?: emptyFlow()
130130
}
131131

132132
/**
133-
* [Flow] of [StyleLoaded] updates from [MapboxMap.subscribeStyleLoaded].
133+
* Conflated [Flow] of [StyleLoaded] updates from [MapboxMap.subscribeStyleLoaded].
134134
*/
135135
public val styleLoadedEvents: Flow<StyleLoaded> = mapboxMapFlow.flatMapLatest {
136136
it?.styleLoadedEvents ?: emptyFlow()
137137
}
138138

139139
/**
140-
* [Flow] of [StyleDataLoaded] updates from [MapboxMap.subscribeStyleDataLoaded].
140+
* Conflated [Flow] of [StyleDataLoaded] updates from [MapboxMap.subscribeStyleDataLoaded].
141141
*/
142142
public val styleDataLoadedEvents: Flow<StyleDataLoaded> = mapboxMapFlow.flatMapLatest {
143143
it?.styleDataLoadedEvents ?: emptyFlow()
144144
}
145145

146146
/**
147-
* [Flow] of [CameraChanged] updates from [MapboxMap.subscribeCameraChanged].
147+
* Conflated [Flow] of [CameraChanged] updates from [MapboxMap.subscribeCameraChanged].
148148
*/
149149
public val cameraChangedEvents: Flow<CameraChanged> = mapboxMapFlow.flatMapLatest {
150150
it?.cameraChangedEvents ?: emptyFlow()
151151
}
152152

153153
/**
154-
* [Flow] of [MapIdle] updates from [MapboxMap.subscribeMapIdle].
154+
* Conflated [Flow] of [MapIdle] updates from [MapboxMap.subscribeMapIdle].
155155
*/
156156
public val mapIdleEvents: Flow<MapIdle> = mapboxMapFlow.flatMapLatest {
157157
it?.mapIdleEvents ?: emptyFlow()
158158
}
159159

160160
/**
161-
* [Flow] of [SourceAdded] updates from [MapboxMap.subscribeSourceAdded].
161+
* Conflated [Flow] of [SourceAdded] updates from [MapboxMap.subscribeSourceAdded].
162162
*/
163163
public val sourceAddedEvents: Flow<SourceAdded> = mapboxMapFlow.flatMapLatest {
164164
it?.sourceAddedEvents ?: emptyFlow()
165165
}
166166

167167
/**
168-
* [Flow] of [SourceRemoved] updates from [MapboxMap.subscribeSourceRemoved].
168+
* Conflated [Flow] of [SourceRemoved] updates from [MapboxMap.subscribeSourceRemoved].
169169
*/
170170
public val sourceRemovedEvents: Flow<SourceRemoved> = mapboxMapFlow.flatMapLatest {
171171
it?.sourceRemovedEvents ?: emptyFlow()
172172
}
173173

174174
/**
175-
* [Flow] of [SourceDataLoaded] updates from [MapboxMap.subscribeSourceDataLoaded].
175+
* Conflated [Flow] of [SourceDataLoaded] updates from [MapboxMap.subscribeSourceDataLoaded].
176176
*/
177177
public val sourceDataLoadedEvents: Flow<SourceDataLoaded> = mapboxMapFlow.flatMapLatest {
178178
it?.sourceDataLoadedEvents ?: emptyFlow()
179179
}
180180

181181
/**
182-
* [Flow] of [StyleImageMissing] updates from [MapboxMap.subscribeStyleImageMissing].
182+
* Conflated [Flow] of [StyleImageMissing] updates from [MapboxMap.subscribeStyleImageMissing].
183183
*/
184184
public val styleImageMissingEvents: Flow<StyleImageMissing> = mapboxMapFlow.flatMapLatest {
185185
it?.styleImageMissingEvents ?: emptyFlow()
186186
}
187187

188188
/**
189-
* [Flow] of [StyleImageRemoveUnused] updates from [MapboxMap.subscribeStyleImageRemoveUnused].
189+
* Conflated [Flow] of [StyleImageRemoveUnused] updates from [MapboxMap.subscribeStyleImageRemoveUnused].
190190
*/
191191
public val styleImageRemoveUnusedEvents: Flow<StyleImageRemoveUnused> =
192192
mapboxMapFlow.flatMapLatest {
193193
it?.styleImageRemoveUnusedEvents ?: emptyFlow()
194194
}
195195

196196
/**
197-
* [Flow] of [RenderFrameStarted] updates from [MapboxMap.subscribeRenderFrameStarted].
197+
* Conflated [Flow] of [RenderFrameStarted] updates from [MapboxMap.subscribeRenderFrameStarted].
198198
*/
199199
public val renderFrameStartedEvents: Flow<RenderFrameStarted> = mapboxMapFlow.flatMapLatest {
200200
it?.renderFrameStartedEvents ?: emptyFlow()
201201
}
202202

203203
/**
204-
* [Flow] of [RenderFrameFinished] updates from [MapboxMap.subscribeRenderFrameFinished].
204+
* Conflated [Flow] of [RenderFrameFinished] updates from [MapboxMap.subscribeRenderFrameFinished].
205205
*/
206206
public val renderFrameFinishedEvents: Flow<RenderFrameFinished> = mapboxMapFlow.flatMapLatest {
207207
it?.renderFrameFinishedEvents ?: emptyFlow()
208208
}
209209

210210
/**
211-
* [Flow] of [ResourceRequest] updates from [MapboxMap.subscribeResourceRequest].
211+
* Conflated [Flow] of [ResourceRequest] updates from [MapboxMap.subscribeResourceRequest].
212212
*/
213213
public val resourceRequestEvents: Flow<ResourceRequest> = mapboxMapFlow.flatMapLatest {
214214
it?.resourceRequestEvents ?: emptyFlow()
215215
}
216216

217217
/**
218-
* [Flow] of [GenericEvent] updates from [MapboxMap.subscribeGenericEvent].
218+
* Conflated [Flow] of [GenericEvent] updates from [MapboxMap.subscribeGenericEvent].
219219
*/
220220
@MapboxExperimental
221221
public fun genericEvents(eventName: String): Flow<GenericEvent> = mapboxMapFlow.flatMapLatest {

maps-sdk/src/main/java/com/mapbox/maps/coroutine/MapboxMapExt.kt

Lines changed: 31 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import kotlinx.coroutines.channels.awaitClose
1818
import kotlinx.coroutines.channels.trySendBlocking
1919
import kotlinx.coroutines.flow.Flow
2020
import kotlinx.coroutines.flow.callbackFlow
21+
import kotlinx.coroutines.flow.conflate
2122
import kotlinx.coroutines.flow.flowOn
2223
import kotlin.coroutines.resume
2324
import kotlin.coroutines.suspendCoroutine
@@ -258,151 +259,151 @@ suspend fun MapboxMap.setFeatureState(
258259
}
259260

260261
/**
261-
* [Flow] of [MapLoaded] updates from [MapboxMap.subscribeMapLoaded].
262+
* Conflated [Flow] of [MapLoaded] updates from [MapboxMap.subscribeMapLoaded].
262263
*/
263264
val MapboxMap.mapLoadedEvents: Flow<MapLoaded>
264265
@JvmSynthetic
265266
get() = callbackFlow<MapLoaded> {
266267
val cancelable = nativeObserver.subscribeMapLoaded(::trySendBlocking, onCancel = channel::close)
267268
awaitClose(cancelable::cancel)
268-
}.flowOn(Dispatchers.Main.immediate)
269+
}.flowOn(Dispatchers.Main.immediate).conflate()
269270

270271
/**
271-
* [Flow] of [MapLoadingError] updates from [MapboxMap.subscribeMapLoadingError].
272+
* Conflated [Flow] of [MapLoadingError] updates from [MapboxMap.subscribeMapLoadingError].
272273
*/
273274
val MapboxMap.mapLoadingErrorEvents: Flow<MapLoadingError>
274275
@JvmSynthetic
275276
get() = callbackFlow<MapLoadingError> {
276277
val cancelable = nativeObserver.subscribeMapLoadingError(::trySendBlocking, onCancel = channel::close)
277278
awaitClose(cancelable::cancel)
278-
}.flowOn(Dispatchers.Main.immediate)
279+
}.flowOn(Dispatchers.Main.immediate).conflate()
279280

280281
/**
281-
* [Flow] of [StyleLoaded] updates from [MapboxMap.subscribeStyleLoaded].
282+
* Conflated [Flow] of [StyleLoaded] updates from [MapboxMap.subscribeStyleLoaded].
282283
*/
283284
val MapboxMap.styleLoadedEvents: Flow<StyleLoaded>
284285
@JvmSynthetic
285286
get() = callbackFlow<StyleLoaded> {
286287
val cancelable = nativeObserver.subscribeStyleLoaded(::trySendBlocking, onCancel = channel::close)
287288
awaitClose(cancelable::cancel)
288-
}.flowOn(Dispatchers.Main.immediate)
289+
}.flowOn(Dispatchers.Main.immediate).conflate()
289290

290291
/**
291-
* [Flow] of [StyleDataLoaded] updates from [MapboxMap.subscribeStyleDataLoaded].
292+
* Conflated [Flow] of [StyleDataLoaded] updates from [MapboxMap.subscribeStyleDataLoaded].
292293
*/
293294
val MapboxMap.styleDataLoadedEvents: Flow<StyleDataLoaded>
294295
@JvmSynthetic
295296
get() = callbackFlow<StyleDataLoaded> {
296297
val cancelable = nativeObserver.subscribeStyleDataLoaded(::trySendBlocking, onCancel = channel::close)
297298
awaitClose(cancelable::cancel)
298-
}.flowOn(Dispatchers.Main.immediate)
299+
}.flowOn(Dispatchers.Main.immediate).conflate()
299300

300301
/**
301-
* [Flow] of [CameraChanged] updates from [MapboxMap.subscribeCameraChanged].
302+
* Conflated [Flow] of [CameraChanged] updates from [MapboxMap.subscribeCameraChanged].
302303
*/
303304
val MapboxMap.cameraChangedEvents: Flow<CameraChanged>
304305
@JvmSynthetic
305306
get() = callbackFlow<CameraChanged> {
306307
val cancelable = nativeObserver.subscribeCameraChanged(::trySendBlocking, onCancel = channel::close)
307308
awaitClose(cancelable::cancel)
308-
}.flowOn(Dispatchers.Main.immediate)
309+
}.flowOn(Dispatchers.Main.immediate).conflate()
309310

310311
/**
311-
* [Flow] of [MapIdle] updates from [MapboxMap.subscribeMapIdle].
312+
* Conflated [Flow] of [MapIdle] updates from [MapboxMap.subscribeMapIdle].
312313
*/
313314
val MapboxMap.mapIdleEvents: Flow<MapIdle>
314315
@JvmSynthetic
315316
get() = callbackFlow<MapIdle> {
316317
val cancelable = nativeObserver.subscribeMapIdle(::trySendBlocking, onCancel = channel::close)
317318
awaitClose(cancelable::cancel)
318-
}.flowOn(Dispatchers.Main.immediate)
319+
}.flowOn(Dispatchers.Main.immediate).conflate()
319320

320321
/**
321-
* [Flow] of [SourceAdded] updates from [MapboxMap.subscribeSourceAdded].
322+
* Conflated [Flow] of [SourceAdded] updates from [MapboxMap.subscribeSourceAdded].
322323
*/
323324
val MapboxMap.sourceAddedEvents: Flow<SourceAdded>
324325
@JvmSynthetic
325326
get() = callbackFlow<SourceAdded> {
326327
val cancelable = nativeObserver.subscribeSourceAdded(::trySendBlocking, onCancel = channel::close)
327328
awaitClose(cancelable::cancel)
328-
}.flowOn(Dispatchers.Main.immediate)
329+
}.flowOn(Dispatchers.Main.immediate).conflate()
329330

330331
/**
331-
* [Flow] of [SourceRemoved] updates from [MapboxMap.subscribeSourceRemoved].
332+
* Conflated [Flow] of [SourceRemoved] updates from [MapboxMap.subscribeSourceRemoved].
332333
*/
333334
val MapboxMap.sourceRemovedEvents: Flow<SourceRemoved>
334335
@JvmSynthetic
335336
get() = callbackFlow<SourceRemoved> {
336337
val cancelable = nativeObserver.subscribeSourceRemoved(::trySendBlocking, onCancel = channel::close)
337338
awaitClose(cancelable::cancel)
338-
}.flowOn(Dispatchers.Main.immediate)
339+
}.flowOn(Dispatchers.Main.immediate).conflate()
339340

340341
/**
341-
* [Flow] of [SourceDataLoaded] updates from [MapboxMap.subscribeSourceDataLoaded].
342+
* Conflated [Flow] of [SourceDataLoaded] updates from [MapboxMap.subscribeSourceDataLoaded].
342343
*/
343344
val MapboxMap.sourceDataLoadedEvents: Flow<SourceDataLoaded>
344345
@JvmSynthetic
345346
get() = callbackFlow<SourceDataLoaded> {
346347
val cancelable = nativeObserver.subscribeSourceDataLoaded(::trySendBlocking, onCancel = channel::close)
347348
awaitClose(cancelable::cancel)
348-
}.flowOn(Dispatchers.Main.immediate)
349+
}.flowOn(Dispatchers.Main.immediate).conflate()
349350

350351
/**
351-
* [Flow] of [StyleImageMissing] updates from [MapboxMap.subscribeStyleImageMissing].
352+
* Conflated [Flow] of [StyleImageMissing] updates from [MapboxMap.subscribeStyleImageMissing].
352353
*/
353354
val MapboxMap.styleImageMissingEvents: Flow<StyleImageMissing>
354355
@JvmSynthetic
355356
get() = callbackFlow<StyleImageMissing> {
356357
val cancelable =
357358
nativeObserver.subscribeStyleImageMissing(::trySendBlocking, onCancel = channel::close)
358359
awaitClose(cancelable::cancel)
359-
}.flowOn(Dispatchers.Main.immediate)
360+
}.flowOn(Dispatchers.Main.immediate).conflate()
360361

361362
/**
362-
* [Flow] of [StyleImageRemoveUnused] updates from [MapboxMap.subscribeStyleImageRemoveUnused].
363+
* Conflated [Flow] of [StyleImageRemoveUnused] updates from [MapboxMap.subscribeStyleImageRemoveUnused].
363364
*/
364365
val MapboxMap.styleImageRemoveUnusedEvents: Flow<StyleImageRemoveUnused>
365366
@JvmSynthetic
366367
get() = callbackFlow<StyleImageRemoveUnused> {
367368
val cancelable =
368369
nativeObserver.subscribeStyleImageRemoveUnused(::trySendBlocking, onCancel = channel::close)
369370
awaitClose(cancelable::cancel)
370-
}.flowOn(Dispatchers.Main.immediate)
371+
}.flowOn(Dispatchers.Main.immediate).conflate()
371372

372373
/**
373-
* [Flow] of [RenderFrameStarted] updates from [MapboxMap.subscribeRenderFrameStarted].
374+
* Conflated [Flow] of [RenderFrameStarted] updates from [MapboxMap.subscribeRenderFrameStarted].
374375
*/
375376
val MapboxMap.renderFrameStartedEvents: Flow<RenderFrameStarted>
376377
@JvmSynthetic
377378
get() = callbackFlow<RenderFrameStarted> {
378379
val cancelable =
379380
nativeObserver.subscribeRenderFrameStarted(::trySendBlocking, onCancel = channel::close)
380381
awaitClose(cancelable::cancel)
381-
}.flowOn(Dispatchers.Main.immediate)
382+
}.flowOn(Dispatchers.Main.immediate).conflate()
382383

383384
/**
384-
* [Flow] of [RenderFrameFinished] updates from [MapboxMap.subscribeRenderFrameFinished].
385+
* Conflated [Flow] of [RenderFrameFinished] updates from [MapboxMap.subscribeRenderFrameFinished].
385386
*/
386387
val MapboxMap.renderFrameFinishedEvents: Flow<RenderFrameFinished>
387388
@JvmSynthetic
388389
get() = callbackFlow<RenderFrameFinished> {
389390
val cancelable =
390391
nativeObserver.subscribeRenderFrameFinished(::trySendBlocking, onCancel = channel::close)
391392
awaitClose(cancelable::cancel)
392-
}.flowOn(Dispatchers.Main.immediate)
393+
}.flowOn(Dispatchers.Main.immediate).conflate()
393394

394395
/**
395-
* [Flow] of [ResourceRequest] updates from [MapboxMap.subscribeResourceRequest].
396+
* Conflated [Flow] of [ResourceRequest] updates from [MapboxMap.subscribeResourceRequest].
396397
*/
397398
val MapboxMap.resourceRequestEvents: Flow<ResourceRequest>
398399
@JvmSynthetic
399400
get() = callbackFlow<ResourceRequest> {
400401
val cancelable = nativeObserver.subscribeResourceRequest(::trySendBlocking, onCancel = channel::close)
401402
awaitClose(cancelable::cancel)
402-
}.flowOn(Dispatchers.Main.immediate)
403+
}.flowOn(Dispatchers.Main.immediate).conflate()
403404

404405
/**
405-
* [Flow] of [GenericEvent] updates from [MapboxMap.subscribeGenericEvent].
406+
* Conflated [Flow] of [GenericEvent] updates from [MapboxMap.subscribeGenericEvent].
406407
*/
407408
@JvmSynthetic
408409
@MapboxExperimental
@@ -411,7 +412,7 @@ fun MapboxMap.genericEvents(eventName: String): Flow<GenericEvent> {
411412
val cancelable =
412413
nativeObserver.subscribeGenericEvent(eventName, ::trySendBlocking, onCancel = channel::close)
413414
awaitClose(cancelable::cancel)
414-
}.flowOn(Dispatchers.Main.immediate)
415+
}.flowOn(Dispatchers.Main.immediate).conflate()
415416
}
416417

417418
/**

0 commit comments

Comments
 (0)