Refactor and cleanup

This commit is contained in:
Rohan Sircar 2020-12-30 13:49:59 +05:30
parent cc54220dc9
commit 61793d5702
13 changed files with 81 additions and 62 deletions

View File

@ -1,7 +1,13 @@
package outwatchapp package outwatchapp
import scala.concurrent.duration._
import colibri.ext.monix._
import com.softwaremill.macwire._ import com.softwaremill.macwire._
import monix.bio.Task import monix.bio.Task
import monix.eval.Coeval
import monix.reactive.Observable
import outwatchapp.ui.components.todo.TodoListStore
import org.scalajs.dom.raw.Element import org.scalajs.dom.raw.Element
import outwatch._ import outwatch._
import outwatch.dsl._ import outwatch.dsl._
@ -10,7 +16,9 @@ import outwatchapp.components.CounterDemo
import outwatchapp.components.RequestDemo import outwatchapp.components.RequestDemo
import outwatchapp.components.todo.ChartjsDemo import outwatchapp.components.todo.ChartjsDemo
import outwatchapp.pages.HomePage import outwatchapp.pages.HomePage
import nova.monadic_sfx.ui.components.todo.TodoListStore import outwatchapp.util.reactive.WorkerData
import outwatchapp.util.reactive.WebWorker
import outwatchapp.components.todo.FusejsDemo
class MainApp(el: Element)(implicit class MainApp(el: Element)(implicit
backend: AppTypes.Backend, backend: AppTypes.Backend,
@ -28,23 +36,36 @@ class MainApp(el: Element)(implicit
chartDemo <- ChartjsDemo() chartDemo <- ChartjsDemo()
todoStore <- TodoListStore() todoStore <- TodoListStore()
requestDemo <- RequestDemo(todoStore) requestDemo <- RequestDemo(todoStore)
resolver: PartialFunction[Page, VDomModifier] = { demoWorker <- WebWorker[WorkerData]("/worker.js")
} yield {
case Page.Home => wire[HomePage].render case Page.Home => wire[HomePage].render
case Page.SomePage => case Page.SomePage =>
div( div(
div(cls := "title", "SomePage"), div(cls := "title", "SomePage"),
// RequestDemo(todoStore), // RequestDemo(todoStore),
Observable
.interval(1.second)
.doOnNextF(i => Coeval(println(s"Producer emitted $i")))
.doOnNextF(i =>
Coeval(demoWorker.onNext(WorkerData(i))) >> Coeval.unit
)
.map(_ => div()),
demoWorker.map(_.toString).map(v => p(cls := "text-white", v)),
requestDemo, requestDemo,
div(cls := "slider") div(cls := "slider")
) )
case Page.UserHome(id) => case Page.UserHome(id) =>
div(div(cls := "title", "UserHome"), s"User id: $id") div(
cls := "text-white",
div(cls := "title", "UserHome"),
s"User id: $id",
div(FusejsDemo.y.map(_.item.toString).mkString(" "))
)
case Page.NotFound => case Page.NotFound =>
div( div(
div(cls := "title", "NotFound"), div(cls := "title", "NotFound"),
p(cls := "profile-description", "notfound") p(cls := "text-white", "notfound")
) )
} }
} yield resolver
) )
} }

View File

@ -1,13 +1,15 @@
package outwatchapp package outwatchapp
import scala.scalajs.js.annotation.JSImport
import cats.effect.ExitCode import cats.effect.ExitCode
import monix.bio._ import monix.bio._
import org.scalajs.dom.raw.Element
import sttp.client.impl.monix.FetchMonixBackend
import org.scalajs.dom.document import org.scalajs.dom.document
import scala.scalajs.js.annotation.JSImport import org.scalajs.dom.raw.Element
import scalajs.js
import outwatch.router.AppRouter import outwatch.router.AppRouter
import sttp.client.impl.monix.FetchMonixBackend
import scalajs.js
@JSImport("bootstrap/dist/css/bootstrap.min.css", JSImport.Namespace) @JSImport("bootstrap/dist/css/bootstrap.min.css", JSImport.Namespace)
@js.native @js.native

View File

@ -1,13 +1,10 @@
package outwatchapp package outwatchapp
import colibri.ext.monix._
import monix.bio.Task import monix.bio.Task
import monix.eval.Coeval
import outwatch._ import outwatch._
import outwatch.dsl._ import outwatch.dsl._
import outwatch.router.AppRouter import outwatch.router.AppRouter
import outwatch.router._ import outwatch.router._
import outwatch.router.dsl._ import outwatch.router.dsl._
import outwatchapp.components.todo.FusejsDemo
import Page._ import Page._
@ -30,7 +27,7 @@ object Router {
cls := "navbar-translate", cls := "navbar-translate",
a( a(
cls := "navbar-brand", cls := "navbar-brand",
href := "https://demos.creative-tim.com/blk-design-system/index.html", href := "#",
rel := "tooltip", rel := "tooltip",
title := "", title := "",
attr("data-placement") := "bottom", attr("data-placement") := "bottom",
@ -104,11 +101,6 @@ object Router {
), ),
div( div(
cls := "container", cls := "container",
Coeval(
println("Result = " + FusejsDemo.y.toString())
) >> Coeval(
FusejsDemo.y.foreach(o => println(o.item))
) >> Coeval(div()),
router.render(resolver), router.render(resolver),
router.watch() router.watch()
) )

View File

@ -15,7 +15,7 @@ object CounterDemo {
def apply(): Task[HtmlVNode @@ CounterDemo] = def apply(): Task[HtmlVNode @@ CounterDemo] =
Task.deferAction(implicit s => Task.deferAction(implicit s =>
Task( Task(
div(p(cls := "profile-description", "count: ", counter2)) div(p(cls := "text-white", "count: ", counter2))
.taggedWith[CounterDemo] .taggedWith[CounterDemo]
) )
) )

View File

@ -5,15 +5,15 @@ import com.softwaremill.tagging._
import monix.bio._ import monix.bio._
import monix.eval.Coeval import monix.eval.Coeval
import monix.{eval => me} import monix.{eval => me}
import nova.monadic_sfx.ui.components.todo.Todo import outwatchapp.ui.components.todo.Todo
import nova.monadic_sfx.ui.components.todo.TodoListStore import outwatchapp.ui.components.todo.TodoListStore
import outwatchapp.util.reactive.store.Store
import outwatch._ import outwatch._
import outwatch.dsl._ import outwatch.dsl._
import outwatch.reactive.handlers.monix._ import outwatch.reactive.handlers.monix._
import outwatchapp.AppTypes import outwatchapp.AppTypes
import outwatchapp.implicits._ import outwatchapp.implicits._
import sttp.client._ import sttp.client._
import nova.monadic_sfx.util.reactive.store.Store
sealed trait RequestDemo sealed trait RequestDemo
object RequestDemo { object RequestDemo {
@ -57,15 +57,14 @@ object RequestDemo {
cls := "form-control", cls := "form-control",
placeholder := "0", placeholder := "0",
onInput.value --> requestSub onInput.value --> requestSub
)
), ),
div(
cls := "form-group",
label( label(
color := "hsla(0,0%,100%,0.8)", color := "hsla(0,0%,100%,0.8)",
"Enter content for todo" "Enter content for todo"
), ),
small(cls := "form-text text-muted", "default is 0")
),
div(
cls := "form-group",
input( input(
cls := "form-control", cls := "form-control",
onInput.value --> todoContent onInput.value --> todoContent
@ -78,8 +77,7 @@ object RequestDemo {
Coeval(println("Clicked")) Coeval(println("Clicked"))
), ),
onClick( onClick(
todoContent todoContent.map(TodoListStore.Add)
.map(TodoListStore.Add)
) --> todoStore.sink ) --> todoStore.sink
) )
) )
@ -87,7 +85,7 @@ object RequestDemo {
), ),
div( div(
p( p(
cls := "profile-description", cls := "text-white",
requestSub requestSub
.doOnNext(str => me.Task(println(str))) .doOnNext(str => me.Task(println(str)))
.mapEval(request) .mapEval(request)

View File

@ -1,6 +1,6 @@
package outwatchapp.components.todo package outwatchapp.components.todo
import nova.monadic_sfx.ui.components.todo.Todo import outwatchapp.ui.components.todo.Todo
import typings.fuseJs.mod.Fuse.IFuseOptions import typings.fuseJs.mod.Fuse.IFuseOptions
import typings.fuseJs.mod._ import typings.fuseJs.mod._
import typings.fuseJs.mod.{default => FuseC} import typings.fuseJs.mod.{default => FuseC}

View File

@ -18,7 +18,7 @@ class HomePage(
counterDemo, counterDemo,
chartDemo, chartDemo,
p( p(
cls := "profile-description", cls := "text-white",
div( div(
"hm", "hm",
htmlTag("blockQuote")( htmlTag("blockQuote")(

View File

@ -1,4 +1,4 @@
package nova.monadic_sfx.util.reactive.store package outwatchapp.util.reactive
import scala.concurrent.Future import scala.concurrent.Future

View File

@ -0,0 +1,11 @@
package outwatchapp.util
import monix.reactive.Observable
import monix.reactive.Observer
package object reactive {
type MonixProSubject[-I, +O] = Observable[O] with Observer[I]
type MonixSubject[A] = MonixProSubject[A, A]
type WebWorker[A] = MonixSubject[A]
type WebSocket[A] = MonixSubject[A]
}

View File

@ -1,4 +1,4 @@
package nova.monadic_sfx.util.reactive.store package outwatchapp.util.reactive.store
import java.time.LocalDateTime import java.time.LocalDateTime

View File

@ -1,4 +1,4 @@
package nova.monadic_sfx.util.reactive.store package outwatchapp.util.reactive.store
import cats.implicits._ import cats.implicits._
import monix.reactive.Observable import monix.reactive.Observable
@ -6,8 +6,7 @@ import monix.reactive.ObservableLike
object Reducer { object Reducer {
/** /** Creates a Reducer which yields a new State, as-well as an Observable of Effects
* Creates a Reducer which yields a new State, as-well as an Observable of Effects
* Effects are Actions which will be executed after the Action that caused them to occur. * Effects are Actions which will be executed after the Action that caused them to occur.
* This is accomplished by subscribing to the Effects Observable within the stores scan loop. * This is accomplished by subscribing to the Effects Observable within the stores scan loop.
* *
@ -20,14 +19,12 @@ object Reducer {
f: (M, A) => (M, F[A]) f: (M, A) => (M, F[A])
): Reducer[A, M] = (s: M, a: A) => f(s, a).map(ObservableLike[F].apply) ): Reducer[A, M] = (s: M, a: A) => f(s, a).map(ObservableLike[F].apply)
/** /** Creates a reducer which just transforms the state, without additional effects.
* Creates a reducer which just transforms the state, without additional effects.
*/ */
def apply[A, M](f: (M, A) => M): Reducer[A, M] = def apply[A, M](f: (M, A) => M): Reducer[A, M] =
(s: M, a: A) => f(s, a) -> Observable.empty (s: M, a: A) => f(s, a) -> Observable.empty
/** /** Creates a Reducer with an optional effect.
* Creates a Reducer with an optional effect.
*/ */
def withOptionalEffects[F[_]: ObservableLike, A, M]( def withOptionalEffects[F[_]: ObservableLike, A, M](
f: (M, A) => (M, Option[F[A]]) f: (M, A) => (M, Option[F[A]])

View File

@ -1,10 +1,11 @@
package nova.monadic_sfx.util.reactive.store package outwatchapp.util.reactive.store
import monix.bio.Task import monix.bio.Task
import monix.eval.Coeval import monix.eval.Coeval
import monix.reactive.Observer
import monix.reactive.OverflowStrategy import monix.reactive.OverflowStrategy
import monix.reactive.subjects.ConcurrentSubject import monix.reactive.subjects.ConcurrentSubject
import monix.reactive.Observer import outwatchapp.util.reactive.MonixProSubject
object Store { object Store {
def createL[A, M]( def createL[A, M](

View File

@ -1,15 +1,12 @@
package nova.monadic_sfx.util.reactive package outwatchapp.util.reactive
import monix.reactive.Observable import monix.reactive.Observable
import monix.reactive.Observer
package object store { package object store {
type MonixProSubject[-I, +O] = Observable[O] with Observer[I]
type Middleware[A, M] = Observable[(A, M)] => Observable[(A, M)] type Middleware[A, M] = Observable[(A, M)] => Observable[(A, M)]
type Store[A, M] = MonixProSubject[A, (A, M)] type Store[A, M] = MonixProSubject[A, (A, M)]
/** /** A Function that applies an Action onto the Stores current state.
* A Function that applies an Action onto the Stores current state.
* @param reducer The reducing function * @param reducer The reducing function
* @tparam A The Action Type * @tparam A The Action Type
* @tparam M The Model Type * @tparam M The Model Type