Liking cljdoc? Tell your friends :D

starfederation.datastar.clojure.api

Public api for the Datastar SDK.

The main api consists several functions that operate on SSE generators, see:

These function take options map whose keys are:

To help manage SSE generators's underlying connection there is:

Helper to extract datastar specific data from ring requests:

Some common utilities for HTTP are also provided:

Some scripts are provided:

Public api for the Datastar SDK.

The main api consists several functions that operate on SSE generators, see:
- [[patch-elements!]]
- [[patch-elements-seq!]]
- [[remove-element!]]
- [[patch-signals!]]
- [[execute-script!]]


These function take options map whose keys are:
- [[id]]
- [[retry-duration]]
- [[selector]]
- [[patch-mode]]
- [[use-view-transition]]
- [[only-if-missing]]
- [[auto-remove]]
- [[attributes]]

To help manage SSE generators's underlying connection there is:
- [[close-sse!]]
- [[lock-sse!]]
- [[with-open-sse]]

Helper to extract datastar specific data from ring requests:
- [[get-signals]]
- [[datastar-request?]]

Some common utilities for HTTP are also provided:
- [[sse-get]]
- [[sse-post]]
- [[sse-put]]
- [[sse-patch]]
- [[sse-delete]]

Some scripts are provided:
- [[console-log!]]
- [[console-error!]]
- [[redirect!]]
raw docstring

attributesclj

execute-script! option, map:

A map of attributes to add to the script element.

[[execute-script!]] option, map:

A map of attributes to add to the script element.
sourceraw docstring

auto-removeclj

execute-script! option, boolean:

Whether to remove the script after execution, if not provided the Datastar client side will default to true.

[[execute-script!]] option, boolean:

Whether to remove the script after execution, if not
provided the Datastar client side will default to true.
sourceraw docstring

CDN-map-urlclj

URL for the Datastar js source map

URL for the Datastar js source map
sourceraw docstring

CDN-urlclj

URL for the Datastar js bundle

URL for the Datastar js bundle
sourceraw docstring

close-sse!clj

(close-sse! sse-gen)

Close the connection of a sse generator.

Return value:

  • true if sse-gen closed
  • false if it was already closed
Close the connection of a sse generator.

Return value:
- true if `sse-gen` closed
- false if it was already closed
sourceraw docstring

console-error!clj

(console-error! sse-gen msg)
(console-error! sse-gen msg opts)

Log error msg in the browser console.

Same behavior as execute-script!.

Log error msg in the browser console.

Same behavior as [[execute-script!]].
sourceraw docstring

console-log!clj

(console-log! sse-gen msg)
(console-log! sse-gen msg opts)

Log msg in the browser console.

Same behavior as execute-script!.

Log msg in the browser console.

Same behavior as [[execute-script!]].
sourceraw docstring

datastar-request?clj

(datastar-request? request)

Test for the presence of the datastar header in a ring request. The presence of the header means the request is issued from a datastar action.

Test for the presence of the datastar header in a ring request. The presence
of the header means the request is issued from a datastar action.
sourceraw docstring

execute-script!clj

(execute-script! sse-gen script-text)
(execute-script! sse-gen script-text opts)

Construct a HTML script tag using script-text as its content. Then sends it to the brower using patch-elements! with patch-mode set to pm-append and selector set to "body".

The default behavior is to auto remove the script after it has run.

Args:

  • sse-gen: the sse generator to send from
  • script-text: string that represents the JavaScript to be executed by the browser.
  • opts: An options map

Options keys:

Return value:

  • false if the connection is closed
  • true otherwise
Construct a HTML script tag using `script-text` as its content. Then sends it
to the brower using [[patch-elements!]] with [[patch-mode]] set to
[[pm-append]] and [[selector]] set to `"body"`.

The default behavior is to auto remove the script after it has run.

 Args:
- `sse-gen`: the sse generator to send from
- `script-text`: string that represents the JavaScript to be executed
  by the browser.
- `opts`: An options map

Options keys:
- [[id]]
- [[retry-duration]]
- [[auto-remove]] defaults to true
- [[attributes]]

Return value:
- `false` if the connection is closed
- `true` otherwise
sourceraw docstring

get-signalsclj

(get-signals ring-request)

Extract datastar signals from a ring request map.

This function returns either a string or an InputStream depending on the HTTP method of the request.

  • In the case of a GET request a string is returned (the signals are found in the :query-params map of the request)
  • For all other HTTP methods an InputStream is returned (the signals are the :body of the request)

We do not impose any json parsing library. This means that you need to bring your own to parse the returned value into Clojure data.

Extract datastar signals from a ring request map.

This function returns either a string or an InputStream depending on the
HTTP method of the request.

- In the case of a GET request a string is returned (the signals are found in
  the `:query-params` map of the request)
- For all other HTTP methods an `InputStream` is returned (the signals are the
  `:body` of the request)

We do not impose any json parsing library. This means that you need to bring
your own to parse the returned value into Clojure data.
sourceraw docstring

idclj

SSE option use in all event functions, string:

Each event may include an eventId. This can be used by the backend to replay events. This is part of the SSE spec and is used to tell the browser how to handle the event. For more details see https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events#id

SSE option use in all event functions, string:

Each event may include an eventId. This can be used by
the backend to replay events. This is part of the SSE spec and is used to
tell the browser how to handle the event. For more details see
https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events#id
sourceraw docstring

lock-sse!cljmacro

(lock-sse! sse-gen & body)

Hold onto the lock of a sse-gen while executing body. This allows for preventing concurent sending of sse events. Sse generators use [[java.util.concurrent.locks.ReentrantLock]] under the hood.

Ex:

(lock-sse! my-sse-gen
           (patch-elements! sse frags)
           (patch-signals!  sse signals))
Hold onto the lock of a `sse-gen` while executing `body`. This allows for
preventing concurent sending of sse events. Sse generators use
[[java.util.concurrent.locks.ReentrantLock]] under the hood.

Ex:
```clojure
(lock-sse! my-sse-gen
           (patch-elements! sse frags)
           (patch-signals!  sse signals))
```
sourceraw docstring

only-if-missingclj

patch-signals! option, boolean:

Whether to patch the signal only if it does not already exist. If not provided, the Datastar client side will default to false, which will cause the data to be patchd into the signals.

[[patch-signals!]] option, boolean:

Whether to patch the signal only if it does not already
exist. If not provided, the Datastar client side will default to false, which
will cause the data to be patchd into the signals.
sourceraw docstring

patch-elements!clj

(patch-elements! sse-gen elements)
(patch-elements! sse-gen elements opts)

Send HTML elements to the browser to be patchd into the DOM.

Args:

  • sse-gen: the sse generator to send from
  • elements: A string of HTML elements.
  • opts: An options map

Options keys:

Return value:

  • false if the connection is closed
  • true otherwise
Send HTML elements to the browser to be patchd into the DOM.

Args:
- `sse-gen`: the sse generator to send from
- `elements`: A string of HTML elements.
- `opts`: An options map

Options keys:
- [[id]]
- [[retry-duration]]
- [[selector]]
- [[patch-mode]]
- [[use-view-transition]]

Return value:
- `false` if the connection is closed
- `true` otherwise
sourceraw docstring

patch-elements-seq!clj

(patch-elements-seq! sse-gen elements)
(patch-elements-seq! sse-gen elements opts)

Same as patch-elements! except that it takes a seq of elements.

Same as [[patch-elements!]] except that it takes a seq of elements.
sourceraw docstring

patch-modeclj

patch-elements! & patch-elements-seq! option, string:

The mode to use when merging elements into the DOM. If not provided the Datastar client side will default to morph.

The set of valid values is:

[[patch-elements!]] & [[patch-elements-seq!]] option, string:

The mode to use when merging elements into the DOM.
If not provided the Datastar client side will default to morph.

The set of valid values is:
- [[pm-outer]] default
- [[pm-inner]]
- [[pm-remove]]
- [[pm-prepend]]
- [[pm-append]]
- [[pm-before]]
- [[pm-after]]
- [[pm-replace]]
sourceraw docstring

patch-signals!clj

(patch-signals! sse-gen signals-content)
(patch-signals! sse-gen signals-content opts)

Send signals to the browser using RFC 7386 JSON Merge Patch semantics.

Args:

  • sse-gen: the sse generator to send from
  • signals-content: a JavaScript object or JSON string that will be sent to the browser to update signals. The data must evaluate to a valid JavaScript Object. null values for keys in this JSON object mean that the signal at these keys are to be removed.
  • opts: An options map

Options keys:

Return value:

  • false if the connection is closed
  • true otherwise
Send signals to the browser using
[RFC 7386 JSON Merge Patch](https://datatracker.ietf.org/doc/html/rfc7386)
semantics.

 Args:
 - `sse-gen`: the sse generator to send from
 - `signals-content`: a JavaScript object or JSON string that will be sent to
    the browser to update signals. The data must evaluate to a
    valid JavaScript Object. `null` values for keys in this JSON object mean
    that the signal at these keys are to be removed.
 - `opts`: An options map

Options keys:
- [[id]]
- [[retry-duration]]
- [[only-if-missing]]

Return value:
- `false` if the connection is closed
- `true` otherwise
sourceraw docstring

pm-afterclj

patch mode: inserts the element after the existing element.

patch mode: inserts the element after the existing element.
sourceraw docstring

pm-appendclj

patch mode: appends the element to the existing element.

patch mode: appends the element to the existing element.
sourceraw docstring

pm-beforeclj

patch mode: inserts the element before the existing element.

patch mode: inserts the element before the existing element.
sourceraw docstring

pm-innerclj

patch mode: replaces the inner HTML of the existing element.

patch mode: replaces the inner HTML of the existing element.
sourceraw docstring

pm-outerclj

patch mode: replaces the outer HTML of the existing element.

patch mode: replaces the outer HTML of the existing element.
sourceraw docstring

pm-prependclj

patch mode: prepends the element to the existing element.

patch mode: prepends the element to the existing element.
sourceraw docstring

pm-removeclj

patch mode: remove the existing element from the dom.

patch mode: remove the existing element from the dom.
sourceraw docstring

pm-replaceclj

patch mode: Do not morph, simply replace the whole element and reset any related state.

patch mode: Do not morph, simply replace the whole element and reset any
related state.
sourceraw docstring

redirect!clj

(redirect! sse-gen url)
(redirect! sse-gen url opts)

Redirect a page using a script.

Same behavior as execute-script!.

Redirect a page using a script.

Same behavior as [[execute-script!]].
sourceraw docstring

remove-element!clj

(remove-element! sse-gen selector)
(remove-element! sse-gen selector opts)

Remove element(s) from the dom. It is a convenience function using patch-elements! with the patch-mode options set to pm-remove and a selector set to selector.

Args:

  • sse-gen: the sse generator to send from
  • selector: string, CSS selector that represents the elements to be removed from the DOM.
  • opts: options map

Options keys:

Return value:

  • false if the connection is closed
  • true otherwise
Remove element(s) from the dom. It is a convenience function using
[[patch-elements!]] with the [[patch-mode]] options set to [[pm-remove]]
and a [[selector]] set to `selector`.

Args:
- `sse-gen`: the sse generator to send from
- `selector`: string, CSS selector that represents the elements to be
  removed from the DOM.
- `opts`: options map

Options keys:
- [[id]]
- [[retry-duration]]
- [[use-view-transition]]

Return value:
- `false` if the connection is closed
- `true` otherwise
sourceraw docstring

retry-durationclj

SSE option used in all event functions, number:

Each event may include a retryDuration value. If one is not provided the SDK must default to 1000 milliseconds. This is part of the SSE spec and is used to tell the browser how long to wait before reconnecting if the connection is lost. For more details see https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events#retry

SSE option used in all event functions, number:

Each event may include a retryDuration value. If one is
not provided the SDK must default to 1000 milliseconds. This is part of the
SSE spec and is used to tell the browser how long to wait before reconnecting
if the connection is lost. For more details see
https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events#retry
sourceraw docstring

selectorclj

patch-elements! & patch-elements-seq! option, string:

The CSS selector to use to insert the elements. If not provided or empty, Datastar will default to using the id attribute of the element.

[[patch-elements!]] & [[patch-elements-seq!]] option, string:

The CSS selector to use to insert the elements. If not
provided or empty, Datastar will default to using the id attribute of the
element.
sourceraw docstring

sse-deleteclj

(sse-delete url)
(sse-delete url opts-string)

Helper making a @delete(...) action.

Helper making a @delete(...) action.
sourceraw docstring

sse-getclj

(sse-get url)
(sse-get url opts-string)

Helper making a @get(...) action.

Helper making a @get(...) action.
sourceraw docstring

sse-patchclj

(sse-patch url)
(sse-patch url opts-string)

Helper making a @patch(...) action.

Helper making a @patch(...) action.
sourceraw docstring

sse-postclj

(sse-post url)
(sse-post url opts-string)

Helper making a @post(...) action.

Helper making a @post(...) action.
sourceraw docstring

sse-putclj

(sse-put url)
(sse-put url opts-string)

Helper making a @put(...) action.

Helper making a @put(...) action.
sourceraw docstring

use-view-transitionclj

patch-elements! / [[remove-element!] option, boolean:

Whether to use view transitions, if not provided the Datastar client side will default to false.

[[patch-elements!]] / [[remove-element!]  option, boolean:

Whether to use view transitions, if not provided the
Datastar client side will default to false.
sourceraw docstring

with-open-ssecljmacro

(with-open-sse sse-gen & body)

Macro functioning similarly to [[clojure.core/with-open]]. It evalutes the body inside a try expression and closes the sse-gen at the end using [[close-see!]] in a finally clause.

Ex:

(with-open-sse sse-gen
  (d*/patch-elements! sse-gen frag1)
  (d*/patch-signals!  sse-gen signals))
Macro functioning similarly to [[clojure.core/with-open]]. It evalutes the
`body` inside a try expression and closes the `sse-gen` at the end using
[[close-see!]] in a finally clause.

Ex:
```
(with-open-sse sse-gen
  (d*/patch-elements! sse-gen frag1)
  (d*/patch-signals!  sse-gen signals))
```
sourceraw docstring

cljdoc builds & hosts documentation for Clojure/Script libraries

Keyboard shortcuts
Ctrl+kJump to recent docs
Move to previous article
Move to next article
Ctrl+/Jump to the search field
× close