123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323moduletypeS=sig(** Commands to be executed.
An elementary command consists of a {!module:Task} to be executed.
*)typetimetypetime_zonetype_randomtypevaluetypefiletype_nav_keytypeemptytyperead_failedtypehttp_errortype_http_expecttypehttp_bodytype(_,_)tasktype_html(** {1 Basics} *)type_t(** [msg t] is the type of a command generating an object of type [msg] to
inject it into the update function of the application. *)valnone:_t(** An empty command. *)valbatch:'mtlist->'mt(** [batch lst] A list of commands to be executed. *)valmap:('a->'b)->'at->'bt(** Map the message of a command. *)(** {1 Simple Commands} *)(** {2 Time and Time Zone} *)valnow:(time->'m)->'mt(** Get the current time. *)valtime_zone:(time_zone->'m)->'mt(** Get the time zone. *)(** {2 Focus and Blur} *)valfocus:string->'mt(** [focus id]
Focus the element [id]. If the element does not exist, then nothing is
done. This command does not return any message.
*)valblur:string->'mt(** [blur id]
Blur the element [id]. If the element does not exist, then nothing is
done. This command does not return any message.
*)valfocus_with_info:string->'m->'m->'mt(** [focus_with_info id ok not_found]
Focus the element [id] and return [ok]. Return [not_found], if the
element does not exist.
*)valblur_with_info:string->'m->'m->'mt(** [blur_with_info id ok not_found]
Blur the element [id] and return [ok]. Return [not_found], if the
element does not exist.
*)(** {2 File Operations} *)valselect_file:stringlist->(file->'m)->'mt(** [select_file media_types f]
Show the browser's file selection dialog and produce [f file] when the
user selected a file. 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 command 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).
*)valselect_files:stringlist->(filelist->'m)->'mt(** [select_files media_types f]
The same as {!select_file} but allows selecting multiple files at once.
NOTE: This command 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).
*)valfile_text:file->((string,read_failed)result->'m)->'mt(** [file_text file f]
Read the contents of [file] into a string [result] and produce the
message [f result] when reading has finished. Reading can fail, e.g. in
case of missing filesystem permissions.
*)(** {2 Logging to the Console} *)vallog_string:string->'mt(** Print a string to the console, don't return a message. *)vallog_value:value->'mt(** Print a value to the console, don't return a message. *)(** {2 Random Values} *)valrandom:'mrandom->'mt(** Generate a random value. *)(** {2 Send Messages} *)valnotify:int->'m->'mt(** [notify millis msg]
Send [msg] in [millis] milliseconds.
*)valsend_to_javascript:value->'mt(** Send a value to the surrounding javascript code. *)(** {2 Navigation } *)valpush_url:'mnav_key->string->'mt(** [push_url navigation_key url]
Set the browser address bar to [url] and add an entry to the browser
history. This requires a [navigation_key] which is only available in
{{!application}full web applications}. See {!Navigation.key} for
details.
NOTE: For security reasons browsers don't allow setting an invalid URL
or a URL with a different origin (protocol, hostname and port) than the
current URL. Browsers may also enforce a rate limit for changing the
URL. This means that if [url] is not a valid URL of the same origin or
if we try to change the URL too frequently, an exception is thrown.
*)valreplace_url:'mnav_key->string->'mt(** [replace_url navigation_key url]
Like [push_url], but do not add a new entry to the browser history.
This is useful for changing the [query] part of the URL according to
user input, e.g. [?search=hats]. If [push_url] was used in this case,
{{!Command.back}navigating back} would undo a single keystroke whereas
users would rather expect to go to the previous page.
NOTE: For security reasons browsers don't allow setting an invalid URL
or a URL with a different origin (protocol, hostname and port) than the
current URL. Browsers may also enforce a rate limit for changing the
URL. This means that if [url] is not a valid URL of the same origin or
if we try to change the URL too frequently, an exception is thrown.
*)valback:'mnav_key->int->'mt(** [back navigation_key count]
Navigate back [count] entries in the browser history. This requires a
[navigation_key] which is only available in
{{!application}full web applications}. See {!Navigation.key} for
details.
*)valforward:'mnav_key->int->'mt(** [forward navigation_key count]
Navigate forward [count] entries in the browser history. This requires a
[navigation_key] which is only available in
{{!application}full web applications}. See {!Navigation.key} for
details.
*)valload:string->'mt(** [load url]
Load the given [url]. This causes a traditional page load. For
navigating to a different page within a
{{!application}single-page application}, use {!push_url} instead.
*)valreload:unit->'mt(** [reload ()]
Reload the current page.
*)(** {2 Reference Nodes }
More details on reference nodes see {!val:Html.reference}.
*)valset_reference:string->'mhtml->'mt(** [set_reference name vdom]
Display [vdom] in the reference node [name].
If a reference node [name] does not yet exist, then create a reference
node.
*)(** {2 Http Requests} *)valhttp_request:string->string->(string*string)list->http_body->'mhttp_expect->(http_error->'m)->'mt(** [http_request method url headers body expect on_error]
Make an http [method] request to [url] with [headers] and [body].
[expect] specifies the expected response format and can be used to
produce a message. [on_error] transforms a {!Http.error} into a
message.
Examples:
{[
(* Send an empty body and expect a string response *)
let on_success msg = Got_message msg in
let on_error _ = Got_error "Failed to obtain message" in
http_request
"GET"
"/message/123"
[]
Http.Body.empty
(Http.Expect.map on_success Http.Expect.string)
on_error
(* Send file contents as the body and expect a json object with
field "url" *)
let on_success url = Got_file_uploaded url in
let on_error _ = Got_error "file upload failed" in
http_request
"PUT"
"/upload/my_file.txt"
[]
(Http.Body.file file)
(Http.Expect.json Decoder.(map on_success (field "url" string)))
on_error
]}
*)(** {1 Execute Tasks} *)(** If a command wants to execute chains of simple commands before returning
a message to the application, then it is necessary to create a task
which does the more complex operation and perform the task within a
command.
An object of type [('a, 'e) Task.t] is a task which in case of success
returns a value of type ['a] and in case of failure returns a value of
type ['e].
An object of type [('a, Task.empty) Task.t] is a task which cannot fail.
*)valattempt:(('a,'e)result->'m)->('a,'e)task->'mt(** [attempt f task] Attempt the possibly failing [task] and map the result
via the function [f] into a message to send to the application. *)valperform:('m,empty)task->'mt(** [perform task] Perform the non failing [task] and send the message
generated by the task to the application. *)valjust_do:(unit,empty)task->'mt(** [perform task] Perform the non failing [task] and don't send any message
to the application. *)end