From 66e5acd785d09871244db7da1acc821ce24c412c Mon Sep 17 00:00:00 2001 From: Him188 Date: Tue, 24 Sep 2024 10:37:58 +0100 Subject: [PATCH] Fix incorrect LazyThreadSafetyMode.NONE usages, because: - it's an undefined behavior on Native platforms - to ensure singleton property --- .../data/models/preference/VideoResolverSettings.kt | 2 +- .../kotlin/data/source/danmaku/protocol/Updates.kt | 12 +++++++++++- .../source/media/fetch/FilteredMediaSourceResults.kt | 11 ++++++++++- .../source/media/source/web/SelectorMediaSource.kt | 2 +- .../kotlin/tools/torrent/TorrentManager.kt | 11 ++++++++++- .../mediaFetch/MediaSourceResultPresentation.kt | 11 ++++++++++- 6 files changed, 43 insertions(+), 6 deletions(-) diff --git a/app/shared/app-data/src/commonMain/kotlin/data/models/preference/VideoResolverSettings.kt b/app/shared/app-data/src/commonMain/kotlin/data/models/preference/VideoResolverSettings.kt index 40db33c4b8..f34e6e2621 100644 --- a/app/shared/app-data/src/commonMain/kotlin/data/models/preference/VideoResolverSettings.kt +++ b/app/shared/app-data/src/commonMain/kotlin/data/models/preference/VideoResolverSettings.kt @@ -41,7 +41,7 @@ enum class WebViewDriver { } companion object { - val enabledEntries by lazy(LazyThreadSafetyMode.NONE) { + val enabledEntries by lazy { entries.sortedDescending() } } diff --git a/app/shared/app-data/src/commonMain/kotlin/data/source/danmaku/protocol/Updates.kt b/app/shared/app-data/src/commonMain/kotlin/data/source/danmaku/protocol/Updates.kt index ef0b9396a5..37150b804c 100644 --- a/app/shared/app-data/src/commonMain/kotlin/data/source/danmaku/protocol/Updates.kt +++ b/app/shared/app-data/src/commonMain/kotlin/data/source/danmaku/protocol/Updates.kt @@ -1,6 +1,16 @@ +/* + * Copyright (C) 2024 OpenAni and contributors. + * + * 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证. + * Use of this source code is governed by the GNU AGPLv3 license, which can be found at the following link. + * + * https://github.com/open-ani/ani/blob/main/LICENSE + */ + package me.him188.ani.app.data.source.danmaku.protocol import kotlinx.serialization.Serializable +import me.him188.ani.app.data.source.danmaku.protocol.ReleaseClass.entries @Serializable @@ -55,7 +65,7 @@ enum class ReleaseClass { /** * 在客户端启用了的项目 */ - val enabledEntries by lazy(LazyThreadSafetyMode.NONE) { + val enabledEntries by lazy { entries.filter { it != RC }.sortedDescending() } diff --git a/app/shared/app-data/src/commonMain/kotlin/data/source/media/fetch/FilteredMediaSourceResults.kt b/app/shared/app-data/src/commonMain/kotlin/data/source/media/fetch/FilteredMediaSourceResults.kt index f104b29290..363d9f88d9 100644 --- a/app/shared/app-data/src/commonMain/kotlin/data/source/media/fetch/FilteredMediaSourceResults.kt +++ b/app/shared/app-data/src/commonMain/kotlin/data/source/media/fetch/FilteredMediaSourceResults.kt @@ -1,3 +1,12 @@ +/* + * Copyright (C) 2024 OpenAni and contributors. + * + * 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证. + * Use of this source code is governed by the GNU AGPLv3 license, which can be found at the following link. + * + * https://github.com/open-ani/ani/blob/main/LICENSE + */ + package me.him188.ani.app.data.source.media.fetch import kotlinx.coroutines.CoroutineScope @@ -93,7 +102,7 @@ class FilteredMediaSourceResults( // } //} -private val EmptyFilteredMediaSourceResults by lazy(LazyThreadSafetyMode.NONE) { +private val EmptyFilteredMediaSourceResults by lazy { FilteredMediaSourceResults(flowOf(emptyList()), flowOf(MediaSelectorSettings.Default)) } diff --git a/app/shared/app-data/src/commonMain/kotlin/data/source/media/source/web/SelectorMediaSource.kt b/app/shared/app-data/src/commonMain/kotlin/data/source/media/source/web/SelectorMediaSource.kt index ff6ca4cec5..4e4689eeab 100644 --- a/app/shared/app-data/src/commonMain/kotlin/data/source/media/source/web/SelectorMediaSource.kt +++ b/app/shared/app-data/src/commonMain/kotlin/data/source/media/source/web/SelectorMediaSource.kt @@ -184,7 +184,7 @@ class SelectorMediaSource( }.merge() } - override val matcher: WebVideoMatcher by lazy(LazyThreadSafetyMode.NONE) { + override val matcher: WebVideoMatcher by lazy(LazyThreadSafetyMode.PUBLICATION) { WebVideoMatcher { url, _ -> engine.matchWebVideo(url, arguments.searchConfig.matchVideo) } diff --git a/app/shared/app-data/src/commonMain/kotlin/tools/torrent/TorrentManager.kt b/app/shared/app-data/src/commonMain/kotlin/tools/torrent/TorrentManager.kt index 9fa9ed3325..a2b9952335 100644 --- a/app/shared/app-data/src/commonMain/kotlin/tools/torrent/TorrentManager.kt +++ b/app/shared/app-data/src/commonMain/kotlin/tools/torrent/TorrentManager.kt @@ -1,3 +1,12 @@ +/* + * Copyright (C) 2024 OpenAni and contributors. + * + * 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证. + * Use of this source code is governed by the GNU AGPLv3 license, which can be found at the following link. + * + * https://github.com/open-ani/ani/blob/main/LICENSE + */ + package me.him188.ani.app.tools.torrent import kotlinx.coroutines.CoroutineName @@ -52,7 +61,7 @@ class DefaultTorrentManager( ) } - override val engines: List by lazy(LazyThreadSafetyMode.NONE) { + override val engines: List by lazy { // 注意, 是故意只启用一个下载器的, 因为每个下载器都会创建一个 DirectoryMediaCacheStorage // 并且使用相同的 mediaSourceId: MediaCacheManager.LOCAL_FS_MEDIA_SOURCE_ID. // 搜索数据源时会使用 mediaSourceId 作为 map key, 导致总是只会用一个 storage. diff --git a/app/shared/src/commonMain/kotlin/ui/subject/episode/mediaFetch/MediaSourceResultPresentation.kt b/app/shared/src/commonMain/kotlin/ui/subject/episode/mediaFetch/MediaSourceResultPresentation.kt index fa83921690..e46ed9d45c 100644 --- a/app/shared/src/commonMain/kotlin/ui/subject/episode/mediaFetch/MediaSourceResultPresentation.kt +++ b/app/shared/src/commonMain/kotlin/ui/subject/episode/mediaFetch/MediaSourceResultPresentation.kt @@ -1,3 +1,12 @@ +/* + * Copyright (C) 2024 OpenAni and contributors. + * + * 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证. + * Use of this source code is governed by the GNU AGPLv3 license, which can be found at the following link. + * + * https://github.com/open-ani/ani/blob/main/LICENSE + */ + package me.him188.ani.app.ui.subject.episode.mediaFetch import androidx.compose.runtime.Composable @@ -135,7 +144,7 @@ class MediaSourceResultsPresentation( val totalSourceCount by derivedStateOf { list.count { it.kind != MediaSourceKind.LocalCache } } // 缓存数据源属于内部的, 用户应当无感 } -private val EmptyMediaSourceResultsPresentation by lazy(LazyThreadSafetyMode.NONE) { +private val EmptyMediaSourceResultsPresentation by lazy { MediaSourceResultsPresentation(emptyMediaSourceResults(), EmptyCoroutineContext) }