Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
# Changelog

## 1.3.0 - 2026-11-11

### Added

- `Scheduler.cancelAll()`: cancels the jobs of all tasks and all other coroutines launched in `scheduler.scope`, the
scheduler stays usable. Cancelling `scheduler.scope` itself makes the scheduler unusable for good (later starts and
launches do nothing), so `cancelAll()` is the way to stop and start again, e.g. a remo 1.x model started again with
a new scope.

## 1.2.0 - 2026-11-10

### Added
Expand Down
16 changes: 11 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ A library for scheduling, executing and observing repeateble reactive tasks.
Kotlin Multiplatform: JVM, Android, iOS (`iosX64`, `iosArm64`, `iosSimulatorArm64`).

```kotlin
implementation("ru.kode:reactive-tasks:1.2.0")
implementation("ru.kode:reactive-tasks:1.3.0")
```

# How to use
Expand Down Expand Up @@ -128,6 +128,12 @@ scheduler.cancel(jobHandle2)
runBlocking {
scheduler.cancelAndJoin(jobHandle2)
}

//
// Cancel the jobs of all tasks and all coroutines launched in scheduler.scope, the scheduler stays usable.
// Cancelling scheduler.scope itself makes the scheduler unusable for good
//
scheduler.cancelAll()
```

# Migrating from remo
Expand All @@ -141,19 +147,19 @@ reactive-tasks drops the `ReactiveModel` base class and `kotlin-result`, and add
// before
implementation("ru.kode:remo:1.2.0") // or ru.kode:remo:9.0.1, ru.kode.android:remo:9.1.0
// after
implementation("ru.kode:reactive-tasks:1.2.0")
implementation("ru.kode:reactive-tasks:1.3.0")
```

remo 1.2.0 and 9.x differ only in how a model gets its scope; tasks and `JobFlow` are the same.
Below `scheduler` is a `Scheduler`, `fetch` is a `TaskHandle<A, R>`.

## API map

| remo 1.2.0 | remo 9.0.1 | reactive-tasks 1.2.0 |
| remo 1.2.0 | remo 9.0.1 | reactive-tasks 1.3.0 |
|---|---|---|
| `class M : ReactiveModel(dispatcher)` | `class M(scope) : ReactiveModel(scope)` | `class M(scheduler: Scheduler)`, no base class |
| `model.start(parentScope)`, `Job.cancel()` to stop | constructor `scope`, cancel it to stop | `Scheduler(scope)`, cancel `scope` to stop |
| `onPostStart()` | `init {}` (`onPostStart` deprecated) | `init {}` |
| `model.start(parentScope)`, `Job.cancel()` to stop, can be started again with a new scope | constructor `scope`, cancel it to stop | `Scheduler(scope)`, cancel `scope` to stop for good; to stop and start again: `parentScope.coroutineContext.job.invokeOnCompletion { scheduler.cancelAll() }` on each start |
| `onPostStart()` | `init {}` (`onPostStart` deprecated) | 1.x: a `start(parentScope)` function of the model, called where `model.start` was (runs on every start); 9.x: `init {}` |
| `scope.launch {}` inside the model | same | `scheduler.scope.launch {}` |
| `uncaughtExceptions: Flow<Throwable>` | same | `Scheduler(onUncaughtException = { taskId, taskName, e -> })` |
| `ReactiveModel(errorMapper = …)`, `task(errorMapper = …)`, `mapErrors {}` | same | map in the flow: `errors(fetch).map { … }`, or catch inside the task body |
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ kotlin.mpp.enableCInteropCommonization=true
android.useAndroidX=true
android.nonTransitiveRClass=true
#Publishing
versionName=1.2.0
versionName=1.3.0
pomGroupId=ru.kode
pomName=Reactive Tasks
pomDescription=Kotlin Multiplatform library for scheduling, executing and observing reactive tasks
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,9 @@ interface TaskStateChangeListener {
class Scheduler(
/**
* Scope to be used as a parent for all tasks/jobs in this scheduler.
* If this scope gets cancelled, all tasks/jobs will be cancelled too.
* If this scope gets cancelled, all tasks/jobs will be cancelled too and the scheduler can't be used
* anymore: later starts and coroutines launched in it do nothing. Use [cancelAll] to stop the work and keep
* the scheduler usable.
*/
val scope: CoroutineScope,
/**
Expand Down Expand Up @@ -222,6 +224,11 @@ class Scheduler(

private val stateChangeListeners: MutableList<TaskStateChangeListener> = createConcurrentMutableList()

/**
* Jobs of the task scopes created by [registerTask], they are kept active by [cancelAll]
*/
private val taskScopeJobs: MutableList<Job> = createConcurrentMutableList()

/**
* Called with a taskId when a job of that task is launched or completes (normally or by cancellation)
*/
Expand Down Expand Up @@ -256,6 +263,7 @@ class Scheduler(
): TaskHandle<A, R> {
val taskId = Uuid.random()
val scope = CoroutineScope(SupervisorJob(scope.coroutineContext.job) + createExceptionHandler(taskId, name) + dispatcher)
taskScopeJobs.add(scope.coroutineContext.job)
return TaskHandle(
name = name,
id = taskId,
Expand Down Expand Up @@ -445,6 +453,15 @@ class Scheduler(
suspend fun cancelAndJoin(jobHandle: JobHandle) {
jobHandle.job.cancelAndJoin()
}

/**
* Cancels the jobs of all tasks and all other coroutines launched in [scope], unlike cancelling [scope] the
* scheduler stays usable: tasks can be started and coroutines launched in [scope] again.
* Registered change listeners will get [TaskStateChangeListener#onJobCancelled] notifications
*/
fun cancelAll() {
scope.coroutineContext.job.children.forEach { if (it in taskScopeJobs) it.cancelChildren() else it.cancel() }
}
}

@OptIn(ExperimentalUuidApi::class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import kotlinx.coroutines.test.StandardTestDispatcher
import kotlinx.coroutines.test.TestScope
import kotlinx.coroutines.test.advanceTimeBy
import kotlinx.coroutines.test.advanceUntilIdle
import kotlinx.coroutines.test.runCurrent
import kotlinx.coroutines.test.runTest
Expand Down Expand Up @@ -463,4 +464,44 @@ class SchedulerTest {
}

// endregion

// region cancelAll

@Test
fun cancelAllCancelsJobsAndCoroutinesAndKeepsSchedulerUsable() = runTest {
val scheduler = testScheduler()
val listener = RecordingListener().also(scheduler::addTaskStateChangeListener)
val task = scheduler.registerTask("t") { _: Int -> delay(1_000) }
var ticks = 0
fun launchTicker() = scheduler.scope.launch {
while (true) {
delay(100)
ticks++
}
}

val job = scheduler.start(task, 1)
val ticker = launchTicker()
runCurrent()
assertTrue(scheduler.isRunning(task))

scheduler.cancelAll()
runCurrent()

assertTrue(job.job.isCancelled)
assertTrue(ticker.isCancelled)
assertTrue(!scheduler.isRunning(task))
assertEquals(listOf(job.id), listener.cancelled.map { it.jobId })

// the scheduler is still usable: tasks start and coroutines launch again
scheduler.start(task, 2)
launchTicker()
advanceTimeBy(1_050)
runCurrent()

assertEquals(RunState.FinishedSuccess, listener.states.last { it.argument == 2 }.runState)
assertEquals(10, ticks)
}

// endregion
}
Loading