Browse Source

Upgrade outwatch dependency and add history api link (#1)

master
Zak Patterson 5 years ago
committed by GitHub
parent
commit
4dc36533cb
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 2
      .gitignore
  2. 2
      README.md
  3. 2
      build.sbt
  4. 22
      outwatch-router/src/main/scala/outwatch/router/Router.scala
  5. 13
      outwatch-router/src/main/scala/outwatch/router/dsl/C.scala
  6. 2
      outwatch-router/src/main/scala/outwatch/router/package.scala
  7. 2
      project/Version.scala

2
.gitignore

@ -140,3 +140,5 @@ GitHub.sublime-settings
# End of https://www.gitignore.io/api/sbt,scala,bloop,metals,intellij,sublimetext
/.idea/
/outwatch-router/yarn.lock

2
README.md

@ -8,7 +8,7 @@ Most of this code is adapted from [http4s](http4s.org)'s route parsing and path
Add to library dependencies:
```
"com.clovellytech" %%% "outwatch-router" % "0.0.4"
"com.clovellytech" %%% "outwatch-router" % "0.0.5"
```
See [documentation][doc-root]

2
build.sbt

@ -81,7 +81,7 @@ lazy val router = project
webpackBundlingMode in fastOptJS := BundlingMode.LibraryOnly(),
resolvers += "jitpack" at "https://jitpack.io",
libraryDependencies ++= Seq(
"io.github.outwatch" % "outwatch" % "ea240c6d04",
"io.github.outwatch" % "outwatch" % "e0f28a8fbb",
"org.scalatest" %%% "scalatest" % "3.0.5" % Test
),
copyFastOptJS := {

22
outwatch-router/src/main/scala/outwatch/router/Router.scala

@ -1,6 +1,6 @@
package outwatch.router
import cats.effect.LiftIO
import cats.effect.IO
import monix.execution.Scheduler
import org.scalajs.dom.window
import outwatch.dom._
@ -12,7 +12,7 @@ final case class Replace(path: Path) extends Action
final case class RouterState[P](page: P)
class AppRouter[F[_]: LiftIO, P](root: Path, f: Path => P) {
class AppRouter[P](root: Path, f: Path => P) {
def routerReducer(state: RouterState[P], action: Action): RouterState[P] = action match {
case Replace(path) =>
window.history.replaceState("", "", Path(root, path).toString)
@ -20,24 +20,24 @@ class AppRouter[F[_]: LiftIO, P](root: Path, f: Path => P) {
case _ => state
}
def store(implicit S : Scheduler): F[RouterStore[P]] = {
def store(implicit S : Scheduler): IO[RouterStore[P]] = {
val startingPath = Path(window.location.pathname)
Store.create[RouterState[P], Action](
Store.create[Action, RouterState[P]](
Replace(startingPath),
RouterState(f(startingPath)),
Store.Reducer.justState(routerReducer _)
).to[F]
)
}
}
object AppRouter{
def render[P](resolver: RouterResolve[P])(implicit store: RouterStore[P]): Observable[VDomModifier] =
store.map(state => resolver(state.page))
store.map{ case (_, RouterState(p)) => resolver(p) }
def create[F[_]: LiftIO, P](notFound: P)(f: PartialFunction[Path, P]): AppRouter[F, P] =
create[F, P](Root, notFound)(f)
def create[F[_]: LiftIO, P](parent: Path, notFound: P)(f: PartialFunction[Path, P]): AppRouter[F, P] =
new AppRouter[F, P](parent, f.lift.andThen(_.getOrElse(notFound)))
def create[P](notFound: P)(f: PartialFunction[Path, P]): AppRouter[P] =
create[P](Root, notFound)(f)
def create[P](parent: Path, notFound: P)(f: PartialFunction[Path, P]): AppRouter[P] =
new AppRouter[P](parent, f.lift.andThen(_.getOrElse(notFound)))
}

13
outwatch-router/src/main/scala/outwatch/router/dsl/C.scala

@ -0,0 +1,13 @@
package outwatch.router
package dsl
import outwatch.dom.VDomModifier
import outwatch.dom.{dsl => O, _}
object C {
def a[P](linkHref: String)(attrs: VDomModifier*)(implicit store: RouterStore[P]): BasicVNode =
O.a(
O.href := linkHref,
O.onClick.preventDefault.mapTo(Replace(Path(linkHref))) --> store
)(attrs)
}

2
outwatch-router/src/main/scala/outwatch/router/package.scala

@ -3,7 +3,7 @@ package outwatch
import outwatch.dom.VDomModifier
package object router {
type RouterStore[P] = ProHandler[Action, RouterState[P]]
type RouterStore[P] = ProHandler[Action, (Action, RouterState[P])]
type RouterResolve[P] = PartialFunction[P, VDomModifier]
}

2
project/Version.scala

@ -1,4 +1,4 @@
object Version{
val version = "0.0.4"
val version = "0.0.5"
val scalaVersion = "2.12.8"
}
Loading…
Cancel
Save