inventory-framework icon indicating copy to clipboard operation
inventory-framework copied to clipboard

Support suspendable functions on Kotlin DSL

Open DevNatan opened this issue 2 years ago • 0 comments

Allow that when using the Kotlin module the user can use it in a coroutine, asynchronous functions like PlatformViewFrame#open implemented in #76.

In all functions the user must pass a coroutine context as the IF has no way of handling this because it is just a DSL for Kotlin and not a wrapper with a CoroutineScope attached to it.

Examples show how it is used today (current on Java code, current on Kotlin code and how it will be used with coroutines)

Asynchronous Opening

@Override
protected void onOpen(OpenViewContext context) {
    CompletableFuture<?> job = myService.someRandomAsynchronousJob();
    context.waitUntil(job);
}
override protected fun onOpen(context: OpenViewContext) {
    val future = myService.someRandomAsynchronousJob()
    context.waitUntil(future);
}

We cannot add a suspend to onOpen since it is Java code.
waitUntil will return a Job and it will be cancellable, exceptions will be propagated to the view error handler

override protected fun onOpen(context: OpenViewContext) {
    context.waitUntil(coroutineContext) {
        /* suspend context */
        myService.someRandomAsynchronousJob()
    }
}

Asynchronous Pagination

In asynchronous paging the user provides a CompletableFuture too, we will make it so that he can provide a Deferred for a non-suspended context or the object itself in a suspended context.

setSourceAsync(context -> CompletableFuture);
setSourceAsync { CompletableFuture } // implicit `it` is a PaginatedViewContext
setSourceAsync { Deferred } // non-suspendable context
setSourceAsync(coroutineContext = ...) { T } // suspendable context

Maybe we have to change the name to setSourceSuspend because of the method signature

setSourceSuspend(coroutineContext = ...) { T } // suspendable context

DevNatan avatar Jun 29 '22 14:06 DevNatan