Skip to content

Commit

Permalink
refactor: move into package posts the examples
Browse files Browse the repository at this point in the history
  • Loading branch information
tassiluca committed Jan 12, 2024
1 parent a9cf14f commit deb31fd
Show file tree
Hide file tree
Showing 12 changed files with 287 additions and 251 deletions.
89 changes: 0 additions & 89 deletions src/main/scala/io/github/tassiLuca/gears/posts/PostsService.scala

This file was deleted.

22 changes: 0 additions & 22 deletions src/main/scala/io/github/tassiLuca/gears/posts/UsePostsApp.scala

This file was deleted.

102 changes: 0 additions & 102 deletions src/main/scala/io/github/tassiLuca/gears/posts/quo/PostsService.scala

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.github.tassiLuca.gears.posts
package io.github.tassiLuca.posts

import java.util.Date

Expand Down
18 changes: 18 additions & 0 deletions src/main/scala/io/github/tassiLuca/posts/Utils.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package io.github.tassiLuca.posts

import gears.async.default.given
import gears.async.Async
import gears.async.AsyncOperations

import scala.util.Random

extension (component: String)
def simulates(action: String)(using Async): Unit =
println(s"[$component - ${Thread.currentThread()}] $action")
AsyncOperations.sleep(Random.nextInt(10_000))
println(s"[$component - ${Thread.currentThread()}] ended $action")

def simulatesBlocking(action: String): Unit =
println(s"[$component - ${Thread.currentThread()}] $action")
Thread.sleep(Random.nextInt(10_000))
println(s"[$component - ${Thread.currentThread()}] ended $action")
35 changes: 35 additions & 0 deletions src/main/scala/io/github/tassiLuca/posts/direct/BlogPostsApp.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package io.github.tassiLuca.posts.direct

import io.github.tassiLuca.posts.PostsModel

object BlogPostsApp extends PostsServiceComponent with PostsModel with PostsRepositoryComponent:
override type AuthorId = String
override type Body = String
override type Title = String

override val repository: PostsRepository = PostsRepository()
override val service: PostsService = PostsService()

@main def useSimple(): Unit =
val app = BlogPostsApp
val result = app.service.create("ltassi@gmail.com", "A hello world post", "Hello World!")
println(s"Result: $result")
println(app.service.get("A hello world post"))

///* TODO WARNING: USING AN OBJECT WHICH EXTENDS `App` CAUSES STARVATION!
// * THE WORKER THREAD GETS STUCK WAITING FOR THE RELEASE OF THE MONITOR LOCK
// * waiting on the Class initialization monitor for io.github.tassiLuca.gears.posts.UsePostsApp$
// * TO BE INVESTIGATED! */
//@main def usePostsApp(): Unit =
// val app = BlogPostsApp
// Async.blocking:
// val f1 = Future:
// println(s"POST 1 carried by ${Thread.currentThread()}")
// app.postsService.create("ltassi@gmail.com", "A hello world post", "Hello World!")
// println(app.postsService.get("A hello world post"))
// val f2 = Future:
// println(s"POST 2 carried by ${Thread.currentThread()}")
// app.postsService.create("ltassi@gmail.com", "An another post", "Hello World 2!")
// println(app.postsService.get("An another post"))
// f1.await
// f2.await
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package io.github.tassiLuca.posts.direct

import gears.async.Async
import io.github.tassiLuca.posts.{PostsModel, simulates}

import scala.util.Try

/** The component exposing blog posts repositories. */
trait PostsRepositoryComponent:
context: PostsModel =>

/** The repository instance. */
val repository: PostsRepository

/** The repository in charge of storing and retrieving blog posts. */
trait PostsRepository:
/** Save the given [[post]]. */
def save(post: Post)(using Async): Try[Unit]

/** Load the post with the given [[postTitle]]. */
def load(postTitle: Title)(using Async): Try[Post]

/** Load all the saved post. */
def loadAll()(using Async): Try[LazyList[Post]]

object PostsRepository:
/** Constructs a new [[PostsRepository]]. */
def apply(): PostsRepository = PostsLocalRepository()

private class PostsLocalRepository extends PostsRepository:
private var posts: Set[Post] = Set()

override def save(post: Post)(using Async): Try[Unit] = Try:
require(posts.count(_.title == post.title) == 0, "A post with same title has already been saved")
"PostsRepository" simulates s"saving post ${post.title}"
synchronized { posts = posts + post }

override def load(postTitle: Title)(using Async): Try[Post] = Try:
"PostsRepository" simulates s"loading post $postTitle"
posts.find(_.title == postTitle).get

override def loadAll()(using Async): Try[LazyList[Post]] = Try:
"PostsRepository" simulates s"loading all blog posts"
LazyList.from(posts)
Loading

0 comments on commit deb31fd

Please sign in to comment.