react-native-audio-browser 0.2.0-next.55 → 0.2.0-next.57

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.
@@ -137,7 +137,6 @@ class AudioBrowser : HybridAudioBrowserSpec(), ServiceConnection {
137
137
  if (service.player.getCallbacks() === callbacks) service.player.setCallbacks(null)
138
138
  if (service.player.browser === this) {
139
139
  service.player.browser = null
140
- service.player.forgetBrowserRegistration()
141
140
  // It wraps a JS callback from this runtime: left set, every track change throws into the
142
141
  // now-playing updater's fallback until the next setup().
143
142
  service.player.nowPlaying.formatter = null
@@ -186,6 +185,7 @@ class AudioBrowser : HybridAudioBrowserSpec(), ServiceConnection {
186
185
  ?: throw IllegalStateException("NitroModules.applicationContext is null")
187
186
 
188
187
  // MARK: Browser state
188
+ private var isConfigured = false
189
189
  private var _configuration =
190
190
  NativeBrowserConfiguration(
191
191
  path = null,
@@ -444,7 +444,12 @@ class AudioBrowser : HybridAudioBrowserSpec(), ServiceConnection {
444
444
  // Reaches MediaSession, which asserts the main thread, from this JS-thread setter. Launched,
445
445
  // not blocked: what it triggers can wait on JS callbacks, which cannot run while the JS
446
446
  // thread waits. Queued on mainScope ahead of navigationJob, so the two stay ordered.
447
- launchInScope { connectedService?.player?.notifyBrowserConfigurationReady() }
447
+ launchInScope {
448
+ // Here, not in the setter: onServiceConnected reads it on main, and must not see it before
449
+ // browserManager.config is set.
450
+ isConfigured = true
451
+ connectedService?.player?.notifyBrowserConfigurationReady()
452
+ }
448
453
 
449
454
  clearNavigationError()
450
455
 
@@ -1132,9 +1137,7 @@ class AudioBrowser : HybridAudioBrowserSpec(), ServiceConnection {
1132
1137
  player.browser = this@AudioBrowser
1133
1138
 
1134
1139
  // The configuration setter's notify finds no service when it runs before connect.
1135
- if (_configuration.routes?.isNotEmpty() == true) {
1136
- player.notifyBrowserConfigurationReady()
1137
- }
1140
+ if (isConfigured) player.notifyBrowserConfigurationReady()
1138
1141
  }
1139
1142
 
1140
1143
  connectedService?.onBatteryWarningPendingChanged = onBatteryWarningPending
@@ -160,7 +160,7 @@ internal constructor(
160
160
  /** A network-caused retry is waiting; regaining connectivity retries at once. */
161
161
  @Volatile private var pendingNetworkRetry = false
162
162
 
163
- private var browserRegistered = CompletableDeferred<AudioBrowser>()
163
+ @Volatile private var browserRegistered = CompletableDeferred<AudioBrowser>()
164
164
  private var _coilBitmapLoader: CoilBitmapLoader? = null
165
165
 
166
166
  /** For SVG pre-rendering of Android Auto browse items. Set by the service. */
@@ -187,9 +187,14 @@ internal constructor(
187
187
  _coilBitmapLoader = value
188
188
  }
189
189
 
190
- // Setting this does not release callers already waiting in [awaitBrowser]: call
191
- // [notifyBrowserConfigurationReady] once its routes and tabs are configured.
192
190
  var browser: AudioBrowser? = null
191
+ set(value) {
192
+ // A completed registration is the previous browser's; an open one keeps its waiters.
193
+ if (browserRegistered.isCompleted) {
194
+ browserRegistered = CompletableDeferred()
195
+ }
196
+ field = value
197
+ }
193
198
 
194
199
  /**
195
200
  * Marks the browser's routes and tabs configured: completes [awaitBrowser] and lets Android Auto
@@ -222,31 +227,17 @@ internal constructor(
222
227
  }
223
228
 
224
229
  /**
225
- * Makes [awaitBrowser] wait for a replacement browser instead of returning the disposed one; a
226
- * [CompletableDeferred] completes only once.
227
- */
228
- fun forgetBrowserRegistration() {
229
- if (browserRegistered.isCompleted) {
230
- browserRegistered = CompletableDeferred()
231
- }
232
- }
233
-
234
- /**
235
- * [browser] if set, else the one [notifyBrowserConfigurationReady] registers next.
230
+ * The browser [notifyBrowserConfigurationReady] registered, waiting for it if none is.
236
231
  *
237
232
  * @throws TimeoutCancellationException if it is not registered within 10 seconds
238
233
  */
239
234
  suspend fun awaitBrowser(): AudioBrowser =
240
- browser
241
- ?: try {
242
- withTimeout(10.seconds) {
243
- Timber.d("Waiting for browser registration...")
244
- browserRegistered.await()
245
- }
246
- } catch (e: TimeoutCancellationException) {
247
- Timber.e("Timed out waiting for browser registration (10s)")
248
- throw e
249
- }
235
+ try {
236
+ withTimeout(10.seconds) { browserRegistered.await() }
237
+ } catch (e: TimeoutCancellationException) {
238
+ Timber.e("Timed out waiting for browser registration (10s)")
239
+ throw e
240
+ }
250
241
 
251
242
  private lateinit var playerListener: PlayerListener
252
243
  private var cache: SimpleCache? = null
@@ -0,0 +1,62 @@
1
+ package com.audiobrowser.player
2
+
3
+ import com.audiobrowser.AudioBrowser
4
+ import kotlinx.coroutines.async
5
+ import kotlinx.coroutines.test.runTest
6
+ import kotlinx.coroutines.yield
7
+ import org.junit.Assert.assertFalse
8
+ import org.junit.Assert.assertSame
9
+ import org.junit.Test
10
+ import org.junit.runner.RunWith
11
+ import org.mockito.Mockito.mock
12
+ import org.robolectric.RobolectricTestRunner
13
+ import org.robolectric.RuntimeEnvironment
14
+
15
+ @RunWith(RobolectricTestRunner::class)
16
+ class PlayerBrowserTest {
17
+ private val player = Player(RuntimeEnvironment.getApplication())
18
+
19
+ @Test
20
+ fun `a browser connected but not yet configured is awaited until it is`() = runTest {
21
+ val browser = mock(AudioBrowser::class.java)
22
+ player.browser = browser
23
+
24
+ val awaited = async { player.awaitBrowser() }
25
+ yield()
26
+ assertFalse(awaited.isCompleted)
27
+
28
+ player.notifyBrowserConfigurationReady()
29
+ assertSame(browser, awaited.await())
30
+ }
31
+
32
+ @Test
33
+ fun `a browser that replaces a configured one is awaited until it is configured`() = runTest {
34
+ player.browser = mock(AudioBrowser::class.java)
35
+ player.notifyBrowserConfigurationReady()
36
+ val replacement = mock(AudioBrowser::class.java)
37
+ player.browser = replacement
38
+
39
+ val awaited = async { player.awaitBrowser() }
40
+ yield()
41
+ assertFalse(awaited.isCompleted)
42
+
43
+ player.notifyBrowserConfigurationReady()
44
+ assertSame(replacement, awaited.await())
45
+ }
46
+
47
+ @Test
48
+ fun `a cleared browser is not served to callers waiting for its replacement`() = runTest {
49
+ player.browser = mock(AudioBrowser::class.java)
50
+ player.notifyBrowserConfigurationReady()
51
+ player.browser = null
52
+
53
+ val awaited = async { player.awaitBrowser() }
54
+ yield()
55
+ assertFalse(awaited.isCompleted)
56
+
57
+ val replacement = mock(AudioBrowser::class.java)
58
+ player.browser = replacement
59
+ player.notifyBrowserConfigurationReady()
60
+ assertSame(replacement, awaited.await())
61
+ }
62
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-native-audio-browser",
3
- "version": "0.2.0-next.55",
3
+ "version": "0.2.0-next.57",
4
4
  "description": "react-native-audio-browser is a react native package built with Nitro",
5
5
  "keywords": [
6
6
  "react-native",