class=”htmledit_views”>
How to use and test
apply plugin: 'java-library'
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
compile 'org.jdeferred.v2:jdeferred-core:2.0.0-beta2'
compile 'org.slf4j:slf4j-simple:1.7.7'
// compile 'org.jdeferred:jdeferred-android-aar:1.2.4'
}
//org.slf4j
// slf4j-simple
//1.7.7
sourceCompatibility = "1.8"
targetCompatibility = "1.8"
Key method
when()
Executed in sub-threads Multiple callbacks can be added. fail()
Failed callback done()
Successful callback always()
Both success and failure will call back progress()
The interceptor of the callback filter()
triggered by calling notify
is executed in the main thread, which can forcefully change a result. resolve
stands for resolution and hooks up with the done() callback reject
stands for rejection and hooks up with the fail callback
Notes
If there is an error in multiple when, it will directly call fail
and will not trigger done
Common categories
Deferred deferred = new DeferredObject();
This kind of object needs to manually call resolve
or reject
Example
Promise promise = deferred. promise();
promise. done(new DoneCallback() {
@Override
public void onDone(Object result) {
System.out.println("done " + result + " " + Thread.currentThread().getName());
}
}).fail(new FailCallback() {
@Override
public void onFail(Object result) {
System.out.println("fail " + result + " " + Thread.currentThread().getName());
}
}).progress(new ProgressCallback() {
@Override
public void onProgress(Object progress) {
System.out.println("progress " + progress + " " + Thread.currentThread().getName());
}
}).always(new AlwaysCallback() {
@Override
public void onAlways(Promise. State state, Object resolved, Object rejected) {
System.out.println("alayw " + state.name() + " resolved:" + resolved + " rejected:" + rejected + " " + Thread.currentThread().getName());
}
});
deferred. notify("1%");
deferred. notify("10%");
deferred.notify("100%"); //notify will call the onProgress thread to call this, but cannot be called after resolution or rejection.
// deferred. resolve("done_str");// done alay
deferred. reject("333");
DeferredManager dm = new DefaultDeferredManager();
From the perspective of the author github, it is used to cooperate with when
and then
,
Call then
after when
DeferredManager dm = new DefaultDeferredManager();
dm.when(new Callable() { //Sub thread.
public Integer call() {
// return something
// or throw a new exception
System.out.println("when CALL thread:" + Thread.currentThread().getName());
return 10;
}
}).then(new DoneCallback() {
@Override
public void onDone(Integer result) {
System.out.println("then result " + result + ",Thread:" + Thread.currentThread().getName());
}
}).done(new DoneCallback() {
public void onDone(Integer result) {
System.out.println("onDone thread:" + Thread.currentThread().getName());
System.out.println("done." + result);
}
}).fail(new FailCallback() {
public void onFail(Throwable e) {
System.out.println("when fail." + e.getMessage());
}
});
All Examples
System.out.println("exec " + Thread.currentThread().getName());
Deferred deferred = new DeferredObject();
Promise promise = deferred. promise();
promise. done(new DoneCallback() {
@Override
public void onDone(Object result) {
System.out.println("done " + result + " " + Thread.currentThread().getName());
}
}).fail(new FailCallback() {
@Override
public void onFail(Object result) {
System.out.println("fail " + result + " " + Thread.currentThread().getName());
}
}).progress(new ProgressCallback() {
@Override
public void onProgress(Object progress) {
System.out.println("progress " + progress + " " + Thread.currentThread().getName());
}}).always(new AlwaysCallback() {
@Override
public void onAlways(Promise. State state, Object resolved, Object rejected) {
System.out.println("alayw " + state.name() + " resolved:" + resolved + " rejected:" + rejected + " " + Thread.currentThread().getName());
}
});
deferred. notify("1%");
deferred. notify("10%");
deferred.notify("100%"); //notify will call the onProgress thread to call this, but cannot be called after resolution or rejection.
deferred.resolve("done_str");// done alay
System.out.println("-=--------------------------");
Deferred d = new DeferredObject();
Promise p = d. promise();
Promise filtered = p.filter(new DoneFilter() {//Process the successful result
public Integer filterDone(Integer result) {
return result * 10;
}
});
filtered. done(new DoneCallback() {
public void onDone(Integer result) {
// result would be original * 10
System.out.println("done result:" + result);
}
});
d.notify(10);//It doesn't matter
d.resolve(3);//The published result will be *10
System.out.println("-=--------------------------");
//-------
//If it is less than 100, it is normal, otherwise return an exception object
d = new DeferredObject();
p = d. promise();
p.pipe(new DonePipe() {
public Deferred pipeDone(Integer result) {
if (result < 100) {
return new DeferredObject().resolve(result);
} else {
return new DeferredObject(). reject(new RuntimeException());
}
}
}).done(new DoneCallback() {
@Override
public void onDone(Object result) {
System.out.println("done:" + result);
}
}).fail(new FailCallback() {
@Override
public void onFail(Object result) {
System.out.println("fail:" + result);
}
});
// d.resolve(15);//call done callback
d.resolve(150);//call fail callback
System.out.println("---------------------------");
DeferredManager dm = new DefaultDeferredManager();
dm.when(new Callable() { //Sub thread.
public Integer call() {
// return something
// or throw a new exception
System.out.println("when CALL thread:" + Thread.currentThread().getName());
return 10;
}
}).then(new DoneCallback() {
@Override
public void onDone(Integer result) {
System.out.println("then result " + result + ",Thread:" + Thread.currentThread().getName());
}
}).done(new DoneCallback() {
public void onDone(Integer result) {
System.out.println("onDone thread:" + Thread.currentThread().getName());
System.out.println("done." + result);
}
}).fail(new FailCallback() {
public void onFail(Throwable e) {
System.out.println("when fail." + e.getMessage());
}
});
System.out.println("will exec over");
System.out.println("-----------------");
dm = new DefaultDeferredManager();
dm.when(//pass 2 parameters
() -> {
return "a";
},
() -> {
return "b";
},
() -> {
return "c";
},
() -> {
return "d";
},
() -> {
return "e";
},
() -> {
return "f";
},
() -> {
throw new RuntimeException("testfail");
},
() -> {
return "g";
}
).progress(new ProgressCallback() {
@Override
public void onProgress(MasterProgress progress) {
System.out.println("test progress");
}
}).done(rs ->//done will be called multiple times
rs.forEach(r -> System.out.println("->" + r.getResult()))
).fail(result -> {
System.err.println("fail cail " + result);
}).always((state, resolved, rejected) -> { //void onAlways(final State state, final D resolved, final F rejected); Multiple parameters in lambda are wrapped in parentheses,
//Status Resolve Reject Whether it is 100 or successful, it will be called
//If successful, the status is resolved. If the failure status is rejected.
System.out.println("always: state:" + state.name());// If it fails to get parameters 2 3, it will crash, "\nresolved:"+resolved.getSecond()+"\nrejected: "+resolved. getSecond()
});
//In this example, if there is a failure, it will not go to done, but go directly to fail. when is running in the child thread.
try {
Thread. sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("exec over");
Execution result:
image.png
Reference project
//If successful, the status is resolved. If the failure status is rejected.
System.out.println(“always: state:” + state.name());// If it fails to get parameters 2 3, it will crash, “\nresolved:”+resolved.getSecond()+”\nrejected: “+resolved. getSecond()
});
//In this example, if there is a failure, it will not go to done, but go directly to fail. when is running in the child thread.
try {
Thread. sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(“exec over”);
Execution result:
image.png
Reference project
https://github.com/jdeferred/jdeferred