Skip to content
Permalink
Browse files

Updated dependencies and replaced SingleThreadedFuture with KeyedSequ…

…ence for easier usage and better cleanup strategy
  • Loading branch information
darkfrog26 committed Jan 18, 2020
1 parent 1a37fe4 commit bd1a8413e2280c582d6e960058f732bb10ec4880
@@ -4,7 +4,7 @@ import sbtcrossproject.CrossType

name := "youi"
organization in ThisBuild := "io.youi"
version in ThisBuild := "0.12.12"
version in ThisBuild := "0.12.13-SNAPSHOT"
scalaVersion in ThisBuild := "2.13.1"
crossScalaVersions in ThisBuild := List("2.13.1", "2.12.10")
resolvers in ThisBuild ++= Seq(
@@ -39,14 +39,14 @@ val openTypeVersion = "0.7.3_1"
val picaVersion = "3.0.5_1"
val webFontLoaderVersion = "1.6.28_1"

val scalaJSDOM = "0.9.7"
val okHttpVersion = "4.2.2"
val scalaJSDOM = "0.9.8"
val okHttpVersion = "4.3.1"
val uaDetectorVersion = "2014.10"
val undertowVersion = "2.0.28.Final"
val closureCompilerVersion = "v20191111"
val undertowVersion = "2.0.29.Final"
val closureCompilerVersion = "v20200112"
val jSoupVersion = "1.12.1"
val scalaXMLVersion = "1.2.0"
val collectionCompat = "2.1.2"
val collectionCompat = "2.1.3"
val scalaTestVersion = "3.1.0-SNAP13"
val scalaCheckVersion = "1.14.0"

@@ -0,0 +1,41 @@
package io.youi

import scala.concurrent.Future
import scala.util.{Failure, Success}

import scribe.Execution.global

/**
* KeyedSequence allows the use of a unique key to build sequences of futures so that key is not concurrently utilized.
*
* Should self-clean upon completion.
*/
object KeyedSequence {
var errorHandler: Throwable => Unit = (t: Throwable) => scribe.error(t)

private var map = Map.empty[Any, Future[Any]]

def apply[Key, Return](key: Key)(builder: => Future[Return]): Future[Return] = synchronized {
val future: Future[Return] = map.get(key) match {
case Some(running) => {
running.transformWith { _ =>
builder
}
}
case None => {
builder
}
}
map += key -> future
future.onComplete { result =>
if (map(key) eq future) {
map -= key
}
result match {
case Success(_) => // Success, nothing to do
case Failure(exception) => errorHandler(exception)
}
}
future
}
}

This file was deleted.

@@ -1,21 +1,20 @@
package io.youi.image.resize

import com.outr.pica.{Pica, ResizeOptions}
import io.youi.SingleThreadedFuture
import io.youi.KeyedSequence
import org.scalajs.dom.html

import scala.concurrent.Future
import scala.scalajs.js.|

object PicaResizer extends ImageResizer {
private lazy val pica: Pica = Pica()
private val picaFuture = new SingleThreadedFuture()

override protected def resizeInternal(source: html.Image | html.Canvas, destination: html.Canvas): Future[html.Canvas] = {
picaFuture {
KeyedSequence(this) {
pica.resize(source, destination, new ResizeOptions {
alpha = true
}).toFuture
}
}
}
}
@@ -5,7 +5,7 @@ import io.youi.image.resize.ImageResizer
import io.youi.video.Video
import io.youi.{dom, _}
import org.scalajs.dom._
import org.scalajs.dom.raw.{CanvasRenderingContext2D, File, FileReader, URL}
import org.scalajs.dom.raw.{CanvasRenderingContext2D, File}

import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.{Future, Promise}
@@ -61,10 +61,9 @@ object ImageUtility {
}

private val tempImage = dom.create[html.Image]("img")
private val loadImageFuture = new SingleThreadedFuture()

def loadImage[R](file: File)(process: html.Image => Future[R]): Future[R] = {
loadImageFuture {
KeyedSequence(this) {
val promise = Promise[Unit]
val listener: js.Function1[Event, _] = (_: Event) => promise.success(())
tempImage.addEventListener("load", listener)

0 comments on commit bd1a841

Please sign in to comment.
You can’t perform that action at this time.