Skip to content

Commit

Permalink
Merge pull request #5 from ioleo/main
Browse files Browse the repository at this point in the history
ScalaJS cross-compilation
  • Loading branch information
bilal-fazlani authored May 4, 2023
2 parents 6bbdd8a + 80a5bbe commit 6969052
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 42 deletions.
26 changes: 18 additions & 8 deletions build.sbt
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import sbtcrossproject.CrossPlugin.autoImport.{crossProject, CrossType}

val scala3Version = "3.2.2"
val scala2Version = "2.13.10"

Expand Down Expand Up @@ -27,26 +29,34 @@ ThisBuild / homepage := Some(url("https://zio-ulid.bilal-fazlani.com/"))

lazy val root = project
.in(file("."))
.aggregate(`zio-ulid`, benchmarks, examples)
.aggregate(
`zio-ulid`.jvm,
`zio-ulid`.js,
benchmarks,
examples
)
.settings(
name := "zio-ulid-root",
scalaVersion := scala2Version,
publish / skip := true
)

lazy val `zio-ulid` = project
lazy val `zio-ulid` = crossProject(JSPlatform, JVMPlatform)
.crossType(CrossType.Pure)
.in(file("./zio-ulid"))
.settings(
name := "zio-ulid",
scalaVersion := scala2Version,
crossScalaVersions := Seq(scala2Version, scala3Version),
libraryDependencies ++= Seq(
Libs.zio,
Libs.zioDirect,
Libs.zioTest,
Libs.zioTestSbt
"dev.zio" %%% "zio" % Libs.zioVersion,
"dev.zio" %%% "zio-test" % Libs.zioVersion,
"dev.zio" %%% "zio-test-sbt" % Libs.zioVersion
)
)
.jsSettings(
scalaJSUseMainModuleInitializer := true
)

lazy val benchmarks = project
.in(file("./benchmarks"))
Expand All @@ -63,7 +73,7 @@ lazy val benchmarks = project
BenchmarkLibs.SulkyUlid
)
)
.dependsOn(`zio-ulid`)
.dependsOn(`zio-ulid`.jvm)

lazy val examples = project
.in(file("./examples"))
Expand All @@ -72,4 +82,4 @@ lazy val examples = project
scalaVersion := scala2Version,
publish / skip := true
)
.dependsOn(`zio-ulid`)
.dependsOn(`zio-ulid`.jvm)
5 changes: 0 additions & 5 deletions project/Libs.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,6 @@ import sbt._

object Libs {
val zioVersion = "2.0.10"

lazy val zio = "dev.zio" %% "zio" % zioVersion
lazy val zioDirect = "dev.zio" %% "zio-direct" % "1.0.0-RC7"
lazy val zioTest = "dev.zio" %% "zio-test" % zioVersion
lazy val zioTestSbt = "dev.zio" %% "zio-test-sbt" % zioVersion
}

object BenchmarkLibs {
Expand Down
4 changes: 3 additions & 1 deletion project/plugins.sbt
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
addSbtPlugin("com.github.sbt" % "sbt-ci-release" % "1.5.11")
addSbtPlugin("pl.project13.scala" % "sbt-jmh" % "0.4.3")
addSbtPlugin("pl.project13.scala" % "sbt-jmh" % "0.4.3")
addSbtPlugin("org.scala-js" % "sbt-scalajs" % "1.13.1")
addSbtPlugin("org.portable-scala" % "sbt-scalajs-crossproject" % "1.0.0")
45 changes: 23 additions & 22 deletions zio-ulid/src/main/scala/com/bilalfazlani/zioUlid/ULIDGen.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package com.bilalfazlani.zioUlid

import java.util.concurrent.TimeUnit
import zio._
import zio.direct._

trait ULIDGen {
def nextULID: UIO[ULID]
Expand All @@ -18,27 +17,29 @@ object ULIDGen {
case class ULIDGenLive(state: Ref.Synchronized[BinaryULID]) extends ULIDGen {
private def generateBinary: UIO[BinaryULID] =
state.updateAndGetZIO { lastState =>
defer {
val currentMillis =
ZIO.clockWith(_.currentTime(TimeUnit.MILLISECONDS)).run
val lastTimestamp = lastState.timestamp
val differentTime = currentMillis != lastTimestamp
if (differentTime) {
// No conflict at millisecond level. We can generate a new ULID safely
val bin: IO[ULIDBytesParsingError.InvalidTimestamp, BinaryULID] =
BinaryULID(currentMillis)
bin.orDie.run
} else
// do increment
if (lastState.low != ~0L) BinaryULID(lastState.high, lastState.low + 1L)
else {
val nextHi = (lastState.high & ~(~0L << 16)) + 1
if ((nextHi & (~0L << 16)) != 0)
// Random number overflow. Wait for one millisecond and retry
(ZIO.sleep(1.millis) *> generateBinary).run
else BinaryULID(nextHi | currentMillis << (64 - 48), 0)
}
}
for {
currentMillis <- ZIO.clockWith(_.currentTime(TimeUnit.MILLISECONDS))
lastTimestamp = lastState.timestamp
differentTime = currentMillis != lastTimestamp
result <-
(if (differentTime) {
// No conflict at millisecond level. We can generate a new ULID safely
val bin: IO[ULIDBytesParsingError.InvalidTimestamp, BinaryULID] =
BinaryULID(currentMillis)
bin.orDie
} else
// do increment
if (lastState.low != ~0L)
ZIO.succeed(BinaryULID(lastState.high, lastState.low + 1L))
else {
val nextHi = (lastState.high & ~(~0L << 16)) + 1
if ((nextHi & (~0L << 16)) != 0)
// Random number overflow. Wait for one millisecond and retry
ZIO.sleep(1.millis) *> generateBinary
else
ZIO.succeed(BinaryULID(nextHi | currentMillis << (64 - 48), 0))
})
} yield result
}

def nextULID = generateBinary.map(ULID.unsafe)
Expand Down
10 changes: 4 additions & 6 deletions zio-ulid/src/main/scala/com/bilalfazlani/zioUlid/package.scala
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.bilalfazlani

import zio._
import zio.direct._
import com.bilalfazlani.zioUlid.ULIDBytesParsingError._
import com.bilalfazlani.zioUlid.ULIDStringParsingError._
import java.util.concurrent.TimeUnit
Expand Down Expand Up @@ -50,11 +49,10 @@ package object zioUlid {
private[zioUlid] def apply(
timestamp: Long
): IO[InvalidTimestamp, BinaryULID] =
defer {
val randBytes = ZIO.randomWith(_.nextBytes(10)).run
val _ = ZIO.fromEither(validateTimestamp(timestamp)).run
unsafe(timestamp, randBytes)
}
for {
randBytes <- ZIO.randomWith(_.nextBytes(10))
_ <- ZIO.fromEither(validateTimestamp(timestamp))
} yield unsafe(timestamp, randBytes)

// todo:remove var
private[zioUlid] def fromBytes(
Expand Down

0 comments on commit 6969052

Please sign in to comment.