Module Fmlib_browser.Task

Tasks to be performed within Commands

Error types

type empty =
  1. | []
type http_error = [
  1. | `Http_status of int
    • 0: no internet, server not found, timeout, ...
    • 401: bad request
    • 403: forbidden
    • 404: page not found
    • ...
  2. | `Http_no_json

    Resource is not a valid json file

  3. | `Http_decode

    Resource is a valid json file, but the decoder could not decode the corresponding javascript object.

type not_found = [
  1. | `Not_found

Basic type and functions

type ('a, +'e) t

Task succeeding with a value of type 'a or failing with an error object of type 'e

val succeed : 'a -> ('a, 'e) t

succeed a Task which immediately succeeds with value a.

val return : 'a -> ('a, 'e) t

Same as succeed.

val fail : 'e -> ('a, 'e) t

fail e Task which immediately fails with the error e.

val result : ('a, 'e) Stdlib.result -> ('a, 'e) t

result res Task which immediately succeeds or fails depending on res

The effect of the function is described by the code

match res with
| Ok a    -> succeed a
| Error e -> fail e
val (>>=) : ('a, 'e) t -> ('a -> ('b, 'e) t) -> ('b, 'e) t

task >>= f

First execute task. If it fails then the function fails. If task succeeds with the result a then execute the task f a.

val let* : ('a, 'e) t -> ('a -> ('b, 'e) t) -> ('b, 'e) t

More convenient syntax for the monadic bind operator (>>=).

The code

let* a = task in
f a

is equivalent to

task >>= f

With the let* operator it is more convenient to chain tasks.

let* a = t1 in
let* b = t2 a in
let* c = t3 a b in
return f a b c ...
val map : ('a -> 'b) -> ('a, 'e) t -> ('b, 'e) t

map f task Map the success result of task via the function f.

val make_succeed : (('a, 'e) Stdlib.result -> 'b) -> ('a, 'e) t -> ('b, empty) t

make_succeed f task

Convert the task which might fail into a task which always succeeds by converting the positive or negative result via the function f into a new result.

val parallel : 'accu -> ('a -> 'accu -> 'accu) -> ('a, empty) t list -> ('accu, empty) t

parallel accu_start accumulate task_list

Run all the tasks in the task list in parallel. Collect the results of the individual tasks via the function accumulate into the accumulator. If all tasks of the list have finished, return the accumulator.

Note that the tasks of the list do not return errors. If they can have errors then make_succeed can be used to encode the error into the result type 'a.

Write to the console

val log_string : string -> (unit, 'e) t

log_string str Write str to the console.

val log_value : Value.t -> (unit, 'e) t

log_value v Write the javascript object v to the console.

Messages to the javascript world

val send_to_javascript : Value.t -> (unit, 'e) t

send_to_javascript value Send the javascript object value to the surrounding javascript world.

Focus and blur elements

val focus : string -> (unit, not_found) t

focus id Put the dom element with id into focus.

val blur : string -> (unit, not_found) t

blur id Unfocus the dom element with id.

Defer tasks a certain time

val sleep : int -> 'a -> ('a, 'e) t

sleep millis a Sleep for millis milliseconds and then return a.


let* _ = sleep 1000 () in       (* sleep 1000 milliseconds *)
task                            (* and then execute [task] *)

let* a = task1 >>= sleep 1000   (* excute [task1] and return result
                                   [a] after 1000 milliseconds *)
task2 a                         (* then execute [task2 a] *)
val next_tick : 'a -> ('a, 'e) t

next_tick a Return a in the next tick of the event loop.

Example: Execute task in the next round of the event loop.

let* _ = next_tick () in

Time and time zone

val now : (Time.t, 'e) t

Get the current time.

val time_zone : (Time.Zone.t, 'e) t

Get the current time zone.

Random values

val random : 'a Random.t -> ('a, 'e) t

random ran Execute the random generator rand and return the generated random value.

Http requests

val http_text : string -> string -> (string * string) list -> string -> (string, http_error) t

http_text method url headers body

Make a http method request to url with headers and body. Expect the response as a string.

Method is one of GET, POST, DELETE, ... .

The headers and the body can be empty.

val http_json : string -> string -> (string * string) list -> string -> 'a Decoder.t -> ('a, http_error) t

http_json method url headers body decoder Same as http_text but the response is expected as a valid json string which is converted to a javascript object and decoded by decoder.