GithubHelp home page GithubHelp logo

lavruk / coroutineworker Goto Github PK

View Code? Open in Web Editor NEW

This project forked from autodesk/coroutineworker

0.0 1.0 0.0 97 KB

Kotlin Coroutine-based workers for native

License: Apache License 2.0

Kotlin 100.00%

coroutineworker's Introduction

CoroutineWorker

Build Status

Maven Central

Specs

  • Supported on Native and JVM
  • Kotlin 1.3.31

About

This library helps unify and support coroutine background thread usage in common code for Kotlin/Native, until kotlinx.coroutines has support for native, multi-threaded coroutines. This library doesn't support every use case (but could support more with your help!), but it does support some useful ones:

Spawning Asynchronous Work

val worker = CoroutineWorker.execute {
  // - In here, `this` is a `CoroutineScope`
  // - Run suspend functions, call launch, etc.
  // - This code runs in a thread pool
}

// Tells the worker to cancel (uses standard coroutine cancellation)
worker.cancel()

// Tells the worker to cancel; it suspends until cancellation is finished
worker.cancelAndJoin()

Waiting on Asynchronous Work to Complete

val result = CoroutineWorker.performAndWait {
  // This is similar to execute, but it returns
  // the result of the work at the end of this lambda
  1
}
print(result) // prints 1

Lower-Level Helpers

  • Use threadSafeSuspendCallback to bridge callback-style async work out to your platform back into your library as a suspend fun (see example usages in the library).

Important Notes

  • Closures passed to execute and performAndWait are automatically frozen, so be careful about what your closure captures (e.g. implicit references to this)!
  • The result value from performAndWait is also frozen.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.