Comments (2)
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.
Thanks for the detailed answer.
from saga.
Related Issues (10)
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from saga.