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

弃用无 receiver 的事件扩展函数, 增加其 EventChannel receiver 版本替换 #1827

Closed
11 tasks done
Him188 opened this issue Jan 6, 2022 · 0 comments · Fixed by #2213
Closed
11 tasks done
Labels
N 优先级: 一般 t:feature 类型: 新特性 x:deprecation 属性: API 弃用
Milestone

Comments

@Him188
Copy link
Member

Him188 commented Jan 6, 2022

动机

  • EventChannel 在 2.0 增加, 但原函数均不支持 EventChannel
  • 降低 API 复杂度

变更

弃用

逐渐弃用如下 8 个 top-level 函数:

  • nextEvent
  • nextEventAsync
  • nextEventOrNull
  • nextEventOrNullAsync
  • syncFromEvent
  • asyncFromEvent
  • syncFromEventOrNull
  • asyncFromEventOrNull

增加

新增 2 个 EventChannel 的扩展函数 nextEventsyncFromEvent:

public suspend inline fun <reified E : Event> EventChannel<*>.nextEvent(
    priority: EventPriority = EventPriority.NORMAL,
    noinline filter: suspend E.(E) -> Boolean = { true } 
): E = coroutineScope { nextEventImpl(this@nextEvent, E::class, this@coroutineScope, priority, filter) }

public suspend inline fun <reified E : Event, R : Any> EventChannel<*>.syncFromEvent(
    priority: EventPriority = EventPriority.NORMAL,
    crossinline mapper: suspend (E) -> R? // 删除了 receiver E
): R = coroutineScope { this@syncFromEvent.syncFromEventImpl(E::class, this, priority, mapper) }

注意, 默认的优先级为 NORMAL 而不是原来的 MONITOR

迁移方案

最终上述 8 个方法将简化为两个基础标准函数 (Extensions.kt) (见上 "变更").

超时, Async 和 OrNull 通过协程库的 CoroutineScope.async, withTimeout, withTimeoutOrNull 完成. 示例:

withTimeout(3000) { GlobalEventChannel.nextEvent<MessageEvent>() } // 相当于原来的 syncFromEvent<MessageEvent>(3000)

自动迁移

  • nextEvent: 替换为 EventChannel.nextEvent + withTimeout
  • nextEventAsync: 无替换
  • nextEventOrNull: 替换为 EventChannel.nextEvent + withTimeoutOrNull
  • nextEventOrNullAsync: 无替换
  • syncFromEvent: 替换为 EventChannel.syncFromEvent + withTimeout
  • asyncFromEvent: 无替换
  • syncFromEventOrNull: 替换为 EventChannel.syncFromEvent + withTimeoutOrNull
  • asyncFromEventOrNull: 无替换

更新计划

@Him188 Him188 added t:feature 类型: 新特性 N 优先级: 一般 x:behavior-change 属性: 行为变更 (API 不变, 行为改变) labels Jan 6, 2022
@Him188 Him188 added this to the 2.10.0-RC milestone Jan 6, 2022
Him188 added a commit that referenced this issue Jan 6, 2022
…cFromEvent*`. Add their `EventChannel`-receiver counterparts. #1827
Him188 added a commit that referenced this issue Jan 6, 2022
…cFromEvent*`. Add their `EventChannel`-receiver counterparts. #1827
Him188 added a commit that referenced this issue Jan 6, 2022
…cFromEvent*`. Add their `EventChannel`-receiver counterparts. #1827
Him188 added a commit that referenced this issue Jan 20, 2022
* Deprecate no-receiver functions `nextEvent*`, `syncFromEvent*`, `asyncFromEvent*`. Add their `EventChannel`-receiver counterparts. #1827

* Fix migration

* Migrate `nextMessage` to new API
@Him188 Him188 modified the milestones: 2.10.0-RC, 2.12 Jan 20, 2022
@Him188 Him188 added x:deprecation 属性: API 弃用 and removed x:behavior-change 属性: 行为变更 (API 不变, 行为改变) labels Feb 19, 2022
strear pushed a commit to strear/mirai that referenced this issue Feb 21, 2022
* Deprecate no-receiver functions `nextEvent*`, `syncFromEvent*`, `asyncFromEvent*`. Add their `EventChannel`-receiver counterparts. mamoe#1827

* Fix migration

* Migrate `nextMessage` to new API
@Him188 Him188 modified the milestones: 2.12, 2.12.0-RC May 8, 2022
@Him188 Him188 modified the milestones: 2.12.0-RC, 2.13, 2.13.0-RC Jun 14, 2022
@Him188 Him188 linked a pull request Aug 26, 2022 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
N 优先级: 一般 t:feature 类型: 新特性 x:deprecation 属性: API 弃用
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant