Skip to content

Commit

Permalink
some small update
Browse files Browse the repository at this point in the history
  • Loading branch information
HollandDM committed May 30, 2024
1 parent 837789d commit bfea9de
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.raquo.airstream.split

import com.raquo.airstream.core.{EventStream, Signal, Observable, BaseObservable}
import scala.quoted.{Expr, Quotes, Type}
import scala.annotation.compileTimeOnly

/**
* `SplittableTypeMacros` turns this code
Expand Down Expand Up @@ -63,15 +64,17 @@ object SplittableTypeMacros {
*
* This is important, because `({ case baz: Baz => baz }) :: ({ case Bar(Some(str)) => str }) :: Nil` saves all the infomation needed for the macro to expands into match case definition
*/
final class MatchSplitObservable[Self[+_] <: Observable[_] , I, O] private[SplittableTypeMacros] (
private[SplittableTypeMacros] val observable: BaseObservable[Self, I],
private[SplittableTypeMacros] val caseList: List[PartialFunction[Any, Any]],
private[SplittableTypeMacros] val handlerMap: Map[Int, Function[Any, O]]
) {

// MatchSplitObservable should be erased after macro expansion, any instance in runtime is illegal
final class MatchSplitObservable[Self[+_] <: Observable[_] , I, O] {
throw new UnsupportedOperationException("splitMatch without toSignal/toStream is illegal")
}

object MatchSplitObservable {
@compileTimeOnly("splitMatch without toSignal/toStream is illegal")
def build[Self[+_] <: Observable[_] , I, O](
observable: BaseObservable[Self, I],
caseList: List[PartialFunction[Any, Any]],
handlerMap: Map[Int, Function[Any, O]]
): MatchSplitObservable[Self, I, O] = throw new UnsupportedOperationException("splitMatch without toSignal/toStream is illegal")
}

extension [Self[+_] <: Observable[_], I](inline observable: BaseObservable[Self, I]) {
Expand All @@ -90,16 +93,6 @@ object SplittableTypeMacros {
inline def toStream: EventStream[O] = ${ observableImpl('{ matchSplitObservable })}
}

object MatchSplitObservable {
def build[Self[+_] <: Observable[_] , I, O](
observable: BaseObservable[Self, I],
caseList: List[PartialFunction[Any, Any]],
handlerMap: Map[Int, Function[Any, O]]
): MatchSplitObservable[Self, I, O] = {
MatchSplitObservable(observable, caseList, handlerMap)
}
}

private def handleCaseImpl[Self[+_] <: Observable[_] : Type, I: Type, O: Type, O1 >: O : Type, A: Type, B: Type](
matchSplitObservableExpr: Expr[MatchSplitObservable[Self, I, O]],
casePfExpr: Expr[PartialFunction[A, B]],
Expand Down
23 changes: 0 additions & 23 deletions src/test/scala/com/raquo/airstream/split/SplittableTypeSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -471,27 +471,4 @@ class SplittableTypeSpec extends UnitSpec {

}

it("illegal usage should throw") {
val owner = new TestableOwner
val fooVar = Var[Foo](Tar)
var isThrown = false

try {
fooVar.signal
.splitMatch
.handleCase {
case Bar(None) => "null"
} { case (str, strSignal) =>
()
}
.handleCase { case baz: Baz => baz } { case (baz, bazSignal) =>
()
}
} catch {
case _: UnsupportedOperationException => isThrown = true
} finally {
isThrown shouldBe true
}
}

}

0 comments on commit bfea9de

Please sign in to comment.