Source file task_intf.ml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
module type S =
sig
    (** Tasks to be performed within {{!module:Command} Commands} *)

    type _ random
    type time
    type time_zone
    type file
    type _ nav_key
    type value
    type _ decoder
    type http_error
    type http_body
    type _ http_expect

    (** {1 Error types} *)

    type empty = |


    type not_found  = [`Not_found]


    type read_failed = [`Read_failed]



    (** {1 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) 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) 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].
    *)





    (** {1 Write to the console} *)

    val log_string: string -> (unit, 'e) t
    (** [log_string str] Write [str] to the console. *)


    val log_value: value -> (unit, 'e) t
    (** [log_value v] Write the javascript object [v] to the console. *)





    (** {1 Messages to the javascript world} *)

    val send_to_javascript: value -> (unit, 'e) t
    (** [send_to_javascript value] Send the javascript object [value] to the
        surrounding javascript world. *)



    (** {1 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]. *)




    (** {1 Defer tasks a certain time} *)

    val sleep: int -> 'a -> ('a, 'e) t
    (** [sleep millis a] Sleep for [millis] milliseconds and then return [a].

        Examples:

        {[
            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 *)
            in
            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
            task
        ]}
    *)




    (** {1 Time and time zone} *)

    val now: (time, 'e) t
    (** Get the current time. *)

    val time_zone: (time_zone, 'e) t
    (** Get the current time zone. *)




    (** {1 Random values} *)

    val random: 'a random -> ('a, 'e) t
    (** [random ran] Execute the random generator [rand] and return the
        generated random value. *)




    (** {1 Navigation} *)

    val load: string -> (unit, empty) t
    (** [load url]

        Load the given [url].
    *)

    val reload: (unit, empty) t
    (** Reload the current page. *)




    (** {1 File operations} *)

    val select_file: string list -> (file, empty) t
    (** [select_file media_types]
        Show the browser's file selection dialog and return a file when the user
        selected one. The given list of [media_types] allows restricting what
        file types are visible in the dialog (users can still select different
        file types if they want to).

        NOTE: This task only works if it is triggered in reaction to a user
        event, such as a mouse click. This restriction is imposed by browsers
        for security reasons (websites should not be able to ask for file access
        without user interaction).
    *)

    val select_files: string list -> (file list, empty) t
    (** [select_files media_types]
        The same as {!select_file} but allows selecting multiple files at once.

        NOTE: This task only works if it is triggered in reaction to a user
        event, such as a mouse click. This restriction is imposed by browsers
        for security reasons (websites should not be able to ask for file access
        without user interaction).
    *)

    val file_text: file -> (string, read_failed) t
    (** [file_text file f]

        Read the contents of [file] into a string. Reading can fail, e.g. in
        case of missing filesystem permissions.
    *)




    (** {1 Http requests} *)

    val http_request:
        string
        -> string
        -> (string * string) list
        -> http_body
        -> 'a http_expect
        -> ('a, http_error) t
    (** [http_request method url headers body expect]

        Make an http [method] request to [url] with [headers] and [body].
        [expect] specifies the expected response format.

        This is the most general http request function. See also the more
        specific functions [http_text] and [http_json].

        Example:
        {[
            let user = Value.(record [| ("username", string "Bob") |]) in
            http_request "PUT" "/users" [] (Body.json user) (Expect.string)
            |> Command.attempt (fun result ->
                match result with
                | Ok _ ->
                    GotUserCreated
                | Error _ ->
                    GotError "failed to create user")
        ]}
    *)

    val http_text:
        string
        -> string
        -> (string * string) list
        -> string
        -> (string, http_error) t
    (** [http_text method url headers body]

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

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

        The headers and the body can be empty. The [Content-Type] header
        is automatically set to [text/plain].

        Example:
        {[
            http_text "PUT" "/users" [] "Bob"
            |> Command.attempt (fun result ->
                match result with
                | Ok _ ->
                    GotUserCreated
                | Error _ ->
                    GotError "failed to create user")
        ]}
    *)

    val http_json:
        string
        -> string
        -> (string * string) list
        -> value option
        -> 'a decoder
        -> ('a, http_error) t
        (** [http_json method url headers body decoder]

            Make an http [method] request to [url] with [headers] and an
            optional json value as the [body]. Expect a json value as the
            response which will be decoded by [decoder].

            The [headers] can be empty. The [Content-Type] header is
            automatically set to [application/json] if [body] is not [None].

            Example:
            {[
                let decoder = Decoder.array Decoder.string in
                http_json "GET" "/users" [] None decoder
                |> Command.attempt (fun result ->
                    match result with
                    | Ok usernames -> (* the usernames were successfully decoded
                                         into a string array *)
                        GotUsers (Array.to_list usernames)
                    | Error _ ->
                        GotError "failed to obtain users")
            ]}
        *)
end