Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/zio/interop-monix


https://github.com/zio/interop-monix

interoperability monix scala zio

Last synced: 3 months ago
JSON representation

Awesome Lists containing this project

README

        

[//]: # (This file was autogenerated using `zio-sbt-website` plugin via `sbt generateReadme` command.)
[//]: # (So please do not edit it manually. Instead, change "docs/index.md" file or sbt setting keys)
[//]: # (e.g. "readmeDocumentation" and "readmeSupport".)

# ZIO Interop Monix

This library provides interoperability between **Monix 3.4** and **ZIO 1 and ZIO 2**. Both JVM and Scala.js are supported.

[![Production Ready](https://img.shields.io/badge/Project%20Stage-Production%20Ready-brightgreen.svg)](https://github.com/zio/zio/wiki/Project-Stages) ![CI Badge](https://github.com/zio/interop-monix/workflows/CI/badge.svg) [![Sonatype Releases](https://img.shields.io/nexus/r/https/oss.sonatype.org/dev.zio/zio-interop-monix_2.13.svg?label=Sonatype%20Release)](https://oss.sonatype.org/content/repositories/releases/dev/zio/zio-interop-monix_2.13/) [![Sonatype Snapshots](https://img.shields.io/nexus/s/https/oss.sonatype.org/dev.zio/zio-interop-monix_2.13.svg?label=Sonatype%20Snapshot)](https://oss.sonatype.org/content/repositories/snapshots/dev/zio/zio-interop-monix_2.13/) [![javadoc](https://javadoc.io/badge2/dev.zio/interop-monix-docs_2.13/javadoc.svg)](https://javadoc.io/doc/dev.zio/interop-monix-docs_2.13) [![ZIO Interop Monix](https://img.shields.io/github/stars/zio/interop-monix?style=social)](https://github.com/zio/interop-monix)

## Installation

In order to use this library, we need to add the following line in our `build.sbt` file:

```scala
libraryDependencies += "dev.zio" %% "zio-interop-monix" % "3.4.2.1.1"
```

## Tasks

Monix tasks can be converted to ZIO tasks:

```scala
import zio._
import zio.interop.monix._
import monix.eval

val monixTask: eval.Task[String] = ???

val zioTask: Task[String] = ZIO.fromMonixTask(monixTask)
```

The conversion is lazy: the Monix task will only be executed if the returned ZIO task is executed.

ZIO tasks can be converted to Monix tasks:

```scala
import zio._
import zio.interop.monix._
import monix.eval
import monix.execution.Scheduler.Implicits.global

val zioTask: Task[String] = ???

val createMonixTask: UIO[eval.Task[String]] = zioTask.toMonixTask()

// illustrative, you wouldn't usually do things this way
val monixTask: eval.Task[String] = Runtime.default.unsafeRun(createMonixTask)
val stringResult = monixTask.runSyncUnsafe
```

The conversion is lazy: the ZIO effect so converted will only be executed if the returned Monix task is executed.

Sometimes you need to provide a Monix task in a context where using a ZIO effect is difficult. For example, when an API requires you to provide a function that returns a Monix task. In these situations, the `toMonixTaskUsingRuntime` method can be used:

```scala
import zio._
import zio.interop.monix._
import monix.eval

def monixBasedApi(f: String => eval.Task[Unit]): eval.Task[Unit] = ???

def zioBasedProcessor(s: String): Task[Unit] = ???

val zioEffects = for {
zioRuntime <- ZIO.runtime[Any]
_ <- ZIO.fromMonixTask {
monixBasedApi(s =>
zioBasedProcessor(s).toMonixTaskUsingRuntime(zioRuntime)
)
}
} yield ()
```

Cancellation/Interruption is propagated between the effect systems. Interrupting a ZIO task based on a Monix task will cancel the underlying Monix task and vice-versa. Be aware that ZIO interruption does not return until cancellation effects have completed, whereas Monix cancellation returns as soon as the signal is sent, without waiting for the cancellation effects to complete.

## Monix Scheduler

Sometimes it is useful to have a Monix `Scheduler` available for interop purposes. The `Runtime#monixScheduler` method will create a scheduler that shares its execution context with the ZIO runtime:

```scala
import zio._
import zio.interop.monix._
import monix.execution.Scheduler

ZIO.runtime[Any].flatMap { runtime =>
implicit val monixScheduler: Scheduler = runtime.monixScheduler()

// do Monixy things
}
```

## Documentation

Learn more on the [ZIO Interop Monix homepage](https://zio.dev/interop-monix/)!

## Contributing

For the general guidelines, see ZIO [contributor's guide](https://zio.dev/about/contributing).

## Code of Conduct

See the [Code of Conduct](https://zio.dev/about/code-of-conduct)

## Support

Come chat with us on [![Badge-Discord]][Link-Discord].

[Badge-Discord]: https://img.shields.io/discord/629491597070827530?logo=discord "chat on discord"
[Link-Discord]: https://discord.gg/2ccFBr4 "Discord"

## License

[License](LICENSE)