Say hello to JDeferred

Inspired by the implementation of promise in jQuery, few Java Engineers have started to develop a library called JDeferred. This implements the concept of promise as robustly as it should be by leaving the gaping holes of the java.util.concurrent package. This was a brief of how JDeferred works. Let's dive deep into what it is and its unique advantages, as compared to other implantations available in the market.

Just like jQuery has a deferred object, JDeferred is designed in a similar way to behave and contact with Java's compiler. JDeferred is not only similar with jQuery's implementation of promise, but it also extends its support to the Android Deferred Object. Chapter 8, Promises in jQuery is the dedicated chapter on jQuery and its mechanism and working on promise, so we can skip that part for now and see what the Android Deferred Object is, and how it fits into the implementation of promise.

A few words about Android Deferred Object

It would be unfair not to showcase the existence of Android Deferred Object and its properties when we are discussing JDeferred. The Android Deferred Object is a utility or more simply, it is a chainable utility object that can actually do all the same stuff for the Android domain. It can register multiple callbacks in a single callback queue; it can invoke callback queues and after processing. It also can relay the state of success or failure to whichever function is waiting for; it doesn't matter whether it's a synchronous function or an asynchronous function.

Its working is quite straightforward. You obtain a promise out of a function that was executed asynchronously. As we can work around with promise, you can attach callbacks to get notified about the success or failure. Whenever this piece of program that was working asynchronously finishes off as expected, the promise is called to be resolved in case of any error; it calls the rejected parameter.

Use case 1 – object success and failure callbacks for a task

Say that you need an asynchronous HTTP request. A simple way of using Android Deferred Object is to wrap the request in to DeferredAsyncTask and attach callbacks to your action. Here is the code for such a scenario:

new DeferredAsyncTask<HttpResponse,HttpResponse,Void>() {
  protected abstract Resolved doInBackground() throws Exception {
    //do your async code here
  }
}
.done( new ResolveCallback<HttpResponse> {
  public void onResolve(HttpResponse resolved) {
    //your success code here
  }
})
.fail ( new RejectCallback<HttpResponse> {
  public void onReject(HttpResponse rejected) {
     //your failure code here
  }
});

The reference to preceding code is available at https://github.com/CodeAndMagic/android-deferred-object.

Use case 2 – merging several promises

This use case is best for when you need to add several executed promises into a single one by merging them as a single promise. A convenient way is to call the DeferredObject.when method:

Promise<A1,B1,C1> p1 = new DeferredAsyncTask<A1,B1,C1>() { ... }; 
Promise<A2,B2,C2> p1 = new DeferredAsyncTask<A2,B2,C2>() { ... };
Promise<A3,B3,C3> p3 = new DeferredAsyncTask<A3,B3,C3>() { ... };
//when gives you a new promise that gets triggered when all the merged promises are resolved or one of them fails
DeferredObject.when(p1,p2,p3)
.done(new ResolveCallback<MergedPromiseResult3<A1,A2,A3>() {
  public void onResolve(MergedPromiseResult3<A1,A2,A3> resolved){
    Log.i(TAG, "got: " + resolved.first() + resolved.second() + resolved.third());
  }
})
.fail(new RejectCallback<MergedPromiseReject>() {
  public void onReject(MergedPromiseReject rejected) {
    //failure handling here
  }
})
.progress(new ProgressCallback<MergedPromiseProgress>() {
  public void onProgress(final MergedPromiseProgress progress){
    //you get notified as the merged promises keep coming in
  }
});
//Merging doesn't stop you do add individual callbacks for promises that are in the merge
p1.done(...).fail(...)
//Or even merging them in another way
DeferredObject.when(p1,p2).done(...).fail(...)
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.129.45.92