From 58088c91451e4f3360d9085237dc7456d38bbce5 Mon Sep 17 00:00:00 2001 From: Arman Bilge Date: Mon, 23 Aug 2021 05:04:00 +0000 Subject: [PATCH] Create core module --- build.sbt | 17 ++++++- core/src/main/scala/feral/IOSetup.scala | 45 +++++++++++++++++++ .../main/scala/feral/lambda/IOLambda.scala | 28 ++---------- 3 files changed, 64 insertions(+), 26 deletions(-) create mode 100644 core/src/main/scala/feral/IOSetup.scala diff --git a/build.sbt b/build.sbt index 486e95f0..925d35f4 100644 --- a/build.sbt +++ b/build.sbt @@ -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 ) ) @@ -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) diff --git a/core/src/main/scala/feral/IOSetup.scala b/core/src/main/scala/feral/IOSetup.scala new file mode 100644 index 00000000..eda4dbe8 --- /dev/null +++ b/core/src/main/scala/feral/IOSetup.scala @@ -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 { + + 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] = { + val deferred = Deferred.unsafe[IO, Either[Throwable, Setup]] + setup + .attempt + .allocated + .flatTap { + case (setup, _) => + deferred.complete(setup) + } + .unsafeRunAndForget()(runtime) + deferred.get.rethrow + } + +} diff --git a/lambda/shared/src/main/scala/feral/lambda/IOLambda.scala b/lambda/shared/src/main/scala/feral/lambda/IOLambda.scala index d5e7e0a0..81d22e81 100644 --- a/lambda/shared/src/main/scala/feral/lambda/IOLambda.scala +++ b/lambda/shared/src/main/scala/feral/lambda/IOLambda.scala @@ -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]] }