react-native-audio-browser 0.2.0-next.59 → 0.2.0-next.60
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.
- package/android/src/main/java/com/audiobrowser/AudioBrowser.kt +5 -0
- package/android/src/main/java/com/audiobrowser/browser/BrowserManager.kt +37 -10
- package/android/src/test/java/com/audiobrowser/browser/BrowserManagerInvalidationTest.kt +158 -0
- package/ios/Browser/BrowserManager.swift +12 -4
- package/package.json +1 -1
|
@@ -440,6 +440,7 @@ class AudioBrowser : HybridAudioBrowserSpec(), ServiceConnection {
|
|
|
440
440
|
set(value) {
|
|
441
441
|
_configuration = value
|
|
442
442
|
browserManager.config = buildConfig()
|
|
443
|
+
browserManager.clearContentCache()
|
|
443
444
|
|
|
444
445
|
// Reaches MediaSession, which asserts the main thread, from this JS-thread setter. Launched,
|
|
445
446
|
// not blocked: what it triggers can wait on JS callbacks, which cannot run while the JS
|
|
@@ -702,6 +703,10 @@ class AudioBrowser : HybridAudioBrowserSpec(), ServiceConnection {
|
|
|
702
703
|
// MediaSession asserts the main thread; this override runs on the JS thread.
|
|
703
704
|
launchInScope { connectedService?.player?.invalidateAllContent() }
|
|
704
705
|
|
|
706
|
+
mainScope.launch {
|
|
707
|
+
runCatching { browserManager.queryTabs() }
|
|
708
|
+
.onFailure { Timber.e(it, "Re-querying tabs failed") }
|
|
709
|
+
}
|
|
705
710
|
mainScope.launch { browserManager.refresh() }
|
|
706
711
|
}
|
|
707
712
|
|
|
@@ -81,6 +81,13 @@ class BrowserManager {
|
|
|
81
81
|
|
|
82
82
|
private val contentCache = LruCache<String, ResolvedTrack>(20)
|
|
83
83
|
|
|
84
|
+
// Moves on clearContentCache, so a fetch that outlives one caches nothing. Separate from
|
|
85
|
+
// layerGeneration, which also moves on every path change. Car browses run on IO threads and the
|
|
86
|
+
// clear on the JS thread: each check and its write hold [cacheLock].
|
|
87
|
+
@Volatile private var contentGeneration = 0
|
|
88
|
+
private var tabsGeneration = -1
|
|
89
|
+
private val cacheLock = Any()
|
|
90
|
+
|
|
84
91
|
// Query and results stored as one pair, so a concurrent reader never matches one query with
|
|
85
92
|
// another query's results.
|
|
86
93
|
@Volatile private var lastSearch: Pair<String, Array<Track>>? = null
|
|
@@ -446,9 +453,12 @@ class BrowserManager {
|
|
|
446
453
|
Timber.d("Content cache MISS for path='$normalizedPath'")
|
|
447
454
|
}
|
|
448
455
|
|
|
456
|
+
val generation = contentGeneration
|
|
449
457
|
val resolvedTrack = resolveUncached(normalizedPath)
|
|
450
458
|
|
|
451
|
-
|
|
459
|
+
synchronized(cacheLock) {
|
|
460
|
+
if (generation == contentGeneration) contentCache.put(normalizedPath, resolvedTrack)
|
|
461
|
+
}
|
|
452
462
|
|
|
453
463
|
cacheChildren(resolvedTrack)
|
|
454
464
|
|
|
@@ -466,11 +476,15 @@ class BrowserManager {
|
|
|
466
476
|
|
|
467
477
|
/**
|
|
468
478
|
* Clears every cached page and the last search, which nothing else evicts, and re-resolves the
|
|
469
|
-
* resolver layers and "view more" title on the next request.
|
|
479
|
+
* tabs, resolver layers and "view more" title on the next request. A fetch in flight caches
|
|
480
|
+
* nothing.
|
|
470
481
|
*/
|
|
471
482
|
fun clearContentCache() {
|
|
472
|
-
|
|
473
|
-
|
|
483
|
+
synchronized(cacheLock) {
|
|
484
|
+
contentGeneration += 1
|
|
485
|
+
contentCache.evictAll()
|
|
486
|
+
lastSearch = null
|
|
487
|
+
}
|
|
474
488
|
layerGeneration += 1
|
|
475
489
|
artworkResolutions.clear()
|
|
476
490
|
onArtworkRegistriesCleared?.invoke()
|
|
@@ -776,6 +790,7 @@ class BrowserManager {
|
|
|
776
790
|
Timber.d("Executing fresh search for: ${params.query} (mode=${params.mode})")
|
|
777
791
|
|
|
778
792
|
val searchPath = BrowserPathHelper.createSearchPath(params.query)
|
|
793
|
+
val generation = contentGeneration
|
|
779
794
|
|
|
780
795
|
try {
|
|
781
796
|
// Search results skip validateTrack, so one without a path or src is dropped here:
|
|
@@ -817,7 +832,9 @@ class BrowserManager {
|
|
|
817
832
|
icyCharset = null,
|
|
818
833
|
)
|
|
819
834
|
|
|
820
|
-
|
|
835
|
+
synchronized(cacheLock) {
|
|
836
|
+
if (generation == contentGeneration) lastSearch = params.query to searchResults
|
|
837
|
+
}
|
|
821
838
|
|
|
822
839
|
cacheChildren(searchResolvedTrack)
|
|
823
840
|
Timber.d(
|
|
@@ -864,11 +881,16 @@ class BrowserManager {
|
|
|
864
881
|
onArtworkRegistriesCleared = callback
|
|
865
882
|
}
|
|
866
883
|
|
|
867
|
-
/**
|
|
884
|
+
/**
|
|
885
|
+
* Resolves the tabs once per [clearContentCache]; later calls return that result. The previous
|
|
886
|
+
* tabs stay until new ones resolve.
|
|
887
|
+
*/
|
|
868
888
|
suspend fun queryTabs(): Array<Track> {
|
|
869
|
-
|
|
870
|
-
|
|
871
|
-
|
|
889
|
+
val generation = contentGeneration
|
|
890
|
+
synchronized(cacheLock) { tabs.takeIf { tabsGeneration == generation } }
|
|
891
|
+
?.let {
|
|
892
|
+
return it
|
|
893
|
+
}
|
|
872
894
|
|
|
873
895
|
Timber.d("Getting navigation tabs")
|
|
874
896
|
|
|
@@ -879,7 +901,12 @@ class BrowserManager {
|
|
|
879
901
|
Timber.d("[TABS] Tab[$index] '${tab.title}': path=${tab.path}")
|
|
880
902
|
}
|
|
881
903
|
|
|
882
|
-
|
|
904
|
+
synchronized(cacheLock) {
|
|
905
|
+
if (generation == contentGeneration) {
|
|
906
|
+
this.tabs = tabs
|
|
907
|
+
tabsGeneration = generation
|
|
908
|
+
}
|
|
909
|
+
}
|
|
883
910
|
return tabs
|
|
884
911
|
}
|
|
885
912
|
|
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
package com.audiobrowser.browser
|
|
2
|
+
|
|
3
|
+
import com.audiobrowser.TestFixtures.transformableConfig
|
|
4
|
+
import com.margelo.nitro.audiobrowser.NativeRouteEntry
|
|
5
|
+
import java.util.concurrent.CountDownLatch
|
|
6
|
+
import java.util.concurrent.TimeUnit
|
|
7
|
+
import kotlinx.coroutines.Dispatchers
|
|
8
|
+
import kotlinx.coroutines.async
|
|
9
|
+
import kotlinx.coroutines.runBlocking
|
|
10
|
+
import okhttp3.mockwebserver.Dispatcher
|
|
11
|
+
import okhttp3.mockwebserver.MockResponse
|
|
12
|
+
import okhttp3.mockwebserver.MockWebServer
|
|
13
|
+
import okhttp3.mockwebserver.RecordedRequest
|
|
14
|
+
import org.junit.After
|
|
15
|
+
import org.junit.Assert.assertEquals
|
|
16
|
+
import org.junit.Assert.assertNull
|
|
17
|
+
import org.junit.Assert.assertTrue
|
|
18
|
+
import org.junit.Before
|
|
19
|
+
import org.junit.Test
|
|
20
|
+
import org.junit.runner.RunWith
|
|
21
|
+
import org.robolectric.RobolectricTestRunner
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* clearContentCache (invalidateAllContent) against an HTTP backend whose titles are in the current
|
|
25
|
+
* [language]. A held request reads the language when it arrives, so it answers in the one before
|
|
26
|
+
* the clear.
|
|
27
|
+
*/
|
|
28
|
+
@RunWith(RobolectricTestRunner::class)
|
|
29
|
+
class BrowserManagerInvalidationTest {
|
|
30
|
+
|
|
31
|
+
private lateinit var server: MockWebServer
|
|
32
|
+
private lateinit var browserManager: BrowserManager
|
|
33
|
+
|
|
34
|
+
@Volatile private var language = "en"
|
|
35
|
+
@Volatile private var hold: CountDownLatch? = null
|
|
36
|
+
@Volatile private var failing = false
|
|
37
|
+
private val arrived = CountDownLatch(1)
|
|
38
|
+
|
|
39
|
+
@Before
|
|
40
|
+
fun setup() {
|
|
41
|
+
server = MockWebServer()
|
|
42
|
+
server.dispatcher =
|
|
43
|
+
object : Dispatcher() {
|
|
44
|
+
override fun dispatch(request: RecordedRequest): MockResponse {
|
|
45
|
+
val title = language
|
|
46
|
+
hold?.let {
|
|
47
|
+
arrived.countDown()
|
|
48
|
+
it.await(5, TimeUnit.SECONDS)
|
|
49
|
+
}
|
|
50
|
+
if (failing) return MockResponse().setResponseCode(500)
|
|
51
|
+
val path = request.path.orEmpty()
|
|
52
|
+
val body =
|
|
53
|
+
when {
|
|
54
|
+
path.startsWith("/search") -> """[{"title":"$title","src":"https://s/a.mp3"}]"""
|
|
55
|
+
path.contains("tabs") ->
|
|
56
|
+
"""{"path":"/tabs","title":"Tabs","children":[{"title":"$title","path":"/t"}]}"""
|
|
57
|
+
else -> """{"path":"/page","title":"$title"}"""
|
|
58
|
+
}
|
|
59
|
+
return MockResponse().setBody(body)
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
server.start()
|
|
63
|
+
browserManager = BrowserManager()
|
|
64
|
+
browserManager.config = config()
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
private fun config() =
|
|
68
|
+
BrowserConfig(
|
|
69
|
+
request = transformableConfig(baseUrl = server.url("/").toString().trimEnd('/')),
|
|
70
|
+
routes =
|
|
71
|
+
arrayOf(
|
|
72
|
+
apiRoute(BrowserManager.TABS_ROUTE_PATH, browse = true),
|
|
73
|
+
apiRoute(BrowserManager.SEARCH_ROUTE_PATH, browse = false),
|
|
74
|
+
),
|
|
75
|
+
)
|
|
76
|
+
|
|
77
|
+
@After
|
|
78
|
+
fun tearDown() {
|
|
79
|
+
hold?.countDown()
|
|
80
|
+
server.shutdown()
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
private fun apiRoute(path: String, browse: Boolean) =
|
|
84
|
+
NativeRouteEntry(
|
|
85
|
+
path = path,
|
|
86
|
+
browseCallback = null,
|
|
87
|
+
browseConfig = if (browse) transformableConfig() else null,
|
|
88
|
+
browseStatic = null,
|
|
89
|
+
searchCallback = null,
|
|
90
|
+
searchConfig = if (browse) null else transformableConfig(path = "/search"),
|
|
91
|
+
media = null,
|
|
92
|
+
artwork = null,
|
|
93
|
+
)
|
|
94
|
+
|
|
95
|
+
/** Runs [request] with the server holding its response across [meanwhile]. */
|
|
96
|
+
private fun <T> across(
|
|
97
|
+
meanwhile: () -> Unit = { browserManager.clearContentCache() },
|
|
98
|
+
request: suspend () -> T,
|
|
99
|
+
): Unit = runBlocking {
|
|
100
|
+
val release = CountDownLatch(1)
|
|
101
|
+
hold = release
|
|
102
|
+
val inFlight = async(Dispatchers.Default) { request() }
|
|
103
|
+
assertTrue(arrived.await(5, TimeUnit.SECONDS))
|
|
104
|
+
language = "nl"
|
|
105
|
+
meanwhile()
|
|
106
|
+
hold = null
|
|
107
|
+
release.countDown()
|
|
108
|
+
inFlight.await()
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
@Test
|
|
112
|
+
fun `a page in flight across clearContentCache is not cached`() {
|
|
113
|
+
across { browserManager.resolve("/page") }
|
|
114
|
+
|
|
115
|
+
assertEquals("nl", runBlocking { browserManager.resolve("/page") }.title)
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
@Test
|
|
119
|
+
fun `a search in flight across clearContentCache is not kept`() {
|
|
120
|
+
across { browserManager.search("jazz") }
|
|
121
|
+
|
|
122
|
+
assertNull(browserManager.getCachedSearchResults("jazz"))
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
@Test
|
|
126
|
+
fun `clearContentCache re-resolves the tabs`() = runBlocking {
|
|
127
|
+
assertEquals("en", browserManager.queryTabs().single().title)
|
|
128
|
+
|
|
129
|
+
language = "nl"
|
|
130
|
+
browserManager.clearContentCache()
|
|
131
|
+
|
|
132
|
+
assertEquals("nl", browserManager.queryTabs().single().title)
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
@Test
|
|
136
|
+
fun `tabs in flight across clearContentCache are not kept`() {
|
|
137
|
+
across { browserManager.queryTabs() }
|
|
138
|
+
|
|
139
|
+
assertNull(browserManager.getTabs())
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
@Test
|
|
143
|
+
fun `a page in flight across a config change is cached`() {
|
|
144
|
+
across({ browserManager.config = config() }) { browserManager.resolve("/page") }
|
|
145
|
+
|
|
146
|
+
assertEquals("en", runBlocking { browserManager.resolve("/page") }.title)
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
@Test
|
|
150
|
+
fun `a failed tabs re-query keeps the previous tabs`() = runBlocking {
|
|
151
|
+
browserManager.queryTabs()
|
|
152
|
+
browserManager.clearContentCache()
|
|
153
|
+
failing = true
|
|
154
|
+
|
|
155
|
+
assertTrue(runCatching { browserManager.queryTabs() }.isFailure)
|
|
156
|
+
assertEquals("en", browserManager.getTabs()?.single()?.title)
|
|
157
|
+
}
|
|
158
|
+
}
|
|
@@ -18,6 +18,8 @@ final class BrowserManager {
|
|
|
18
18
|
|
|
19
19
|
// Entries are unhydrated; `resolve` hydrates favorites on the way out.
|
|
20
20
|
private let contentCache = LRUCache<String, ResolvedTrack>(maxSize: 20)
|
|
21
|
+
// Moves on `invalidateAll`, so a fetch that outlives one caches nothing.
|
|
22
|
+
private var contentGeneration = 0
|
|
21
23
|
|
|
22
24
|
// Raw results, never hydrated ones: `FavoriteSet.hydrate` skips a track whose `favorited` is
|
|
23
25
|
// already set, so a hydrated slot would keep the favorites of the first search.
|
|
@@ -143,9 +145,10 @@ final class BrowserManager {
|
|
|
143
145
|
return favorites.hydrate(cached)
|
|
144
146
|
}
|
|
145
147
|
|
|
148
|
+
let generation = contentGeneration
|
|
146
149
|
let resolvedTrack = try await pages.resolve(normalizedPath)
|
|
147
150
|
|
|
148
|
-
contentCache.set(normalizedPath, value: resolvedTrack)
|
|
151
|
+
if generation == contentGeneration { contentCache.set(normalizedPath, value: resolvedTrack) }
|
|
149
152
|
|
|
150
153
|
return favorites.hydrate(resolvedTrack)
|
|
151
154
|
}
|
|
@@ -165,6 +168,7 @@ final class BrowserManager {
|
|
|
165
168
|
/// a failed tabs query keeps the current tabs.
|
|
166
169
|
func invalidateAll(reloadFailed: @escaping @MainActor (Error, String) async -> Void) {
|
|
167
170
|
contentCache.clear()
|
|
171
|
+
contentGeneration &+= 1
|
|
168
172
|
requests.invalidateLayers()
|
|
169
173
|
lastSearchKey = nil
|
|
170
174
|
lastSearchResults = nil
|
|
@@ -206,10 +210,13 @@ final class BrowserManager {
|
|
|
206
210
|
return makeSearchResult(query: query, results: results)
|
|
207
211
|
}
|
|
208
212
|
|
|
213
|
+
let generation = contentGeneration
|
|
209
214
|
let results = try await pages.search(params)
|
|
210
215
|
|
|
211
|
-
|
|
212
|
-
|
|
216
|
+
if generation == contentGeneration {
|
|
217
|
+
lastSearchKey = cacheKey
|
|
218
|
+
lastSearchResults = results
|
|
219
|
+
}
|
|
213
220
|
|
|
214
221
|
return makeSearchResult(query: query, results: results)
|
|
215
222
|
}
|
|
@@ -256,8 +263,9 @@ final class BrowserManager {
|
|
|
256
263
|
// MARK: - Tabs
|
|
257
264
|
|
|
258
265
|
func queryTabs() async throws -> [Track] {
|
|
266
|
+
let generation = contentGeneration
|
|
259
267
|
guard let tabs = try await pages.tabs() else { return [] }
|
|
260
|
-
self.tabs = tabs
|
|
268
|
+
if generation == contentGeneration { self.tabs = tabs }
|
|
261
269
|
return tabs
|
|
262
270
|
}
|
|
263
271
|
|