Skip to content

Commit a1f7097

Browse files
committed
add default looper and possibility chain promises
1 parent 39c85a3 commit a1f7097

File tree

2 files changed

+38
-5
lines changed

2 files changed

+38
-5
lines changed
Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,33 @@
11
package eu.codlab.simplepromise;
22

3-
import eu.codlab.simplepromise.solve.PromiseExec;
3+
import android.support.annotation.NonNull;
4+
import android.support.annotation.Nullable;
45

5-
/**
6-
* Created by kevinleperf on 15/04/2018.
7-
*/
6+
import eu.codlab.simplepromise.solve.PromiseExec;
7+
import eu.codlab.simplepromise.solve.Solver;
88

99
public abstract class AbstractPromise<TYPE_EXECUTE> {
1010

1111
protected abstract <TYPE_RESULT> PromiseInOut<TYPE_EXECUTE, TYPE_RESULT>
1212
then(PromiseExec<TYPE_EXECUTE, TYPE_RESULT> to_resolve);
13+
14+
/**
15+
* Let possible to resolve a promise in chain
16+
*
17+
* This notation will discard the result from the previous
18+
*
19+
* @param to_resolve the promise to make the new chain
20+
* @param <TYPE_RESULT> the type of result from the promise
21+
*
22+
* @return an object resolvable
23+
*/
24+
protected <TYPE_RESULT> PromiseInOut<TYPE_EXECUTE, TYPE_RESULT>
25+
then(final Promise<TYPE_RESULT> to_resolve) {
26+
return new PromiseInOut<>(new PromiseExec<TYPE_EXECUTE, TYPE_RESULT>() {
27+
@Override
28+
public void onCall(@Nullable TYPE_EXECUTE result, @NonNull Solver<TYPE_RESULT> solver) {
29+
solver.resolve(to_resolve);
30+
}
31+
});
32+
}
1333
}

promise/src/main/java/eu/codlab/simplepromise/Promise.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
package eu.codlab.simplepromise;
22

33
import android.os.Handler;
4+
import android.os.Looper;
45
import android.support.annotation.NonNull;
6+
import android.support.annotation.Nullable;
57

68
import java.util.List;
79

810
import eu.codlab.simplepromise.solve.ErrorPromise;
911
import eu.codlab.simplepromise.solve.PromiseExec;
1012
import eu.codlab.simplepromise.solve.PromiseSolver;
13+
import eu.codlab.simplepromise.solve.Solver;
1114

1215
public class Promise<TYPE> extends AbstractPromise<TYPE> {
1316

@@ -16,7 +19,7 @@ public static <T> Promise<List<T>> all(AbstractPromise<T>...promises) {
1619
}
1720

1821
@NonNull
19-
private static Handler sHandler = new Handler();
22+
private static Handler sHandler = new Handler(Looper.getMainLooper());
2023

2124
public static void setHandler(@NonNull Handler handler) {
2225
sHandler = handler;
@@ -45,6 +48,16 @@ public <TYPE_RESULT> PromiseInOut<TYPE, TYPE_RESULT> then(PromiseExec<TYPE, TYPE
4548
return then(new PromiseInOut<>(to_resolve));
4649
}
4750

51+
public <TYPE_RESULT> PromiseInOut<TYPE, TYPE_RESULT> then(final TYPE_RESULT resolved) {
52+
return then(new PromiseExec<TYPE, TYPE_RESULT>() {
53+
@Override
54+
public void onCall(@Nullable TYPE result, @NonNull Solver<TYPE_RESULT> solver) {
55+
solver.resolve(resolved);
56+
}
57+
});
58+
}
59+
60+
4861
public <TYPE_RESULT> void error(ErrorPromise to_error) {
4962
PromiseInOut<TYPE, TYPE_RESULT> promise_inout = new PromiseInOut<>(to_error);
5063
promise_inout.setParent(mPromiseInOut);

0 commit comments

Comments
 (0)