Skip to content
This repository has been archived by the owner on Feb 9, 2024. It is now read-only.

Deprecate CoroutineWorker, enable the new Memory Model #99

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
# This project is DEPRECATED

With the release of Kotlin 1.7.20-Beta (https://blog.jetbrains.com/kotlin/2022/08/kotlin-1-7-20-beta/) and the new
Memory Model for Kotlin Native becoming the default, there's no longer a need for this library. Version 0.9.0 helps
transition over to the new memory model without having to make any changes related to CoroutineWorker code. The latest
version of CoroutineWorker uses the same implementation for all platforms.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we maybe just leave the documentation below set to the last version that did things differently, and tell people to just use the latest version only if they are using the new memory model?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wait why are we doing a 0.9.0 again? I thought we were gonna do 1.0

# CoroutineWorker

[![Build Status](https://github.com/autodesk/coroutineworker/workflows/build/badge.svg)](https://github.com/autodesk/coroutineworker/actions?query=workflow%3Abuild)
Expand All @@ -18,7 +25,7 @@ kotlin {
sourceSets {
commonMain {
dependencies {
implementation "com.autodesk:coroutineworker:0.8.3"
implementation "com.autodesk:coroutineworker:0.9.0"
}
}
}
Expand Down
5 changes: 3 additions & 2 deletions gradle.properties
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
VERSION=0.8.3
VERSION=0.9.0

kotlin.mpp.enableCompatibilityMetadataVariant=true
kotlin.mpp.stability.nowarn=true
kotlin.mpp.stability.nowarn=true
kotlin.native.binary.memoryModel=experimental
Original file line number Diff line number Diff line change
Expand Up @@ -2,41 +2,38 @@ package com.autodesk.coroutineworker

import kotlinx.coroutines.CoroutineScope
import kotlin.coroutines.CoroutineContext
import kotlinx.coroutines.Job
import kotlinx.coroutines.cancelAndJoin
import kotlinx.coroutines.launch

/**
* Encapsulates performing background work. On JVM, this use coroutines outright.
* In native, this uses Worker threads and manages its mutability/concurrency issues.
*/
public expect class CoroutineWorker internal constructor() {

/** Cancels the underlying Job */
public fun cancel()
public class CoroutineWorker : CoroutineScope {
private val job = Job()

/**
* Cancel the underlying job/worker and waits for it
* to receive its cancellation message or complete
* */
public suspend fun cancelAndJoin()
* The context of this scope. See [CoroutineScope.coroutineContext]
*/
override val coroutineContext: CoroutineContext = job

public fun cancel() {
job.cancel()
}

public suspend fun cancelAndJoin() {
job.cancelAndJoin()
}

public companion object {

/**
* Enqueues the background work [block] to run and returns a reference to the worker, which can be cancelled
*/
public fun execute(block: suspend CoroutineScope.() -> Unit): CoroutineWorker

/**
* Performs [block] in another [CoroutineContext] ([jvmContext]) and waits for it to complete.
* This is similar to [kotlinx.coroutines.withContext] with some caveats:
*
* - Native: we cannot use [kotlinx.coroutines.withContext] because
* the global Dispatchers do not work properly. Therefore,
* the context argument is ignored, and the block is always
* run on some other Worker in the Worker pool. This is the
* most similar to switching contexts on JVM.
*
* - JVM: This has the same behavior as calling [kotlinx.coroutines.withContext] in the JVM
*/
public suspend fun <T> withContext(jvmContext: CoroutineContext, block: suspend CoroutineScope.() -> T): T
public fun execute(block: suspend CoroutineScope.() -> Unit): CoroutineWorker {
return CoroutineWorker().also {
it.launch(block = block)
}
}

public suspend fun <T> withContext(jvmContext: CoroutineContext, block: suspend CoroutineScope.() -> T): T {
return kotlinx.coroutines.withContext(jvmContext) {
block()
}
}
}
}
20 changes: 15 additions & 5 deletions src/commonMain/kotlin/com/autodesk/coroutineworker/Utils.kt
Original file line number Diff line number Diff line change
@@ -1,11 +1,21 @@
package com.autodesk.coroutineworker

import kotlinx.coroutines.suspendCancellableCoroutine

/**
* Bridges a platform's callback-based async method to coroutines,
* ensuring that the coroutine is resumed on a thread appropriate
* for the platform.
*
* [startAsync] is called to start the work. It calls the passed [CompletionLambda]
* when complete, and returns a [CancellationLambda] that can be called to cancel the
* async work
* */
public expect suspend fun <T> threadSafeSuspendCallback(startAsync: (CompletionLambda<T>) -> CancellationLambda): T
public suspend fun <T> threadSafeSuspendCallback(startAsync: (CompletionLambda<T>) -> CancellationLambda): T {
return suspendCancellableCoroutine { cont ->
val cancellable = startAsync {
if (cont.isActive) {
cont.resumeWith(it)
}
}
cont.invokeOnCancellation {
cancellable()
}
}
}
40 changes: 0 additions & 40 deletions src/jsMain/kotlin/com/autodesk/coroutineworker/CoroutineWorker.kt

This file was deleted.

16 changes: 0 additions & 16 deletions src/jsMain/kotlin/com/autodesk/coroutineworker/Utils.kt

This file was deleted.

40 changes: 0 additions & 40 deletions src/jvmMain/kotlin/com/autodesk/coroutineworker/CoroutineWorker.kt

This file was deleted.

16 changes: 0 additions & 16 deletions src/jvmMain/kotlin/com/autodesk/coroutineworker/Utils.kt

This file was deleted.

This file was deleted.

Loading