Skip to content

Commit 85c0f7c

Browse files
authoredMar 7, 2025··
Fix a race condition when initializing the EPUB navigator (#622)
1 parent 37c75aa commit 85c0f7c

File tree

3 files changed

+48
-17
lines changed

3 files changed

+48
-17
lines changed
 

‎CHANGELOG.md

+11-1
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,22 @@ All notable changes to this project will be documented in this file. Take a look
44

55
**Warning:** Features marked as *experimental* may change or be removed in a future release without notice. Use with caution.
66

7-
<!-- ## [Unreleased] -->
7+
## [Unreleased]
8+
9+
### Fixed
10+
11+
#### Navigator
12+
13+
* Fixed a race condition causing EPUB decorations to be applied twice when opening a publication.
14+
15+
816

917
## [3.0.3]
1018

1119
### Fixed
1220

21+
#### LCP
22+
1323
* Fixed `IllegalArgumentException` when trying to decrypt the end of a `CbcLcpResource`.
1424

1525

‎readium/navigator/src/main/java/org/readium/r2/navigator/epub/EpubNavigatorFragment.kt

+3-1
Original file line numberDiff line numberDiff line change
@@ -672,11 +672,13 @@ public class EpubNavigatorFragment internal constructor(
672672
RunScriptCommand.Scope.LoadedResources -> {
673673
r2PagerAdapter?.mFragments?.forEach { _, fragment ->
674674
(fragment as? R2EpubPageFragment)
675+
?.takeIf { it.isLoaded.value }
675676
?.runJavaScript(command.script)
676677
}
677678
}
678-
is RunScriptCommand.Scope.Resource -> {
679+
is RunScriptCommand.Scope.LoadedResource -> {
679680
loadedFragmentForHref(command.scope.href)
681+
?.takeIf { it.isLoaded.value }
680682
?.runJavaScript(command.script)
681683
}
682684
is RunScriptCommand.Scope.WebView -> {

‎readium/navigator/src/main/java/org/readium/r2/navigator/epub/EpubNavigatorViewModel.kt

+34-15
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ internal class EpubNavigatorViewModel(
6262
sealed class Scope {
6363
object CurrentResource : Scope()
6464
object LoadedResources : Scope()
65-
data class Resource(val href: Url) : Scope()
65+
data class LoadedResource(val href: Url) : Scope()
6666
data class WebView(val webView: R2BasicWebView) : Scope()
6767
}
6868
}
@@ -145,22 +145,39 @@ internal class EpubNavigatorViewModel(
145145
.launchIn(viewModelScope)
146146
}
147147

148-
fun onResourceLoaded(webView: R2BasicWebView, link: Link): RunScriptCommand {
149-
val templates = decorationTemplates.toJSON().toString()
150-
.replace("\\n", " ")
151-
var script = "readium.registerDecorationTemplates($templates);\n"
148+
fun onResourceLoaded(webView: R2BasicWebView, link: Link): List<RunScriptCommand> =
149+
buildList {
150+
val scope = RunScriptCommand.Scope.WebView(webView)
152151

153-
for ((group, decorations) in decorations) {
154-
val changes = decorations
155-
.filter { it.locator.href == link.url() }
156-
.map { DecorationChange.Added(it) }
152+
// Applies the Readium CSS properties in case they changed since they were injected
153+
// in the HTML document.
154+
val properties = css.value.run {
155+
rsProperties.toCssProperties() + userProperties.toCssProperties()
156+
}
157157

158-
val groupScript = changes.javascriptForGroup(group, decorationTemplates) ?: continue
159-
script += "$groupScript\n"
160-
}
158+
add(
159+
RunScriptCommand(
160+
script = "readium.setCSSProperties(${JSONObject(properties.toMap())});",
161+
scope = scope
162+
)
163+
)
161164

162-
return RunScriptCommand(script, scope = RunScriptCommand.Scope.WebView(webView))
163-
}
165+
// Applies the decorations.
166+
val templates = decorationTemplates.toJSON().toString()
167+
.replace("\\n", " ")
168+
var script = "readium.registerDecorationTemplates($templates);\n"
169+
170+
for ((group, decorations) in decorations) {
171+
val changes = decorations
172+
.filter { it.locator.href == link.url() }
173+
.map { DecorationChange.Added(it) }
174+
175+
val groupScript = changes.javascriptForGroup(group, decorationTemplates) ?: continue
176+
script += "$groupScript\n"
177+
}
178+
179+
add(RunScriptCommand(script, scope = scope))
180+
}
164181

165182
// Serving resources
166183

@@ -296,7 +313,9 @@ internal class EpubNavigatorViewModel(
296313
} else {
297314
for ((href, changes) in source.changesByHref(target)) {
298315
val script = changes.javascriptForGroup(group, decorationTemplates) ?: continue
299-
cmds.add(RunScriptCommand(script, scope = RunScriptCommand.Scope.Resource(href)))
316+
cmds.add(
317+
RunScriptCommand(script, scope = RunScriptCommand.Scope.LoadedResource(href))
318+
)
300319
}
301320
}
302321

0 commit comments

Comments
 (0)
Please sign in to comment.