Giter VIP home page Giter VIP logo

Comments (2)

nomisRev avatar nomisRev commented on June 19, 2024

Hey @pinguinjkeke,

Thanks for the feedback! That pattern seems dangerous :/ In your snippet both the async and CoroutineScope will leak.
To solve that I think this should be written as:

saga {
    saga({
       withContext(Dispatchers.IO)  {
            retry(5) { doSomething() }
        }
    }) {
        // same levels of nesting on rollback
    }
}

It doesn't entirely resolve you problem, but it already makes the indentation a bit smaller. I see you also have indentation configured to 4-spaces, which I am personally not a fan of especially in Kotlin which can be quite heavy on DSLs.
The goal of these monadic DSLs is that they can be safely nested, but that indeed brings some callback-hell style code into play.

Albeit that with callback-hell you can never "escape" the callback, but in this case at some point you'll get the result on the lefthand side like regular imperative code. So this can typically be "solved" in Kotlin by applying some best-practices when working with suspend but doesn't entirely solve this issue of nesting.

For example:

saga {
  val result = doSomething()
}

suspend fun SagaEffect.doSomething(): A = saga({
  withContext(Dispatchers.IO) {
    retry(5) { doSomething() }
  }
}) {  // same levels of nesting on rollback  }

To come back to your original question of "higher order", it will introduce a lot of specialised code in every type similar to Saga for example CircuitBreaker has these similar concerns of CoroutineScope, withContext and retry. The benefit of Kotlin, and its suspend DSLs` is that these can all be nested and composed without having to build separate and duplicated APIs. Which in turn also increases the API surface of every library.

Not sure if my reply helps you in any way, but I think this is out of the scope of this library. Feel free to reply, or ask any questions you might have.

from saga.

pinguinjkeke avatar pinguinjkeke commented on June 19, 2024

Thanks for the detailed answer.

from saga.

Related Issues (10)

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.