Skip to content

Commit 9f624af

Browse files
committed
Refactor Allocator and syntax for improved clarity; update README to reflect changes
1 parent 3fb2c6c commit 9f624af

File tree

3 files changed

+12
-8
lines changed

3 files changed

+12
-8
lines changed

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ Allocator.create[IO]().withListener(new LoggingAllocationListener[IO])
108108

109109
You can have multiple dependencies objects and combine them together. In this case, you can either reuse the same
110110
`Allocator` object or create a new one for each dependency object, but wrap their instantiation
111-
in `allocator.allocate { ... }` so that they are shut down in the right order:
111+
in `allocate { ... }` so that they are shut down in the right order:
112112

113113
Example reusing the same `Allocator` object:
114114

@@ -129,7 +129,7 @@ object Dependencies {
129129
}
130130

131131
class Dependencies(using AllocatorIO) {
132-
val aws = new AwsDependencies(allocator)
132+
val aws = new AwsDependencies
133133

134134
lazy val http4sClient: Client[IO] = allocate {
135135
EmberClientBuilder.default[IO].build
@@ -168,11 +168,11 @@ object Dependencies {
168168
}
169169

170170
class Dependencies(using AllocatorIO) {
171-
lazy val aws = allocator.allocate {
171+
lazy val aws = allocate {
172172
AwsDependencies.create()
173173
}
174174

175-
lazy val http4sClient: Client[IO] = http4sAllocator.allocate {
175+
lazy val http4sClient: Client[IO] = allocate {
176176
EmberClientBuilder.default[IO].build
177177
}
178178
}

src/main/scala/me/ivovk/cedi/Allocator.scala

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,10 @@ object Allocator {
1515
dispatcher <- Dispatcher.parallel[F]
1616
shutdownRef <- Ref.of(Async[F].unit).toResource
1717
allocator <- {
18-
val acquire = Async[F].delay(new Allocator(dispatcher, shutdownRef, NoOpListener[F]))
19-
val release = (a: Allocator[F]) => a.shutdownAll
18+
val acquire: F[Allocator[F]] = Async[F].delay {
19+
new Allocator(dispatcher, shutdownRef, NoOpListener[F])
20+
}
21+
val release: Allocator[F] => F[Unit] = _.shutdownAll
2022

2123
Resource.make(acquire)(release)
2224
}
@@ -56,7 +58,8 @@ class Allocator[F[_]: Sync] private (
5658
dispatcher.unsafeRunSync(fa)
5759
}
5860

59-
def allocate[A: ClassTag](fa: F[A]): A = allocate(fa.toResource)
61+
def allocate[A: ClassTag](fa: F[A]): A =
62+
allocate(fa.toResource)
6063

6164
def shutdownAll: F[Unit] =
6265
shutdown.getAndSet(Sync[F].unit).flatten

src/main/scala/me/ivovk/cedi/syntax.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ import scala.reflect.ClassTag
77
object syntax {
88

99
type Allocator[F[_]] = me.ivovk.cedi.Allocator[F]
10-
type AllocatorIO = me.ivovk.cedi.Allocator[IO]
10+
val Allocator: me.ivovk.cedi.Allocator.type = me.ivovk.cedi.Allocator
11+
type AllocatorIO = me.ivovk.cedi.Allocator[IO]
1112

1213
def allocate[F[_]: Allocator, A: ClassTag](fa: F[A]): A =
1314
Allocator[F].allocate(fa)

0 commit comments

Comments
 (0)