2828import org .slf4j .Logger ;
2929import org .slf4j .LoggerFactory ;
3030
31+ import com .linkedin .parseq .function .Action ;
3132import com .linkedin .parseq .function .Consumer1 ;
3233import com .linkedin .parseq .function .Failure ;
3334import com .linkedin .parseq .function .Function1 ;
@@ -234,8 +235,8 @@ default <R> Task<R> flatMap(final Function1<? super T, Task<R>> func) {
234235 * Creates a new task that will run another task as a side effect once the primary task
235236 * completes successfully. The properties of side effect task are:
236237 * <ul>
237- * <li>The side effect task will not be run if the primary task fails or
238- * is canceled .</li>
238+ * <li>The side effect task will not be run if the primary task has not run e.g. due to
239+ * failure or cancellation .</li>
239240 * <li>The side effect does not affect returned task. It means that
240241 * failure of side effect task is not propagated to returned task.</li>
241242 * <li>The returned task is marked done once this task completes, even if
@@ -252,7 +253,8 @@ default <R> Task<R> flatMap(final Function1<? super T, Task<R>> func) {
252253 * Task{@code <String>} userName = id.flatMap("fetch", u -> fetch(u))
253254 * .withSideEffect("update memcache", u -> updateMemcache(u));
254255 * </code></pre>
255- * @param desc description of a function, it will show up in a trace
256+ *
257+ * @param desc description of a side effect, it will show up in a trace
256258 * @param func function to be applied on result of successful completion of this task
257259 * to get side effect task
258260 * @return a new task that will run side effect task specified by given function upon succesful
@@ -261,7 +263,7 @@ default <R> Task<R> flatMap(final Function1<? super T, Task<R>> func) {
261263 default Task <T > withSideEffect (final String desc , final Function1 <? super T , Task <?>> func ) {
262264 ArgumentUtil .requireNotNull (func , "function" );
263265 final Task <T > that = this ;
264- return async (desc , context -> {
266+ return async ("withSideEffect" , context -> {
265267 final Task <?> sideEffectWrapper = async (desc , ctx -> {
266268 Task <?> sideEffect = func .apply (that .get ());
267269 ctx .run (sideEffect );
@@ -274,13 +276,23 @@ default Task<T> withSideEffect(final String desc, final Function1<? super T, Tas
274276 }
275277
276278 /**
277- * Equivalent to {@code withSideEffect("withSideEffect ", func)}.
279+ * Equivalent to {@code withSideEffect("sideEffect ", func)}.
278280 * @see #withSideEffect(String, Function)
279281 */
280282 default Task <T > withSideEffect (final Function1 <? super T , Task <?>> func ) {
281- return withSideEffect ("withSideEffect " , func );
283+ return withSideEffect ("sideEffect " , func );
282284 }
283285
286+ default Task <T > shareable () {
287+ final Task <T > that = this ;
288+ return async ("shareable" , context -> {
289+ final SettablePromise <T > result = Promises .settable ();
290+ context .runSideEffect (that );
291+ Promises .propagateResult (that , result );
292+ return result ;
293+ }, true );
294+ }
295+
284296 /**
285297 * Creates a new task which applies a consumer to the result of this task
286298 * and completes with a result of this task. It is used
@@ -737,7 +749,7 @@ public static <R> Task<R> flatten(final Task<Task<R>> task) {
737749 * Task{@code <Void>} task = Task.action("greeting", () -> System.out.println("Hello"));
738750 * </code></pre>
739751 *
740- * Returned task will fail if {@code Runnable } passed in as a parameter throws
752+ * Returned task will fail if {@code Action } passed in as a parameter throws
741753 * an exception.
742754 * <pre><code>
743755 * // this task will fail with java.lang.ArithmeticException
@@ -748,7 +760,7 @@ public static <R> Task<R> flatten(final Task<Task<R>> task) {
748760 * @param action the action that will be executed when the task is run
749761 * @return the new task that will execute the action
750762 */
751- public static Task <Void > action (final String desc , final Runnable action )
763+ public static Task <Void > action (final String desc , final Action action )
752764 {
753765 ArgumentUtil .requireNotNull (action , "action" );
754766 return async (desc , () -> {
@@ -758,12 +770,12 @@ public static Task<Void> action(final String desc, final Runnable action)
758770 }
759771
760772 /**
761- * Equivalent to {@code action("action", runnable )}.
762- * @see #action(String, Runnable )
773+ * Equivalent to {@code action("action", action )}.
774+ * @see #action(String, Action )
763775 */
764- public static Task <Void > action (final Runnable runnable )
776+ public static Task <Void > action (final Action action )
765777 {
766- return action ("action" , runnable );
778+ return action ("action" , action );
767779 }
768780
769781 /**
0 commit comments