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

This commit is contained in:
Zak Patterson 2019-02-17 14:19:05 -05:00 committed by GitHub
parent 5c5dcdf1a5
commit 4dc36533cb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 30 additions and 15 deletions

2
.gitignore vendored
View File

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

View File

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

View File

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

View File

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

View File

@ -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)
}

View File

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

View File

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