Skip to content
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

优先选择偏好类型数据源,不需要等其他源查询完毕 #1002

Open
wants to merge 6 commits into
base: main
Choose a base branch
from

Conversation

NieR4ever
Copy link
Member

resolve #816

@Him188 Him188 self-requested a review September 24, 2024 09:26
@Him188 Him188 added the s: media 子系统: 数据源基础 label Sep 24, 2024
Copy link
Member

@Him188 Him188 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

需要增加测试, 当一个源完成后就立即选择

@NieR4ever
Copy link
Member Author

不清楚怎么测试,感觉比较难观察 hasCompleted 的变化

Copy link
Member

@Him188 Him188 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, 但是需要有 test 确保以后不会 regress

需要测试:

  • prefer BT 时, WEB 完成了, 应当继续等待
  • prefer BT 时, BT 完成了, 应当自动选择
  • prefer BT 时, 没有启用 BT 源, 会怎么样?
  • prefer BT 时, 但 BT 没有搜到东西, 是什么行为?
  • 没有 preferefence, BT 完成了但 web 没有完成, 应当继续等待
  • 没有 preferefence, 都完成了, 自动选择 (应该是现有 case?)

@Him188
Copy link
Member

Him188 commented Sep 25, 2024

test 中协程是单线程的, 你可以控制的

在数据源实现中, 用 CompletableDeferred<Unit> 来等待. 然后你在后面可以 deferred.complete(Unit), 然后 runCurrent() 让 fetch 的协程跑.

Comment on lines +382 to +383
@JvmInline
value class CompletedConditions(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
@JvmInline
value class CompletedConditions(
class CompletedConditions(

这个东西看起来会用在被 box 的地方, 那其实不需要 value

Comment on lines +350 to +352
states.all { it is MediaSourceFetchState.Disabled } -> null
states.all { it is MediaSourceFetchState.Completed || it is MediaSourceFetchState.Disabled } -> true
else -> false
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

为什么要有 null? 不能用 false

}
}

combine(map) { pairs ->
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

目前两个 combine 的算法会导致启动数倍多的协程

用一个 combine 就足够解决问题了, 让 combine 的 lambda 返回 ImmutableEnumMap<MSK, Boolean>

Comment on lines 343 to +346
override val hasCompleted = if (mediaSourceResults.isEmpty()) {
flowOf(true)
flowOf(CompletedConditions.AllCompleted)
} else {
combine(mediaSourceResults.map { it.state }) { states ->
states.all { it is MediaSourceFetchState.Completed || it is MediaSourceFetchState.Disabled }
val map = MediaSourceKind.entries.map { kind ->
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

目前这样改会为那些原本总是期待所有数据源加载完成的代码增加计算量 (计算是否完成并构造 EnumMap 等), 导致性能变差

这是可以避免的, 我觉得可能要做一个另外的属性来支持中间状态

null
}

fun copy(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

未使用, 没必要有

Comment on lines +135 to +138
val mediaList = mutableListOf<DefaultMedia>()
if (webEnabled) mediaList.addAll(TestMediaList.map { it.copy(kind = MediaSourceKind.WEB) })
if (btEnabled) mediaList.addAll(TestMediaList.map { it.copy(kind = MediaSourceKind.BitTorrent) })
this.mediaList.value = mediaList
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这看起来可能会意外地影响 test 的结果
我感觉应当至少保持一个其他类型的

Comment on lines +194 to +195
@Test
fun `awaitCompletedAndSelectDefault selects one when prefer bt and bt done`() = runTest {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

我觉得要新加一个 section 表示这些是来测试 "
优先选择偏好类型数据源,不需要等其他源查询完毕" 的, 否则未来的人可能不会知道这些 case 的用途

),
),
),
flowContext = EmptyCoroutineContext,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

memory leak

Comment on lines +252 to +253
val mediaFetcher = MediaSourceMediaFetcher(
configProvider = { MediaFetcherConfig.Default },
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

当用处超过两个就最好弄一个 createMediaSourceFetcher. 否则未来给 MediaSourceMediaFetcher 增加参数时要修改很多地方. 如果有 createMediaSourceFetcher 就只需要改一个地方

Comment on lines +127 to +128
btEnabled: Boolean = true,
webEnabled: Boolean = true,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
btEnabled: Boolean = true,
webEnabled: Boolean = true,
addBtSources: Boolean = true,
addWebSources: Boolean = true,

说明它的直接意图

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
s: media 子系统: 数据源基础
Projects
None yet
Development

Successfully merging this pull request may close these issues.

优先选择在线源时,不需要等BT源查询完毕
2 participants