@@ -4,6 +4,7 @@ import zio._
44
55import zio .http .ServerSentEvent
66import zio .http .datastar .ServerSentEventGenerator .DefaultRetryDelay
7+ import zio .http .template2 .Dom .AttributeValue
78import zio .http .template2 ._
89
910sealed trait DatastarEvent {
@@ -15,13 +16,17 @@ sealed trait DatastarEvent {
1516object DatastarEvent {
1617
1718 final case class PatchElements (
18- elements : Iterable [ Dom ] ,
19+ elements : Dom ,
1920 selector : Option [CssSelector ] = None ,
2021 mode : ElementPatchMode = ElementPatchMode .Outer ,
2122 useViewTransition : Boolean = false ,
2223 eventId : Option [String ] = None ,
2324 retryDuration : Duration = 1000 .millis,
2425 ) extends DatastarEvent {
26+ assert(
27+ mode != ElementPatchMode .Remove || (selector.nonEmpty && elements.isEmpty),
28+ " When using mode 'remove', 'selector' must be defined and 'elements' must be empty" ,
29+ )
2530 override val eventType : EventType = EventType .PatchElements
2631 override def toServerSentEvent : ServerSentEvent [String ] = {
2732 val sb = new StringBuilder ()
@@ -36,13 +41,7 @@ object DatastarEvent {
3641 sb.append(" useViewTransition true\n " )
3742 }
3843
39- elements.foreach { d =>
40- val rendered = d.render
41- if (rendered.contains('\n ' ))
42- rendered.split('\n ' ).foreach(line => sb.append(" elements " ).append(line).append('\n ' ))
43- else
44- sb.append(" elements " ).append(rendered).append('\n ' )
45- }
44+ sb.append(" elements " ).append(elements.renderMinified).append('\n ' )
4645
4746 val retry = if (retryDuration != DefaultRetryDelay ) Some (retryDuration) else None
4847 ServerSentEvent (sb.toString(), Some (eventType.render), eventId, retry)
@@ -159,8 +158,10 @@ object DatastarEvent {
159158 script0 : Dom .Element .Script ,
160159 options : ExecuteScriptOptions ,
161160 ): ExecuteScript = {
162- val removeAttr = if (options.autoRemove) Dom .attr(" data-effect" , " el.remove" ) else Dom .empty
163- val scriptWithAttrs = script0(removeAttr)(options.attributes.map(a => Dom .attr(a._1, a._2)))
161+ val removeAttr =
162+ if (options.autoRemove) Dom .attr(" data-effect" , AttributeValue .StringValue (" el.remove" )) else Dom .empty
163+ val scriptWithAttrs =
164+ script0(removeAttr)(options.attributes.map(a => Dom .attr(a._1, AttributeValue .StringValue (a._2))))
164165
165166 ExecuteScript (
166167 script = scriptWithAttrs,
@@ -200,7 +201,7 @@ object DatastarEvent {
200201 patchElements(elements, PatchElementOptions .default)
201202
202203 def patchElements (elements : String , options : PatchElementOptions ): PatchElements =
203- patchElements(elements.split( ' \n ' ).map( Dom .raw).toList , options)
204+ patchElements(Dom .raw(elements) , options)
204205
205206 def patchElements (elements : String , selector : Option [CssSelector ]): PatchElements =
206207 patchElements(elements, PatchElementOptions (selector = selector))
@@ -239,10 +240,17 @@ object DatastarEvent {
239240 patchElements(elements, PatchElementOptions (selector, mode, useViewTransition, eventId, retryDuration))
240241
241242 def patchElements (element : Dom ): PatchElements =
242- patchElements(List ( element) , PatchElementOptions .default)
243+ patchElements(element, PatchElementOptions .default)
243244
244245 def patchElements (element : Dom , options : PatchElementOptions ): PatchElements =
245- patchElements(List (element), options)
246+ patchElements(
247+ element,
248+ options.selector,
249+ options.mode,
250+ options.useViewTransition,
251+ options.eventId,
252+ options.retryDuration,
253+ )
246254
247255 def patchElements (element : Dom , selector : Option [CssSelector ]): PatchElements =
248256 patchElements(element, PatchElementOptions (selector = selector))
@@ -275,63 +283,13 @@ object DatastarEvent {
275283 eventId : Option [String ],
276284 retryDuration : Duration ,
277285 ): PatchElements =
278- patchElements(element, PatchElementOptions (selector, mode, useViewTransition, eventId, retryDuration))
279-
280- def patchElements (elements : Iterable [Dom ]): PatchElements =
281- patchElements(elements, PatchElementOptions .default)
282-
283- def patchElements (elements : Iterable [Dom ], options : PatchElementOptions ): PatchElements = {
284- PatchElements (
285- elements = elements,
286- selector = options.selector,
287- mode = options.mode,
288- useViewTransition = options.useViewTransition,
289- eventId = options.eventId,
290- retryDuration = options.retryDuration,
291- )
292- }
293-
294- def patchElements (elements : Iterable [Dom ], selector : Option [CssSelector ]): PatchElements =
295- patchElements(elements, PatchElementOptions (selector = selector))
296-
297- def patchElements (elements : Iterable [Dom ], selector : Option [CssSelector ], mode : ElementPatchMode ): PatchElements =
298- patchElements(elements, PatchElementOptions (selector = selector, mode = mode))
299-
300- def patchElements (
301- elements : Iterable [Dom ],
302- selector : Option [CssSelector ],
303- mode : ElementPatchMode ,
304- useViewTransition : Boolean ,
305- ): PatchElements =
306- patchElements(
307- elements,
308- PatchElementOptions (selector = selector, mode = mode, useViewTransition = useViewTransition),
309- )
310-
311- def patchElements (
312- elements : Iterable [Dom ],
313- selector : Option [CssSelector ],
314- mode : ElementPatchMode ,
315- useViewTransition : Boolean ,
316- eventId : Option [String ],
317- ): PatchElements =
318- patchElements(elements, PatchElementOptions (selector, mode, useViewTransition, eventId))
319-
320- def patchElements (
321- elements : Iterable [Dom ],
322- selector : Option [CssSelector ],
323- mode : ElementPatchMode ,
324- useViewTransition : Boolean ,
325- eventId : Option [String ],
326- retryDuration : Duration ,
327- ): PatchElements =
328- patchElements(elements, PatchElementOptions (selector, mode, useViewTransition, eventId, retryDuration))
286+ PatchElements (element, selector, mode, useViewTransition, eventId, retryDuration)
329287
330288 def patchSignals (signal : String ): PatchSignals =
331- patchSignals(Iterable (signal), PatchSignalOptions .default)
289+ patchSignals(List (signal), PatchSignalOptions .default)
332290
333291 def patchSignals (signal : String , options : PatchSignalOptions ): PatchSignals =
334- patchSignals(Iterable (signal), options)
292+ patchSignals(List (signal), options)
335293
336294 def patchSignals (signal : String , onlyIfMissing : Boolean ): PatchSignals =
337295 patchSignals(signal, PatchSignalOptions (onlyIfMissing = onlyIfMissing))
0 commit comments