Skip to content

Commit 8138e88

Browse files
committed
Add ExecuteScript
1 parent 9600cf4 commit 8138e88

File tree

1 file changed

+78
-16
lines changed

1 file changed

+78
-16
lines changed

sdk/README.md

Lines changed: 78 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -104,15 +104,15 @@ String enum of supported events:
104104

105105
```go
106106
ServerSentEventGenerator.PatchElements(
107-
elements: string,
108-
options?: {
109-
selector?: string,
110-
mode?: ElementPatchMode,
111-
useViewTransition?: boolean,
112-
eventId?: string,
113-
retryDuration?: durationInMilliseconds
114-
}
115-
)
107+
elements: string,
108+
options?: {
109+
selector?: string,
110+
mode?: ElementPatchMode,
111+
useViewTransition?: boolean,
112+
eventId?: string,
113+
retryDuration?: durationInMilliseconds
114+
}
115+
)
116116
```
117117

118118
#### Example Output
@@ -246,13 +246,13 @@ String enum defining how elements are patched into the DOM.
246246

247247
```go
248248
ServerSentEventGenerator.PatchSignals(
249-
signals: string,
250-
options ?: {
251-
onlyIfMissing?: boolean,
252-
eventId?: string,
253-
retryDuration?: durationInMilliseconds
254-
}
255-
)
249+
signals: string,
250+
options ?: {
251+
onlyIfMissing?: boolean,
252+
eventId?: string,
253+
retryDuration?: durationInMilliseconds
254+
}
255+
)
256256
```
257257

258258
#### Example Output
@@ -338,6 +338,68 @@ ServerSentEventGenerator.PatchSignals(
338338

339339
---
340340

341+
### `ServerSentEventGenerator.ExecuteScript`
342+
343+
```go
344+
ServerSentEventGenerator.ExecuteScript(
345+
script: string,
346+
options?: {
347+
autoRemove?: boolean,
348+
attributes?: []string,
349+
eventId?: string,
350+
retryDuration?: durationInMilliseconds
351+
}
352+
)
353+
```
354+
355+
#### Example Output
356+
357+
<details>
358+
<summary>Minimal Example</summary>
359+
360+
```
361+
event: datastar-patch-elements
362+
data: mode append
363+
data: selector body
364+
data: elements <script>console.log('Here')</script>
365+
```
366+
</details>
367+
368+
<details>
369+
<summary>Full Example (all options)</summary>
370+
371+
```
372+
event: datastar-patch-elements
373+
id: 123
374+
retry: 2000
375+
data: mode append
376+
data: selector body
377+
data: elements <script type="application/javascript" data-effect="el.remove()">console.log('Here')</script>
378+
```
379+
</details>
380+
381+
### Parameters
382+
383+
- **script**: One or more lines of JavaScript.
384+
385+
### Options
386+
387+
| Parameter | Type | Default | Description |
388+
|-----------|------|---------|-------------|
389+
| `autoRemove` | boolean | `true` | Removes the script tag after executing |
390+
| `attributes` | []string | - | Attributes to add to the script tag |
391+
392+
### Implementation
393+
394+
***Must*** call `ServerSentEventGenerator.send` with event type `datastar-patch-elements`, sending a `script` tag containing the JavaScript to execute. If `autoRemove` is `true`, `data-on-load="el.remove()"` must be added to the `script` tag. If `attributes` exist, they must be added to the `script` tag.
395+
396+
**Data format** (only include non-defaults):
397+
- `selector body\n`
398+
- `mode append\n`
399+
- `elements SCRIPT_TAG\n`
400+
401+
---
402+
341403
## `ReadSignals`
342404

343405
`ReadSignals` parses incoming signal data from the browser into backend objects.

0 commit comments

Comments
 (0)