Agent Skills
› chrisbanes/skills
› kotlin-concurrency-and-flow
kotlin-concurrency-and-flow
GitHub指导 Kotlin 协程作用域管理、生命周期及 Flow 状态事件建模,确保并发安全与语义清晰。
触发场景
Kotlin 协程作用域所有权问题
StateFlow/SharedFlow 使用
CoroutineScope 存储或取消边界
安装
npx skills add chrisbanes/skills --skill kotlin-concurrency-and-flow -g -y
SKILL.md
Frontmatter
{
"name": "kotlin-concurrency-and-flow",
"description": "Use when writing or reviewing Kotlin coroutine scope ownership, init launches, non-suspending launch APIs, runBlocking, cancellation, StateFlow, SharedFlow, Channel, stateIn, SharingStarted, state updates, or one-shot events."
}
Kotlin concurrency and Flow
Core principle
Give asynchronous work an explicit owner and lifetime, then model durable state and transient events with primitives whose delivery and replay semantics match the product contract.
Procedure
- Identify each coroutine owner, cancellation boundary, producer, consumer, durable state, and transient event.
- Select a scope whose lifecycle owns the work; do not retain arbitrary scopes or hide unstructured launches behind non-suspending APIs.
- Model renderable, current data as state and imperative one-shot work as an event only when its loss and replay behavior are explicitly acceptable.
- Choose Flow sharing and buffering semantics from the producer and consumer lifetimes rather than from a default.
- Read the focused reference for the material concern below.
- Finish when cancellation, restart, replay, and failure behavior are all observable from the public API and no caller must guess who owns the work.
Topic router
| Signal | Read |
|---|---|
Stored CoroutineScope, init { launch }, fire-and-forget API, runBlocking, broad catch, or cancellation boundary |
Structured concurrency |
StateFlow, SharedFlow, Channel, stateIn, SharingStarted, .value, state updates, sentinel values, or one-shot events |
Flow state and events |
| Compose collection or UI effect handling | Compose state and effects |
RED/GREEN agent scenarios
- RED stores a long-lived
CoroutineScopein a service and launches from arbitrary callers. GREEN makes ownership and cancellation follow a defined lifecycle boundary. - Novel case: a screen needs replayable loading state and non-replayable navigation. GREEN uses distinct state and event contracts with documented delivery semantics.
- Counterexample: a suspend function already has a caller-owned scope. GREEN does not add an internal scope merely to make the API look asynchronous.
版本历史
- 2026.8.5 当前 2026-08-16 02:45


