-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: move into package posts the examples
- Loading branch information
Showing
12 changed files
with
287 additions
and
251 deletions.
There are no files selected for viewing
89 changes: 0 additions & 89 deletions
89
src/main/scala/io/github/tassiLuca/gears/posts/PostsService.scala
This file was deleted.
Oops, something went wrong.
22 changes: 0 additions & 22 deletions
22
src/main/scala/io/github/tassiLuca/gears/posts/UsePostsApp.scala
This file was deleted.
Oops, something went wrong.
102 changes: 0 additions & 102 deletions
102
src/main/scala/io/github/tassiLuca/gears/posts/quo/PostsService.scala
This file was deleted.
Oops, something went wrong.
37 changes: 0 additions & 37 deletions
37
src/main/scala/io/github/tassiLuca/gears/posts/quo/UsePostsApp.scala
This file was deleted.
Oops, something went wrong.
2 changes: 1 addition & 1 deletion
2
...ub/tassiLuca/gears/posts/PostsModel.scala → ...o/github/tassiLuca/posts/PostsModel.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
35
src/main/scala/io/github/tassiLuca/posts/direct/BlogPostsApp.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
44 changes: 44 additions & 0 deletions
44
src/main/scala/io/github/tassiLuca/posts/direct/PostsRepositoryComponent.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
Oops, something went wrong.