Skip to content

Commit

Permalink
New: flatMapWithStatus event processors
Browse files Browse the repository at this point in the history
  • Loading branch information
raquo committed Nov 30, 2023
1 parent 2ec0443 commit b5cc119
Showing 1 changed file with 44 additions and 1 deletion.
45 changes: 44 additions & 1 deletion src/main/scala/com/raquo/laminar/keys/EventProcessor.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.raquo.laminar.keys

import com.raquo.airstream.core.{EventStream, Observable, Signal, Sink}
import com.raquo.airstream.flatten.SwitchingStrategy
import com.raquo.airstream.status.Status
import com.raquo.laminar.DomApi
import com.raquo.laminar.api.UnitArrowsFeature
import com.raquo.laminar.modifiers.EventListener
Expand Down Expand Up @@ -247,10 +248,13 @@ class EventProcessor[Ev <: dom.Event, V](
*
* Use this when you want to create a new stream or signal on every event, e.g.:
*
* {{{
* button(onClick.preventDefault.flatMap(_ => makeAjaxRequest()) --> observer)
* }}}
*
* #TODO[IDE] IntelliJ (2022.3.2) shows false errors when using this flatMap implementation,
* at least with Scala 2, making it annoying. Use flatMapStream or flatMapSignal to get around that.
* https://youtrack.jetbrains.com/issue/SCL-21836/Kind-context-bound-callback-argument-causes-false-positive-type-mismatch-error
*
* Note: This method is not chainable. Put all the operations you need inside the `operator` callback,
* or use the `compose` method instead for more flexibility
Expand All @@ -260,7 +264,10 @@ class EventProcessor[Ev <: dom.Event, V](
)(
implicit strategy: SwitchingStrategy[EventStream, Obs, Observable]
): LockedEventKey[Ev, V, Out] = {
new LockedEventKey[Ev, V, Out](this, eventStream => eventStream.flatMapSwitch(operator)(strategy))
new LockedEventKey[Ev, V, Out](
this,
eventStream => eventStream.flatMapSwitch(operator)(strategy)
)
}

/** Equivalent to `flatMap(_ => observable)`
Expand Down Expand Up @@ -293,6 +300,42 @@ class EventProcessor[Ev <: dom.Event, V](
flatMap(operator)(strategy)
}

/** Similar to Airstream `flatMapWithStatus` operator.
*
* Use this when you want to flatMapSwitch and get a status indicating
* whether the input has been processed by the inner stream, e.g.:
*
* {{{
* button(onClick.flatMapWithStatus(ev => AjaxStream.get(ev, ...)) --> observer
* }}}
*/
def flatMapWithStatus[Out](
operator: V => EventStream[Out]
): LockedEventKey[Ev, V, Status[V, Out]] = {
new LockedEventKey[Ev, V, Status[V, Out]](
this,
eventStream => eventStream.flatMapWithStatus(operator)
)
}

/** Similar to Airstream `flatMapWithStatus` operator.
*
* Use this when you want to flatMapSwitch and get a status indicating
* whether the input has been processed by the inner stream, e.g.:
*
* {{{
* button(onClick.flatMapWithStatus(AjaxStream.get(...)) --> observer
* }}}
*/
def flatMapWithStatus[Out](
innerStream: => EventStream[Out]
): LockedEventKey[Ev, V, Status[V, Out]] = {
new LockedEventKey[Ev, V, Status[V, Out]](
this,
eventStream => eventStream.flatMapWithStatus(innerStream)
)
}

/** Evaluate `f` if the value was filtered out up the chain. For example:
*
* onClick.filter(isRightClick).orElseEval(_.preventDefault()) --> observer
Expand Down

0 comments on commit b5cc119

Please sign in to comment.