Interface Flow


public interface Flow
An interface to execute a list of asynchronous tasks in parallel or sequentially, or to execute a single task repeatedly as long as certain conditions are met.

The tasks share a context that can be used to store data in a map or on a stack.

// datetime format is "2022-03-31T11:03:39.348365+02:00"
Task<FlowContext> currentTime = context -> fetch("https://worldtimeapi.org/api/timezone/Europe/Berlin")
        .then(Response::json)
        .then(json -> Promise.resolve(Js.<JsPropertyMap<String>>cast(json).get("datetime").substring(11, 23)))
        .then(context::resolve);
double ms = 500 + new Random().nextInt(2_000);
Task<FlowContext> delay = context -> new Promise<>((res, __) -> setTimeout(___ -> res.onInvoke(context), ms));

// execute the two tasks in sequence and cancel after 1_000 ms
Flow.parallel(new FlowContext(), List.of(currentTime, delay))
        .timeout(1_000)
        .subscribe(context -> console.log("Current time: " + context.pop("n/a")));
  • Method Details

    • parallel

      static <C extends FlowContext> Sequence<C> parallel(C context, List<Task<C>> tasks)
      Executes a list of asynchronous tasks in parallel (all at once).
      // datetime format is "2022-03-31T11:03:39.348365+02:00"
      Task<FlowContext> currentTime = context -> fetch("https://worldtimeapi.org/api/timezone/Europe/Berlin")
              .then(Response::json)
              .then(json -> Promise.resolve(Js.<JsPropertyMap<String>>cast(json).get("datetime").substring(11, 23)))
              .then(context::resolve);
      double ms = 500 + new Random().nextInt(2000);
      Task<FlowContext> delay = context -> new Promise<>((res, __) -> setTimeout(___ -> res.onInvoke(context), ms));
      
      // execute the two tasks in parallel
      Flow.parallel(new FlowContext(), List.of(currentTime, delay))
              .subscribe(context -> console.log("Current time: " + context.pop("n/a")));
      
      Type Parameters:
      C - the type of the shared context
      Parameters:
      context - the context shared between tasks
      tasks - the list of tasks to execute in parallel
      Returns:
      an interface to control whether the execution of the tasks should fail fast or fail last
    • sequential

      static <C extends FlowContext> Sequence<C> sequential(C context, List<Task<C>> tasks)
      Executes a list of asynchronous tasks in sequence (one after the other).
      // datetime format is "2022-03-31T11:03:39.348365+02:00"
      Task<FlowContext> currentTime = context -> fetch("https://worldtimeapi.org/api/timezone/Europe/Berlin")
              .then(Response::json)
              .then(json -> Promise.resolve(Js.<JsPropertyMap<String>>cast(json).get("datetime").substring(11, 23)))
              .then(context::resolve);
      double ms = 500 + new Random().nextInt(2_000);
      Task<FlowContext> delay = context -> new Promise<>((res, __) -> setTimeout(___ -> res.onInvoke(context), ms));
      
      // execute the two tasks in sequence and cancel after 1_000 ms
      Flow.parallel(new FlowContext(), List.of(currentTime, delay))
              .timeout(1_000)
              .subscribe(context -> console.log("Current time: " + context.pop("n/a")));
      
      Type Parameters:
      C - the type of the shared context
      Parameters:
      context - the context shared between tasks
      tasks - the list of tasks to execute in order
      Returns:
      an interface to control whether the execution of the tasks should fail fast or fail last
    • repeat

      static <C extends FlowContext> Repeat<C> repeat(C context, Task<C> task)
      Executes the given task repeatedly as long as the conditions defined by Repeat are met.
      Task<FlowContext> currentTime = context -> fetch("https://worldtimeapi.org/api/timezone/Europe/Berlin")
              .then(Response::json)
              .then(json -> Promise.resolve(Js.<JsPropertyMap<String>>cast(json).get("datetime").substring(11, 23)))
              .then(context::resolve);
      
      // fetch the current time until the milliseconds end with "0" and cancel after 5 iterations
      Flow.repeat(new FlowContext(), currentTime)
              .while_(context -> !context.pop("").endsWith("0"))
              .iterations(5)
              .subscribe(context -> console.log("Current time: " + context.pop("n/a")));
      
      Type Parameters:
      C - the type of the shared context
      Parameters:
      context - the context shared between the iterations
      task - the task to execute while the predicate evaluates to true
      Returns:
      an interface to control the interval, timeout and fail fast behaviour