Skip to content

Commit

Permalink
No more Stream usage
Browse files Browse the repository at this point in the history
  • Loading branch information
japgolly committed Apr 26, 2017
1 parent dedd2c2 commit ae84c5a
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 26 deletions.
48 changes: 28 additions & 20 deletions core/shared/src/main/scala/scalacss/internal/Css.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,25 @@ package scalacss.internal

object Css {

def apply(ss: TraversableOnce[StyleA], kfs: TraversableOnce[Keyframes], ff: TraversableOnce[FontFace[String]])(implicit env: Env): Css =
styles(ss) append keyframes(kfs) append fontFaces(ff)
def apply(ss: TraversableOnce[StyleA],
kfs: TraversableOnce[Keyframes],
ff: TraversableOnce[FontFace[String]])
(implicit env: Env): Css = {
val b = Vector.newBuilder[CssEntry]
b ++= styles(ss)
b ++= keyframes(kfs)
b ++= fontFaces(ff)
b.result()
}

def styles(ss: TraversableOnce[StyleA])(implicit env: Env): StyleStream =
ss.toStream flatMap styleA
ss.toIterator.flatMap(styleA).toVector

def keyframes(kfs: TraversableOnce[Keyframes])(implicit env: Env): KeyframeStream =
kfs.toStream map keyframes
kfs.toIterator.map(keyframes).toList

def fontFaces(ff: TraversableOnce[FontFace[String]]): FontFaceStream =
ff.toStream map fontFaces
ff.toIterator.map(fontFaces).toList

def className(cn: ClassName): CssSelector =
"." + cn.value
Expand All @@ -36,21 +44,21 @@ object Css {
style(className(s.className), s.style)

def style(sel: CssSelector, s: StyleS)(implicit env: Env): StyleStream = {
def main: StyleStream =
s.data.toStream.flatMap {
def main: Iterator[CssEntry.Style] =
s.data.iterator.flatMap {
case (cond, avs) =>
val kvs = avs.avIterator.map(_(env)).foldLeft(Vector.empty[CssKV])(_ ++ _)
NonEmptyVector.maybe(kvs, Stream.empty[CssEntry.Style]) {c =>
NonEmptyVector.maybe(kvs, List.empty[CssEntry.Style]) { c =>
val mq = mediaQuery(cond)
val s = selector(sel, cond)
Stream(CssEntry.Style(mq, s, c))
val s = selector(sel, cond)
CssEntry.Style(mq, s, c) :: Nil
}
}

def exts: StyleStream =
s.unsafeExts.toStream.flatMap(unsafeExt(sel, _))
def exts: Iterator[CssEntry.Style] =
s.unsafeExts.toIterator.flatMap(unsafeExt(sel, _))

main append exts
(main ++ exts).toVector
}

def unsafeExt(root: CssSelector, u: Style.UnsafeExt)(implicit env: Env): StyleStream = {
Expand All @@ -62,9 +70,9 @@ object Css {
type ByMediaQuery = Map[CssMediaQueryO, ValuesByMediaQuery]

def separateStylesAndKeyframes(c: Css): (StyleStream, KeyframeStream, FontFaceStream) = {
val styles = Stream.newBuilder[CssEntry.Style]
val animations = Stream.newBuilder[CssEntry.Keyframes]
val fontFaces = Stream.newBuilder[CssEntry.FontFace]
val styles = Vector.newBuilder[CssEntry.Style]
val animations = List.newBuilder[CssEntry.Keyframes]
val fontFaces = List.newBuilder[CssEntry.FontFace]
c.foreach {
case e: CssEntry.Style => styles += e
case e: CssEntry.Keyframes => animations += e
Expand All @@ -82,14 +90,14 @@ object Css {
}
}

def flatten(css: StyleStream): Stream[(CssMediaQueryO, CssSelector, CssKV)] =
def flatten(css: StyleStream): Vector[(CssMediaQueryO, CssSelector, CssKV)] =
css.flatMap { case CssEntry.Style(mq, sel, kvs) =>
kvs.toStream.map(kv => (mq, sel, kv))
kvs.iterator.map(kv => (mq, sel, kv))
}

type Flat4 = (CssMediaQueryO, CssSelector, String, String)
def flatten4(css: StyleStream): Stream[Flat4] =
def flatten4(css: StyleStream): Vector[Flat4] =
css.flatMap { case CssEntry.Style(mq, sel, kvs) =>
kvs.toStream.map(kv => (mq, sel, kv.key, kv.value))
kvs.iterator.map(kv => (mq, sel, kv.key, kv.value))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ final class NonEmptyVector[+A](val head: A, val tail: Vector[A]) {
foldMapLeft1(f)((b, a) => g(b, f(a)))

def toSet[B >: A] = whole.toSet[B]
def toStream = whole.toStream
}

// =====================================================================================================================
Expand Down
10 changes: 5 additions & 5 deletions core/shared/src/main/scala/scalacss/internal/package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,15 @@ package object internal {
type KeyframeAnimationName = ClassName
type KeyframeSelector = Percentage[_]

type StyleStream = Stream[CssEntry.Style]
type KeyframeStream = Stream[CssEntry.Keyframes]
type FontFaceStream = Stream[CssEntry.FontFace]
type StyleStream = Vector[CssEntry.Style]
type KeyframeStream = List[CssEntry.Keyframes]
type FontFaceStream = List[CssEntry.FontFace]

/**
* A stylesheet in its entirety. Normally turned into a `.css` file or a `<style>` tag.
*/
type Css = Stream[CssEntry]
implicit def univEqCss: UnivEq[Css] = UnivEq.univEqStream
type Css = Vector[CssEntry]
implicit def univEqCss: UnivEq[Css] = UnivEq.univEqVector

type WarningMsg = String
final case class Warning(cond: Cond, msg: WarningMsg)
Expand Down
2 changes: 2 additions & 0 deletions doc/history/0.5.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,5 @@
* scalajs-react 1.0.0.

* Compile for Scala 2.12 with `-opt:l:method`

* Replace all instance of `Stream` with `List`, `Vector` or `Iterator`

0 comments on commit ae84c5a

Please sign in to comment.