Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -34,20 +34,32 @@ lazy val root =
project
.in(file("."))
.aggregate(
core.js,
core.jvm,
lambda.js,
lambda.jvm,
lambdaEvents.js,
lambdaEvents.jvm,
lambdaApiGatewayProxyHttp4s.js,
lambdaApiGatewayProxyHttp4s.jvm)
lambdaApiGatewayProxyHttp4s.jvm
)
.enablePlugins(NoPublishPlugin)

lazy val core = crossProject(JSPlatform, JVMPlatform)
.crossType(CrossType.Pure)
.in(file("core"))
.settings(
name := "feral-core",
libraryDependencies ++= Seq(
"org.typelevel" %%% "cats-effect" % catsEffectVersion
)
)

lazy val lambda = crossProject(JSPlatform, JVMPlatform)
.in(file("lambda"))
.settings(
name := "feral-lambda",
libraryDependencies ++= Seq(
"org.typelevel" %%% "cats-effect" % catsEffectVersion,
"io.circe" %%% "circe-core" % circeVersion
)
)
Expand All @@ -63,6 +75,7 @@ lazy val lambda = crossProject(JSPlatform, JVMPlatform)
"io.circe" %%% "circe-fs2" % "0.14.0"
)
)
.dependsOn(core)

lazy val lambdaEvents = crossProject(JSPlatform, JVMPlatform)
.crossType(CrossType.Pure)
Expand Down
45 changes: 45 additions & 0 deletions core/src/main/scala/feral/IOSetup.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Copyright 2021 Typelevel
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package feral

import cats.effect.unsafe.IORuntime
import cats.effect.kernel.Resource
import cats.effect.IO
import cats.effect.kernel.Deferred
import cats.syntax.all._

private[feral] trait IOSetup {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we want to expose this outside of feral?


protected def runtime: IORuntime = IORuntime.global

protected type Setup
protected def setup: Resource[IO, Setup] = Resource.pure(null.asInstanceOf[Setup])

private[feral] final lazy val setupMemo: IO[Setup] = {
Comment on lines +30 to +32
Copy link
Member Author

@armanbilge armanbilge Aug 23, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@djspiewak: @bpholt and I changed this to def / lazy val to avoid initialization errors, undoing your change in #1 to make setup a val. Is there a better way to handle this?

val deferred = Deferred.unsafe[IO, Either[Throwable, Setup]]
setup
.attempt
.allocated
.flatTap {
case (setup, _) =>
deferred.complete(setup)
}
.unsafeRunAndForget()(runtime)
deferred.get.rethrow
}

}
28 changes: 4 additions & 24 deletions lambda/shared/src/main/scala/feral/lambda/IOLambda.scala
Original file line number Diff line number Diff line change
Expand Up @@ -14,38 +14,18 @@
* limitations under the License.
*/

package feral.lambda
package feral
package lambda

import cats.effect.Deferred
import cats.effect.IO
import cats.effect.Resource
import cats.effect.unsafe.IORuntime
import cats.syntax.all._
import io.circe.Decoder
import io.circe.Encoder

abstract class IOLambda[Event, Result](
implicit private[lambda] val decoder: Decoder[Event],
private[lambda] val encoder: Encoder[Result]
) extends IOLambdaPlatform[Event, Result] {

protected def runtime: IORuntime = IORuntime.global

protected type Setup
protected val setup: Resource[IO, Setup] = Resource.pure(null.asInstanceOf[Setup])

private[lambda] final val setupMemo: IO[Setup] = {
val deferred = Deferred.unsafe[IO, Either[Throwable, Setup]]
setup
.attempt
.allocated
.flatTap {
case (setup, _) =>
deferred.complete(setup)
}
.unsafeRunAndForget()(runtime)
deferred.get.rethrow
}
) extends IOLambdaPlatform[Event, Result]
with IOSetup {

def apply(event: Event, context: Context, setup: Setup): IO[Option[Result]]
}