Kotlin coroutines flow

From findscripter/everything-skills · View interactive version

Kotlin coroutines flow is an AI agent skill from the GitHub repository findscripter/everything-skills. 当用 Kotlin 写异步、并发或响应式数据流时使用;用结构化并发、Flow 转换、异常处理与协程测试写出无泄漏可取消的代码;不适用于非 Kotlin 异步、Rx/Reactor 框架或纯阻塞同步逻辑。触发词:协程、Flow、结构化并发

How to use this skill

Skill file

何时使用

适用:

不该用(负边界):

步骤 / 指令

  1. 结构化并发:永远在明确的 CoroutineScope 内启动协程。用 coroutineScope(任一子任务失败则全部取消)或 supervisorScope(子任务相互隔离)分组并发任务,靠 async/await 聚合结果。
  2. 异常处理:顶层作用域用 CoroutineExceptionHandler 兜底;在 suspend 函数内部用 try-catch 做细粒度控制。关键约束——除非随即重新抛出,否则不要捕获 CancellationException,否则会破坏取消机制。
  3. 响应式流:需要保留的状态用 StateFlow,一次性事件用 SharedFlow。冷流(如搜索)用 debounce + flatMapLatest 组合,并用 flowOn(Dispatchers.IO) 切换上游调度器。
  4. 调度器:阻塞 I/O 一律放到 Dispatchers.IO。
  5. 生命周期:作用域不再需要时主动取消(如 ViewModel.onCleared);杜绝 GlobalScope。
  6. 测试:用 TestScope + runTest,并把 TestDispatcher 注入被测类以控制虚拟时间。

示例

并发聚合(任一失败则整体取消):

suspend fun loadDashboardData(): DashboardData = coroutineScope {
    val userDeferred = async { userRepo.getUser() }
    val settingsDeferred = async { settingsRepo.getSettings() }
    DashboardData(
        user = userDeferred.await(),
        settings = settingsDeferred.await()
    )
}

顶层兜底 + 局部捕获:

val handler = CoroutineExceptionHandler { _, exception ->
    println("Caught $exception")
}
viewModelScope.launch(handler) {
    try {
        riskyOperation()
    } catch (e: IOException) {
        // 专门处理网络错误
    }
}

Flow:冷流去抖搜索 + 热流状态:

// 冷流(惰性)
val searchResults: Flow<List<Item>> = searchQuery
    .debounce(300)
    .flatMapLatest { query -> searchRepo.search(query) }
    .flowOn(Dispatchers.IO)

// 热流(状态)
val uiState: StateFlow<UiState> = _uiState.asStateFlow()

并行执行 + 错误隔离(supervisorScope 下 task2 失败不取消 task1):

suspend fun fetchDataWithErrorHandling() = supervisorScope {
    val task1 = async {
        try { api.fetchA() } catch (e: Exception) { null }
    }
    val task2 = async { api.fetchB() }
    val result1 = task1.await()
    val result2 = task2.await() // 可能抛异常
}

注意事项

互见


采编自 sickn33/antigravity-awesome-skills(MIT 许可证)。

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.