Skip to content

Instantly share code, notes, and snippets.

@japgolly
Created March 4, 2015 22:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save japgolly/e0be95b591fd077ba020 to your computer and use it in GitHub Desktop.
Save japgolly/e0be95b591fd077ba020 to your computer and use it in GitHub Desktop.
ConsoleIO.scala
package shipreq.webapp.client.lib
import org.scalajs.dom.console
import scala.annotation.elidable
import scala.scalajs.js
import scalaz.effect.IO
import js.{UndefOr, undefined}
trait ConsoleIO {
def info (msg: js.Any, extra: js.Any*) : IO[Unit]
def warn (msg: js.Any, extra: js.Any*) : IO[Unit]
def error (msg: js.Any, extra: js.Any*) : IO[Unit]
def log (msg: js.Any, extra: js.Any*) : IO[Unit]
def assert (test: Boolean, msg: String, extra: js.Any*): IO[Unit]
def clear () : IO[Unit]
def dir (value: js.Any, extra: js.Any*) : IO[Unit]
def profile (reportName: UndefOr[String] = undefined) : IO[Unit]
def profileEnd() : IO[Unit]
}
object ConsoleIO {
private final val L = elidable.INFO
@elidable(L) private def newInstance: ConsoleIO = {
new ConsoleIO {
override def info (msg: js.Any, extra: js.Any*) = IO[Unit](console.info (msg, extra: _*))
override def warn (msg: js.Any, extra: js.Any*) = IO[Unit](console.warn (msg, extra: _*))
override def error (msg: js.Any, extra: js.Any*) = IO[Unit](console.error (msg, extra: _*))
override def log (msg: js.Any, extra: js.Any*) = IO[Unit](console.log (msg, extra: _*))
override def assert (test: Boolean, msg: String, extra: js.Any*) = IO[Unit](console.assert (test, msg, extra: _*))
override def clear () = IO[Unit](console.clear ())
override def dir (value: js.Any, extra: js.Any*) = IO[Unit](console.dir (value, extra: _*))
override def profile (reportName: UndefOr[String] = undefined) = IO[Unit](reportName.fold(console.profile())(console.profile))
override def profileEnd() = IO[Unit](console.profileEnd())
}
}
private val instance = newInstance
private val nopIO = IO(())
@inline final def apply(f: ConsoleIO => IO[Unit]): IO[Unit] =
if (instance == null) nopIO else f(instance)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment