Skip to content

Commit

Permalink
remove ref
Browse files Browse the repository at this point in the history
  • Loading branch information
Deliganli committed Dec 18, 2020
1 parent ff9f7e7 commit ccf0673
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 30 deletions.
5 changes: 1 addition & 4 deletions src/main/scala/com/deliganli/maven/search/Main.scala
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.deliganli.maven.search

import cats.Applicative
import cats.effect.concurrent.Ref
import cats.effect.{ConcurrentEffect, ContextShift, ExitCode, IO, IOApp, Sync, Timer}
import cats.implicits._
import com.deliganli.maven.search.Program.{ProgramEvent, State}
Expand All @@ -25,9 +24,7 @@ object Main extends IOApp {
}

def entrypoint[F[_]: Sync](env: Environment[F]): F[Unit] = {
Applicative[F].unit
.flatMap(_ => Ref.of(State(0, Nil)))
.flatMap(cache => Program.interpret(env, cache)(ProgramEvent.Search(1)))
Program.interpret(env, State(0, Nil))(ProgramEvent.Search(1))
}

}
49 changes: 23 additions & 26 deletions src/main/scala/com/deliganli/maven/search/Program.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.deliganli.maven.search

import cats.effect.concurrent.Ref
import cats.implicits._
import cats.{Applicative, Monad}
import com.deliganli.maven.search.Domain.MavenModel
Expand Down Expand Up @@ -31,7 +30,7 @@ object Program {

def interpret[F[_]: Monad](
env: Environment[F],
state: Ref[F, State]
state: State
)(
event: ProgramEvent
): F[Unit] = {
Expand All @@ -41,9 +40,9 @@ object Program {
.flatMap {
case Prompt =>
def proceed(e: UserEvent): F[ProgramEvent] =
state.get
.flatTap(s => env.logger.debug(s"size: ${s.docs.size}, page:${s.page}"))
.map(s => env.transformer.userToProgram(s)(e))
Applicative[F].unit
.flatMap(_ => env.logger.debug(s"size: ${state.docs.size}, page:${state.page}"))
.map(_ => env.transformer.userToProgram(state)(e))

def terminate(e: String): F[ProgramEvent] =
env.terminal
Expand All @@ -56,37 +55,35 @@ object Program {
.flatMap(e => interpret(env, state)(e))

case Search(page) =>
def updateCache(m: MavenModel): F[Unit] =
state.update { s =>
val cursor = s.page * env.config.itemPerPage
if (cursor < s.docs.size) s.copy(page = page)
else State(page, s.docs ++ m.docs)
}
def updateCache(m: MavenModel): State = {
val cursor = state.page * env.config.itemPerPage
if (cursor < state.docs.size) state.copy(page = page)
else State(page, state.docs ++ m.docs)
}

Applicative[F].unit
.flatMap(_ => env.maven.search(page))
.flatTap(m => updateCache(m))
.flatTap(m => env.terminal.printTable(m.docs.take(env.config.itemPerPage), page))
.flatMap(_ => interpret(env, state)(Prompt))
.map(m => updateCache(m))
.flatMap(updatedState => interpret(env, updatedState)(Prompt))

case Move(page) =>
def updateCache(): F[List[MavenDoc]] =
state.modify { s =>
val updated = s.copy(page = page)
val from = s.page * env.config.itemPerPage
val till = from + env.config.itemPerPage
def updateCache(): (State, List[MavenDoc]) = {
val updated = state.copy(page = page)
val from = state.page * env.config.itemPerPage
val till = from + env.config.itemPerPage

(updated, s.docs.slice(from, till))
}
(updated, state.docs.slice(from, till))
}

Applicative[F].unit
.flatMap(_ => updateCache())
.flatTap(m => env.terminal.printTable(m, page))
.flatMap(_ => interpret(env, state)(Prompt))
Applicative[F]
.pure(updateCache())
.flatTap { case (_, m) => env.terminal.printTable(m, page) }
.flatMap { case (s, _) => interpret(env, s)(Prompt) }

case Copy(selection) =>
Applicative[F].unit
.flatMap(_ => state.get.map(s => s.docs((s.page - 1) * env.config.itemPerPage + selection)))
Applicative[F]
.pure(state.docs((state.page - 1) * env.config.itemPerPage + selection))
.map(doc => env.formatter.format(doc))
.flatTap(fs => env.clipboard.set(fs))
.flatTap(fs => env.terminal.putStrLn(s"Copied to clipboard: $fs"))
Expand Down

0 comments on commit ccf0673

Please sign in to comment.