{"id":13536969,"url":"https://github.com/Autodesk/coroutineworker","last_synced_at":"2025-04-02T03:31:31.197Z","repository":{"id":34949151,"uuid":"193166495","full_name":"Autodesk/coroutineworker","owner":"Autodesk","description":"Kotlin Coroutine-based workers for native","archived":true,"fork":false,"pushed_at":"2022-08-08T19:52:24.000Z","size":328,"stargazers_count":370,"open_issues_count":3,"forks_count":24,"subscribers_count":12,"default_branch":"master","last_synced_at":"2024-11-03T02:32:09.540Z","etag":null,"topics":["coroutines","kotlin","kotlin-coroutines","kotlin-multiplatform","kotlin-native","multiplatform"],"latest_commit_sha":null,"homepage":"","language":"Kotlin","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Autodesk.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE.txt","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-06-21T22:23:59.000Z","updated_at":"2024-10-14T22:28:26.000Z","dependencies_parsed_at":"2022-08-08T03:01:06.988Z","dependency_job_id":null,"html_url":"https://github.com/Autodesk/coroutineworker","commit_stats":null,"previous_names":[],"tags_count":24,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Autodesk%2Fcoroutineworker","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Autodesk%2Fcoroutineworker/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Autodesk%2Fcoroutineworker/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Autodesk%2Fcoroutineworker/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Autodesk","download_url":"https://codeload.github.com/Autodesk/coroutineworker/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246751244,"owners_count":20827857,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["coroutines","kotlin","kotlin-coroutines","kotlin-multiplatform","kotlin-native","multiplatform"],"created_at":"2024-08-01T09:00:52.874Z","updated_at":"2025-04-02T03:31:30.367Z","avatar_url":"https://github.com/Autodesk.png","language":"Kotlin","readme":"# CoroutineWorker\n\n[![Build Status](https://github.com/autodesk/coroutineworker/workflows/build/badge.svg)](https://github.com/autodesk/coroutineworker/actions?query=workflow%3Abuild)\n\n![Maven Central](https://img.shields.io/maven-central/v/com.autodesk/coroutineworker.svg)\n\n## Specs\n\n- Supported on Native, JVM, and JS (legacy and IR) (feel free to contribute adding more targets)\n- Kotlin 1.7.10\n\n## Gradle\n\nTo use in your multiplatform project, update your common dependencies in your gradle configuration:\n\n```groovy\nkotlin {\n    sourceSets {\n        commonMain {\n            dependencies {\n                implementation \"com.autodesk:coroutineworker:0.8.3\"\n            }\n        }\n    }\n}\n```\n\nCoroutineWorker uses gradle module metadata. We recommend adding the following to your settings.gradle to take advantage of that (not necessary for Gradle 6+):\n\n```groovy\nenableFeaturePreview('GRADLE_METADATA')\n```\n\n## About\n\nCoroutineWorker helps support multi-threaded coroutine usage in common code that works in Kotlin/Native and on JVM until [kotlinx.coroutines has full support for native, multi-threaded coroutines](https://github.com/Kotlin/kotlinx.coroutines/issues/462).\n\n## Projects Using this on your Devices\n\n- [PlanGrid iOS \u0026 Android](https://plangrid.com)\n\n## Sample Usage\n\n### Spawning Asynchronous Work\n\nUse `execute` to start background work from common code:\n\n```kotlin\nval worker = CoroutineWorker.execute {\n  // - In here, `this` is a `CoroutineScope`\n  // - Run suspend functions, call launch, etc.\n  // - This code runs in a thread pool\n}\n\n// Tells the worker to cancel (uses standard coroutine cancellation)\nworker.cancel()\n\n// Tells the worker to cancel; it suspends until cancellation is finished\nworker.cancelAndJoin()\n```\n\n### Waiting on Asynchronous Work to Complete\n\nFrom a coroutine context (i.e. somewhere you can call a `suspend fun`), use `withContext` to kick off work to another thread. It will non-blocking/suspend wait for the cross-thread work to complete:\n\n```kotlin\nsuspend fun doWork() {\n  val result = CoroutineWorker.withContext {\n    // This is similar to execute, but it returns\n    // the result of the work at the end of this lambda\n    1\n  }\n  print(result) // prints 1\n}\n```\n\nThis is like using `withContext` on JVM to switch coroutine contexts. You can also properly pass a dispatcher, which will be used on JVM: `withContext(Dispatchers.IO) { … }`. The idea here is that this will be easy to migrate when we do get multi-threaded coroutine support in Kotlin/Native.\n\n### Waiting on Asynchronous Callback-based Work\n\nUse `threadSafeSuspendCallback` to bridge callback-style async work into your code as a `suspend fun`:\n\n```kotlin\n\nsuspend fun performNetworkFetch() {\n  val result = threadSafeSuspendCallback { completion -\u003e\n    // example: fetch network data that isn't coroutine-compatible\n    fetchNetworkData { networkResult -\u003e\n      // notify that async work is complete\n      completion(networkResult)\n    }\n  }\n\n  // result is now available here\n}\n```\n\n## Sample Project\n\nIn the sample directory, there is a sample project that demonstrates adding CoroutineWorker to an iOS + JVM library. We just used the sample library from IntelliJ's template for a \"Mobile Shared Library.\" In the sample is a function called `performWork` (common code) that takes a completion lambda and demonstrates `CoroutineWorker.execute`. In tests, we use K/N concurrency helpers from `kotlinx.atomicfu` to demonstrate capturing a result across threads in K/N and executing this function.\n\n## CoroutineWorker Prefers Frozen State\n\nObject detachment (i.e. [transferring object ownership](https://kotlinlang.org/docs/native-concurrency.html#object-transfer-and-freezing) from one thread to another) is relatively difficult to achieve (outside of simple scenarios) compared to working with objects that are frozen and immutable. Because of this, CoroutineWorker prefers taking the frozen, immutable route:\n\n- Lambdas passed to CoroutineWorker are automatically frozen when they are going to be passed across threads.\n- The result value from `withContext` is also frozen.\n\n### Tips for Working with Frozen State\n\n- Be careful about what your frozen lambdas capture; those objects will be frozen too. Especially, watch for implicit references to `this`.\n- Call [`ensureNeverFrozen()`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.native.concurrent/ensure-never-frozen.html) on objects that you don't expect to ever be frozen.\n\n## IO-Bound Work\n\nIn the JVM world, you typically write code like this for managing IO-bound work with coroutines:\n\n```\nwithContext(Dispatchers.IO) {\n    // IO writes\n}\n```\nSimilar behavior is supported in CoroutineWorker for Kotlin/Native via the `IODispatcher`. To use it in common code, make an `expect val Dispatchers.IO: CoroutineDispatcher` that returns `IODispatcher` for Kotlin/Native and `Dispatchers.IO` for JVM, and pass that to `CoroutineWorker.withContext` when performing IO-bound worker.\n","funding_links":[],"categories":["Libraries"],"sub_categories":["Utility","➿ Asynchronous"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FAutodesk%2Fcoroutineworker","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FAutodesk%2Fcoroutineworker","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FAutodesk%2Fcoroutineworker/lists"}