Skip to content

Fix a race condition when initializing the EPUB navigator #622

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,22 @@ All notable changes to this project will be documented in this file. Take a look

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

<!-- ## [Unreleased] -->
## [Unreleased]

### Fixed

#### Navigator

* Fixed a race condition causing EPUB decorations to be applied twice when opening a publication.



## [3.0.3]

### Fixed

#### LCP

* Fixed `IllegalArgumentException` when trying to decrypt the end of a `CbcLcpResource`.


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -672,11 +672,13 @@ public class EpubNavigatorFragment internal constructor(
RunScriptCommand.Scope.LoadedResources -> {
r2PagerAdapter?.mFragments?.forEach { _, fragment ->
(fragment as? R2EpubPageFragment)
?.takeIf { it.isLoaded.value }
?.runJavaScript(command.script)
}
}
is RunScriptCommand.Scope.Resource -> {
is RunScriptCommand.Scope.LoadedResource -> {
loadedFragmentForHref(command.scope.href)
?.takeIf { it.isLoaded.value }
?.runJavaScript(command.script)
}
is RunScriptCommand.Scope.WebView -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ internal class EpubNavigatorViewModel(
sealed class Scope {
object CurrentResource : Scope()
object LoadedResources : Scope()
data class Resource(val href: Url) : Scope()
data class LoadedResource(val href: Url) : Scope()
data class WebView(val webView: R2BasicWebView) : Scope()
}
}
Expand Down Expand Up @@ -145,22 +145,39 @@ internal class EpubNavigatorViewModel(
.launchIn(viewModelScope)
}

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

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

val groupScript = changes.javascriptForGroup(group, decorationTemplates) ?: continue
script += "$groupScript\n"
}
add(
RunScriptCommand(
script = "readium.setCSSProperties(${JSONObject(properties.toMap())});",
scope = scope
)
)

return RunScriptCommand(script, scope = RunScriptCommand.Scope.WebView(webView))
}
// Applies the decorations.
val templates = decorationTemplates.toJSON().toString()
.replace("\\n", " ")
var script = "readium.registerDecorationTemplates($templates);\n"

for ((group, decorations) in decorations) {
val changes = decorations
.filter { it.locator.href == link.url() }
.map { DecorationChange.Added(it) }

val groupScript = changes.javascriptForGroup(group, decorationTemplates) ?: continue
script += "$groupScript\n"
}

add(RunScriptCommand(script, scope = scope))
}

// Serving resources

Expand Down Expand Up @@ -296,7 +313,9 @@ internal class EpubNavigatorViewModel(
} else {
for ((href, changes) in source.changesByHref(target)) {
val script = changes.javascriptForGroup(group, decorationTemplates) ?: continue
cmds.add(RunScriptCommand(script, scope = RunScriptCommand.Scope.Resource(href)))
cmds.add(
RunScriptCommand(script, scope = RunScriptCommand.Scope.LoadedResource(href))
)
}
}

Expand Down