jdeferred / jdeferred

Java Deferred/Promise library similar to JQuery.

Home Page:jdeferred.org

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Migrating from org.jdeferred 1.2.6 to 2.0.0

JimClermonts opened this issue · comments

I'm in the process of migrating from:
implementation 'org.jdeferred:jdeferred-android-aar:1.2.6'
to:
implementation 'org.jdeferred.v2:jdeferred-android-aar:2.0.0'

After replacing org.jdeferred with org.jdeferred2 everywhere, I got a last issue in a class named DeferredObjectWithExceptions. This extends DeferredObject and overrides the then method but this doesn't work anymore. And the new overridden methods don't work with my PipedPromiseWithExceptions class.

First I get this errror:

Type inference failed: constructor PipedPromiseWithExceptions<D, F, P, D_OUT, F_OUT, P_OUT>(promise: Promise<D#1 (type parameter of PipedPromiseWithExceptions), F#1 (type parameter of PipedPromiseWithExceptions), P#1 (type parameter of PipedPromiseWithExceptions)>, doneFilter: DonePipe<D#1, D_OUT#1 (type parameter of PipedPromiseWithExceptions), F_OUT, P_OUT>?, failFilter: FailPipe<F#1, D_OUT#1, F_OUT, P_OUT>?, progressFilter: ProgressPipe<P#1, D_OUT#1, F_OUT, P_OUT>?)
cannot be applied to
(DeferredObjectWithExceptions<D#2 (type parameter of DeferredObjectWithExceptions), F#2 (type parameter of DeferredObjectWithExceptions), P#2 (type parameter of DeferredObjectWithExceptions)>,DoneFilter<in D#2, out D_OUT#2 (type parameter of DeferredObjectWithExceptions.then)>?,Nothing?,Nothing?)

This is my code file:

import org.jdeferred2.DonePipe
import org.jdeferred2.FailPipe
import org.jdeferred2.ProgressPipe
import org.jdeferred2.Promise

class PipedPromiseWithExceptions<D, F, P, D_OUT, F_OUT, P_OUT>(
        promise: Promise<D, F, P>,
        doneFilter: DonePipe<D, D_OUT, F_OUT, P_OUT>?,
        failFilter: FailPipe<F, D_OUT, F_OUT, P_OUT>?,
        progressFilter: ProgressPipe<P, D_OUT, F_OUT, P_OUT>?) : DeferredObjectWithExceptions<D_OUT, F_OUT, P_OUT>(), Promise<D_OUT, F_OUT, P_OUT> {
    init {
        promise.done { result ->
            if (doneFilter != null) {
                pipe(doneFilter.pipeDone(result))
            } else {
                // Unchecked cast... but yea that is how PipedPromise.java normally works.
                @Suppress("UNCHECKED_CAST")
                resolve(result as D_OUT)
            }
        }.fail { result ->
            if (failFilter != null) {
                pipe(failFilter.pipeFail(result))
            } else {
                // Unchecked cast... but yea that is how PipedPromise.java normally works.
                @Suppress("UNCHECKED_CAST")
                reject(result as F_OUT)
            }
        }.progress { progress ->
            if (progressFilter != null) {
                pipe(progressFilter.pipeProgress(progress))
            } else {
                // Unchecked cast... but yea that is how PipedPromise.java normally works.
                @Suppress("UNCHECKED_CAST")
                notify(progress as P_OUT)
            }
        }
    }

    protected fun pipe(promise: Promise<D_OUT, F_OUT, P_OUT>): Promise<D_OUT, F_OUT, P_OUT> {
        promise.done { result -> resolve(result) }.fail { result -> reject(result) }.progress { progress -> notify(progress) }
        return promise
    }
}

When changing

DonePipe<D, D_OUT, F_OUT, P_OUT>? -> DoneFilter<in D, out D_OUT>?
FailPipe<F, D_OUT, F_OUT, P_OUT>? -> FailFilter<in F, out F_OUT>?
ProgressPipe<P, D_OUT, F_OUT, P_OUT>? -> ProgressFilter<in P, out P_OUT>?

I have an error in
protected fun pipe(promise: D_OUT)

How can I keep this working the way it was working in the old version?

import org.jdeferred2.*
import org.jdeferred2.impl.DeferredObject

open class DeferredObjectWithExceptions<D, F, P> : DeferredObject<D, F, P>() {

    override fun <D_OUT : Any?> then(doneFilter: DoneFilter<in D, out D_OUT>?): Promise<D_OUT, F, P> {
        return PipedPromiseWithExceptions(this, doneFilter, null, null)
    }

    override fun <D_OUT : Any?, F_OUT : Any?> then(doneFilter: DoneFilter<in D, out D_OUT>?, failFilter: FailFilter<in F, out F_OUT>?): Promise<D_OUT, F_OUT, P> {
        return PipedPromiseWithExceptions(this, doneFilter, failFilter, null)
    }

    override fun <D_OUT : Any?, F_OUT : Any?, P_OUT : Any?> then(doneFilter: DoneFilter<in D, out D_OUT>?, failFilter: FailFilter<in F, out F_OUT>?, progressFilter: ProgressFilter<in P, out P_OUT>?): Promise<D_OUT, F_OUT, P_OUT> {
        return PipedPromiseWithExceptions(this, doneFilter, failFilter, progressFilter)
    }

// This was working in 1.2.6:
//    override fun <D_OUT, F_OUT, P_OUT> then(doneFilter: DonePipe<D, D_OUT, F_OUT, P_OUT>): Promise<D_OUT, F_OUT, P_OUT> {
//        return PipedPromiseWithExceptions(this, doneFilter, null, null)
//    }
//
//    override fun <D_OUT, F_OUT, P_OUT> then(doneFilter: DonePipe<D, D_OUT, F_OUT, P_OUT>, failFilter: FailPipe<F, D_OUT, F_OUT, P_OUT>): Promise<D_OUT, F_OUT, P_OUT> {
//        return PipedPromiseWithExceptions(this, doneFilter, failFilter, null)
//    }
//
//    override fun <D_OUT, F_OUT, P_OUT> then(doneFilter: DonePipe<D, D_OUT, F_OUT, P_OUT>, failFilter: FailPipe<F, D_OUT, F_OUT, P_OUT>, progressFilter: ProgressPipe<P, D_OUT, F_OUT, P_OUT>): Promise<D_OUT, F_OUT, P_OUT> {
//        return PipedPromiseWithExceptions(this, doneFilter, failFilter, progressFilter)
//    }
}