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