Skip to content

Commit

Permalink
New: apply alias for compose event processor
Browse files Browse the repository at this point in the history
  • Loading branch information
raquo committed Nov 24, 2023
1 parent 5af18e3 commit b9b17e5
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/main/scala/com/raquo/laminar/keys/EventProcessor.scala
Original file line number Diff line number Diff line change
Expand Up @@ -221,14 +221,28 @@ class EventProcessor[Ev <: dom.Event, V](
*
* a(onClick.preventDefault.compose(_.delay(100)) --> observer)
*
* Note: can also use with more compact `apply` alias:
*
* div(onScroll(_.throttle(100)) --> observer)
*
* a(onClick.preventDefault(_.delay(100)) --> observer)
*
* Note: This method is not chainable. Put all the operations you need inside the `operator` callback.
*
*/
def compose[Out](
operator: EventStream[V] => Observable[Out]
): LockedEventKey[Ev, V, Out] = {
new LockedEventKey(this, operator)
}

/** Alias for [[compose]] */
@inline def apply[Out](
composer: EventStream[V] => Observable[Out]
): LockedEventKey[Ev, V, Out] = {
compose(composer)
}

/** Similar to the Airstream `flatMap` operator.
*
* Use this when you want to create a new stream or signal on every event, e.g.:
Expand Down
10 changes: 10 additions & 0 deletions src/test/scala/com/raquo/laminar/SyntaxSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -609,4 +609,14 @@ class SyntaxSpec extends UnitSpec {
assert(intVar.now() == 15)
}

it("compose syntax") {
val eventObs = Observer.empty[dom.Event]
div(
onClick.compose(_.delay(100)) --> eventObs,
onClick(_.delay(100)) --> eventObs,
//
onClick.preventDefault.compose(_.delay(100)) --> eventObs,
onClick.preventDefault(_.delay(100)) --> eventObs,
)
}
}

0 comments on commit b9b17e5

Please sign in to comment.