react-native-audio-browser 0.2.0-next.65 → 0.2.0-next.66

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -95,11 +95,14 @@ import kotlin.time.Duration.Companion.seconds
95
95
  import kotlinx.coroutines.CancellationException
96
96
  import kotlinx.coroutines.CompletableDeferred
97
97
  import kotlinx.coroutines.CoroutineScope
98
+ import kotlinx.coroutines.CoroutineStart
99
+ import kotlinx.coroutines.DelicateCoroutinesApi
98
100
  import kotlinx.coroutines.Job
99
101
  import kotlinx.coroutines.MainScope
100
102
  import kotlinx.coroutines.async
101
103
  import kotlinx.coroutines.cancel
102
104
  import kotlinx.coroutines.cancelChildren
105
+ import kotlinx.coroutines.ensureActive
103
106
  import kotlinx.coroutines.launch
104
107
  import kotlinx.coroutines.runBlocking
105
108
  import kotlinx.coroutines.withTimeoutOrNull
@@ -230,7 +233,7 @@ class AudioBrowser : HybridAudioBrowserSpec(), ServiceConnection {
230
233
  post { onBatteryWarningPendingChanged(BatteryWarningPendingChangedEvent(pending)) }
231
234
  }
232
235
  // Every setupPlayer waiting for the service awaits it. Replaced on disconnect, after
233
- // cancelChildren has rejected its waiters.
236
+ // cancelChildren has rejected its waiters, and on a dead binding, after failing them.
234
237
  private var connectionReady = CompletableDeferred<Unit>()
235
238
 
236
239
  // Staged by setup options or by setters called before the player exists; last write wins, and
@@ -659,7 +662,7 @@ class AudioBrowser : HybridAudioBrowserSpec(), ServiceConnection {
659
662
  }
660
663
 
661
664
  override fun search(query: String): Promise<Array<Track>> {
662
- return Promise.async(mainScope) {
665
+ return promise {
663
666
  Timber.d("Searching for: $query")
664
667
  // Rejects with the error a browse would raise but leaves the navigation error unset: a failed
665
668
  // search must not replace the displayed page's state (mirrors iOS HybridAudioBrowser.search).
@@ -776,7 +779,7 @@ class AudioBrowser : HybridAudioBrowserSpec(), ServiceConnection {
776
779
  // ============================================================================
777
780
 
778
781
  override fun setupPlayer(options: NativeSetupPlayerOptions): Promise<Unit> {
779
- return Promise.async(mainScope) {
782
+ return promise {
780
783
  setupOptions.update(options)
781
784
  // Staged to apply together with the engine below, or at service connect. Last write wins
782
785
  // against the imperative setters.
@@ -788,7 +791,7 @@ class AudioBrowser : HybridAudioBrowserSpec(), ServiceConnection {
788
791
  it.player.applyOptions(updateOptions)
789
792
  it.player.setup(setupOptions)
790
793
  applyPendingPlayerState(it.player)
791
- return@async
794
+ return@promise
792
795
  }
793
796
 
794
797
  if (!serviceBinding.bind(Intent(context, Service::class.java), this@AudioBrowser)) {
@@ -1165,10 +1168,27 @@ class AudioBrowser : HybridAudioBrowserSpec(), ServiceConnection {
1165
1168
  Timber.d("AudioBrowser.onServiceDisconnected()")
1166
1169
  }
1167
1170
 
1171
+ // Android never reconnects a dead binding: release it so the next setupPlayer binds afresh.
1172
+ override fun onBindingDied(name: ComponentName) = dropDeadBinding()
1173
+
1174
+ override fun onNullBinding(name: ComponentName) = dropDeadBinding()
1175
+
1176
+ private fun dropDeadBinding() {
1177
+ serviceBinding.unbind(this)
1178
+ connectionReady.completeExceptionally(IllegalStateException("AudioBrowserService binding died"))
1179
+ connectionReady = CompletableDeferred()
1180
+ }
1181
+
1168
1182
  // ============================================================================
1169
1183
  // MARK: Internal Helpers
1170
1184
  // ============================================================================
1171
1185
 
1186
+ private fun <T> promise(run: suspend () -> T): Promise<T> {
1187
+ val promise = Promise<T>()
1188
+ mainScope.launchSettled({ it.fold(promise::resolve, promise::reject) }, run)
1189
+ return promise
1190
+ }
1191
+
1172
1192
  private fun launchInScope(block: suspend () -> Unit) {
1173
1193
  mainScope.launch { block() }
1174
1194
  }
@@ -1554,6 +1574,22 @@ internal suspend fun formattedOrDefault(
1554
1574
  return withTimeoutOrNull(FORMATTER_TIMEOUT) { hop.await() } ?: default
1555
1575
  }
1556
1576
 
1577
+ /**
1578
+ * Runs [run] in this scope and hands its outcome to [settle] once. Unlike Nitro's `Promise.async`,
1579
+ * a scope cancelled before [run] starts still settles, with the cancellation, and [run] never runs.
1580
+ */
1581
+ @OptIn(DelicateCoroutinesApi::class)
1582
+ internal fun <T> CoroutineScope.launchSettled(settle: (Result<T>) -> Unit, run: suspend () -> T) {
1583
+ launch(start = CoroutineStart.ATOMIC) {
1584
+ settle(
1585
+ runCatching {
1586
+ ensureActive()
1587
+ run()
1588
+ }
1589
+ )
1590
+ }
1591
+ }
1592
+
1557
1593
  private fun httpStatusText(statusCode: Int): String {
1558
1594
  return when (statusCode) {
1559
1595
  400 -> "Bad Request"
@@ -0,0 +1,81 @@
1
+ package com.audiobrowser
2
+
3
+ import kotlinx.coroutines.CancellationException
4
+ import kotlinx.coroutines.CompletableDeferred
5
+ import kotlinx.coroutines.CoroutineScope
6
+ import kotlinx.coroutines.ExperimentalCoroutinesApi
7
+ import kotlinx.coroutines.cancel
8
+ import kotlinx.coroutines.test.StandardTestDispatcher
9
+ import kotlinx.coroutines.test.advanceUntilIdle
10
+ import kotlinx.coroutines.test.runTest
11
+ import org.junit.Assert.assertEquals
12
+ import org.junit.Assert.assertFalse
13
+ import org.junit.Assert.assertTrue
14
+ import org.junit.Test
15
+
16
+ @OptIn(ExperimentalCoroutinesApi::class)
17
+ class LaunchSettledTest {
18
+
19
+ @Test
20
+ fun `a call into a cancelled scope settles without running`() = runTest {
21
+ val scope = CoroutineScope(StandardTestDispatcher(testScheduler)).apply { cancel() }
22
+ val outcomes = mutableListOf<Result<Int>>()
23
+ var ran = false
24
+
25
+ scope.launchSettled({ outcomes += it }) {
26
+ ran = true
27
+ 1
28
+ }
29
+ advanceUntilIdle()
30
+
31
+ assertFalse(ran)
32
+ assertEquals(1, outcomes.size)
33
+ assertTrue(outcomes.single().exceptionOrNull() is CancellationException)
34
+ }
35
+
36
+ @Test
37
+ fun `a call whose scope is cancelled before it is dispatched settles without running`() =
38
+ runTest {
39
+ val scope = CoroutineScope(StandardTestDispatcher(testScheduler))
40
+ val outcomes = mutableListOf<Result<Int>>()
41
+ var ran = false
42
+
43
+ scope.launchSettled({ outcomes += it }) {
44
+ ran = true
45
+ 1
46
+ }
47
+ scope.cancel()
48
+ advanceUntilIdle()
49
+
50
+ assertFalse(ran)
51
+ assertEquals(1, outcomes.size)
52
+ assertTrue(outcomes.single().exceptionOrNull() is CancellationException)
53
+ }
54
+
55
+ @Test
56
+ fun `a call cancelled while it waits settles once`() = runTest {
57
+ val scope = CoroutineScope(StandardTestDispatcher(testScheduler))
58
+ val outcomes = mutableListOf<Result<Unit>>()
59
+
60
+ scope.launchSettled({ outcomes += it }) { CompletableDeferred<Unit>().await() }
61
+ advanceUntilIdle()
62
+ scope.cancel()
63
+ advanceUntilIdle()
64
+
65
+ assertEquals(1, outcomes.size)
66
+ assertTrue(outcomes.single().exceptionOrNull() is CancellationException)
67
+ }
68
+
69
+ @Test
70
+ fun `a call settles with what it returns or throws`() = runTest {
71
+ val scope = CoroutineScope(StandardTestDispatcher(testScheduler))
72
+ val outcomes = mutableListOf<Result<Int>>()
73
+ val boom = IllegalStateException("boom")
74
+
75
+ scope.launchSettled({ outcomes += it }) { 1 }
76
+ scope.launchSettled({ outcomes += it }) { throw boom }
77
+ advanceUntilIdle()
78
+
79
+ assertEquals(listOf(Result.success(1), Result.failure<Int>(boom)), outcomes)
80
+ }
81
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-native-audio-browser",
3
- "version": "0.2.0-next.65",
3
+ "version": "0.2.0-next.66",
4
4
  "description": "react-native-audio-browser is a react native package built with Nitro",
5
5
  "keywords": [
6
6
  "react-native",