coroutines-patterns
Skill66 starsChanged 6 months agoMITView on GitHub
Kotlin Coroutines and Flow patterns for structured concurrency, error handling, and async operations.
In this repository51 other files it gives its agents.
- AGENTS.mdAGENTS.md
- rules/agents.mdAGENTS.md
- CLAUDE.mdCLAUDE.md
- examples/CLAUDE.mdCLAUDE.md
- .github/copilot-instructions.mdCopilot instructions
- .cursor/rules/everything-mobile.mdcCursor rule
- accessibility-patternsSkill
- analytics-patternsSkill
- android-patternsSkill
- app-lifecycleSkill
- ci-cd-patternsSkill
- combine-frameworkSkill
- continuous-learningSkill
- continuous-learning-v2Skill
- core-dataSkill
- deep-linkingSkill
- expect-actualSkill
- feature-builderSkill
- feature-flagsSkill
- gradle-patternsSkill
- image-loadingSkill
- ios-testingSkill
- jetpack-composeSkill
- kmp-diSkill
- kmp-navigationSkill
- kmp-networkingSkill
- kmp-repositoriesSkill
- koin-patternsSkill
- ktor-patternsSkill
- liquid-glassSkill
- localization-patternsSkill
- m3-expressiveSkill
- mobile-checkpointSkill
- mobile-compactionSkill
- mobile-instinct-v1Skill
- mobile-instinct-v2Skill
- mobile-memorySkill
- mobile-securitySkill
- mobile-testingSkill
- mobile-verificationSkill
- mvi-architectureSkill
- navigation-composeSkill
- offline-firstSkill
- pagination-patternsSkill
- push-notificationsSkill
- room-patternsSkill
- shared-coroutinesSkill
- shared-modelsSkill
- sqldelight-patternsSkill
- swift-patternsSkill
- swiftui-patternsSkill
The file
Files
Coroutines Patterns
Structured concurrency for Kotlin.
Coroutine Scopes
// ✅ ViewModel scope (auto-cancelled)
class HomeViewModel : ViewModel() {
fun loadData() {
viewModelScope.launch {
// Cancelled when ViewModel cleared
}
}
}
// ✅ Lifecycle scope
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
lifecycleScope.launch {
// Cancelled when lifecycle destroyed
}
}
}
// ❌ AVOID: GlobalScope
GlobalScope.launch { } // Never cancelled, memory leaksDispatchers
// Main - UI operations
withContext(Dispatchers.Main) {
textView.text = "Updated"
}
// IO - Network, disk
withContext(Dispatchers.IO) {
api.fetchData()
database.query()
}
// Default - CPU intensive
withContext(Dispatchers.Default) {
list.sortedBy { it.score }
}Flow Patterns
// StateFlow for UI state
private val _state = MutableStateFlow(HomeState())
val state: StateFlow<HomeState> = _state.asStateFlow()
// SharedFlow for events
private val _events = MutableSharedFlow<Event>()
val events: SharedFlow<Event> = _events.asSharedFlow()
// Collect with lifecycle
@Composable
fun HomeScreen(viewModel: HomeViewModel) {
val state by viewModel.state.collectAsStateWithLifecycle()
}Flow Operators
flow
.filter { it.isActive }
.map { transform(it) }
.distinctUntilChanged()
.debounce(300)
.catch { emit(fallback) }
.collect { process(it) }Error Handling
// Try-catch in coroutine
viewModelScope.launch {
try {
val result = repository.fetchData()
_state.value = Success(result)
} catch (e: Exception) {
_state.value = Error(e.message)
}
}
// supervisorScope - siblings don't cancel
supervisorScope {
launch { task1() } // Failure doesn't cancel task2
launch { task2() }
}Cancellation
// Cooperative cancellation
suspend fun processItems(items: List<Item>) {
items.forEach { item ->
ensureActive() // Check cancellation
process(item)
}
}
// CancellationException handling
try {
coroutineWork()
} catch (e: CancellationException) {
throw e // Don't swallow!
} catch (e: Exception) {
handleError(e)
}Remember: Structured concurrency = lifecycle-bound, cancellable, debuggable.
Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
No reviews yet
People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.
- It worked
- Partly
- It didn't
Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

