diff --git a/CHANGELOG.md b/CHANGELOG.md index 06f6d11..f336135 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/README.md b/README.md index 57dcb9e..4a2f199 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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 @@ -141,7 +147,7 @@ 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. @@ -149,11 +155,11 @@ Below `scheduler` is a `Scheduler`, `fetch` is a `TaskHandle`. ## 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` | 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 | diff --git a/gradle.properties b/gradle.properties index 2a97e9d..5580fc1 100644 --- a/gradle.properties +++ b/gradle.properties @@ -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 diff --git a/library/src/commonMain/kotlin/ru/kode/reactivetasks/ReactiveTasks.kt b/library/src/commonMain/kotlin/ru/kode/reactivetasks/ReactiveTasks.kt index 0be801e..3a6f9ae 100644 --- a/library/src/commonMain/kotlin/ru/kode/reactivetasks/ReactiveTasks.kt +++ b/library/src/commonMain/kotlin/ru/kode/reactivetasks/ReactiveTasks.kt @@ -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, /** @@ -222,6 +224,11 @@ class Scheduler( private val stateChangeListeners: MutableList = createConcurrentMutableList() + /** + * Jobs of the task scopes created by [registerTask], they are kept active by [cancelAll] + */ + private val taskScopeJobs: MutableList = createConcurrentMutableList() + /** * Called with a taskId when a job of that task is launched or completes (normally or by cancellation) */ @@ -256,6 +263,7 @@ class Scheduler( ): TaskHandle { 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, @@ -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) diff --git a/library/src/commonTest/kotlin/ru/kode/reactivetasks/SchedulerTest.kt b/library/src/commonTest/kotlin/ru/kode/reactivetasks/SchedulerTest.kt index 466577b..d670aa0 100644 --- a/library/src/commonTest/kotlin/ru/kode/reactivetasks/SchedulerTest.kt +++ b/library/src/commonTest/kotlin/ru/kode/reactivetasks/SchedulerTest.kt @@ -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 @@ -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 }