- See also
library(dom)
implements a Tau-Prolog compatible Prolog
interface to the browser's DOM.
This library is only available in the WASM version. It provides
helper predicates for the JavaScript part as well as Prolog utility
predicates that help in communicating with JavaScript.
- [det]await(+Request,
=Result)
- Call asynchronous behavior. Request is normally a JavaScript
Promise instance. If we want Prolog to wait for some task to complete,
we first write a JavaScript function that returns a Promise
that resolves when the task is complete. Next, we use :=/2
to get the
Promise and finally we use await/2
to wait for the Promise. On success, Result is
unified to the value with which the Promise was resolved. If
the Promise is rejected, this predicate raises an exception
using the value passed to
reject()
.
- Errors
permission_error(run, goal, Goal)
if the current query is
not aynchronous.
- See also
- sleep/1, fetch/3
and wait/3 in this library
use await/2.
- [semidet]is_async
- True when we can call await/2.
We can not yield when we are in a callback from C (WASM)
to Prolog.
- [det]sleep(+Seconds)
- Sleep by yielding when possible. Note that this defines sleep/1
in
user
, overruling system:sleep/1.
- [det]bind(+Elem,
+EventType, -Event, :Goal)
- [det]bind_async(+Elem,
+EventType, -Event, :Goal)
- Bind EventType on Elem to call Goal. If Event
appears in Goal is is bound to the current event.
The bind_async/4
variation runs the event handler on a new Prolog
engine using Prolog.forEach()
. This implies that the
handler runs asynchronously and all its solutions are enumerated.
- Compatibility
- bind_async/5 is a SWI-Prolog extension to
the Tau library
- [det]unbind(+Elem,
+EventType)
- Remove the event listener for EventType.
- [det]wait(+Elem,
+EventType, =Event)
- Make the calling task wait for EventType on Elem.
If the event is triggered, Event is unified with the event
object.
- [semidet]is_object(@Term)
- [semidet]is_object(@Term,
?Class)
- Test whether a Prolog term is a JavaScript object.
- -Result := +Call
- +Target := +Value
- Call a JavaScript function expressed by Call. Call
is a compound. The functor name denotes the function to be called and
the arguments are converted using
Prolog.toJSON
. The
function return value can be accessed using js_call(Return = Call)
.
In this case Return is the return value of the function
converted by Prolog.toProlog()
. Examples:
?- Res := myfunc([1,2,3]).
?- Max := 'Math'.max(10, 20).
?- Out := document.getElementById('output').
?- Par := document.createElement(p),
Par.textContent := #Text.
?- Par.textContent := "aap" + " " + "noot".
- [det]js_script(+String,
+Options)
- Evaluate String as JavaScript, for example for defining a
function. This may be used together with the strings quasi quotation
facility to easily support long strings that may also use double quotes.
:- use_module(library(strings)).
:- js_script({|string||
function myfunc(a)
...
|}).
Options is currently ignored. While this used to add a
<script>
node to the document it now uses (=:)/2 to
evaluate the script. I.e. js_script is the same as:
?- _ := eval(String).
- [det]fetch(+URL,
+Type, -Data)
- Fetch the content from URL asynchronously. Type is
a method name on the Response object returned by
fetch()
,
e.g., text
, json
,
html
, blob
.