All Downloads are FREE. Search and download functionalities are using the official Maven repository.

de.sciss.mellite.impl.InterpreterSingleton.scala Maven / Gradle / Ivy

The newest version!
/*
 *  InterpreterSingleton.scala
 *  (Mellite)
 *
 *  Copyright (c) 2012-2019 Hanns Holger Rutz. All rights reserved.
 *
 *  This software is published under the GNU Affero General Public License v3+
 *
 *
 *  For further information, please contact Hanns Holger Rutz at
 *  [email protected]
 */

package de.sciss.mellite
package impl

import de.sciss.scalainterpreter.Interpreter

import scala.collection.immutable.{IndexedSeq => Vec}

object InterpreterSingleton {
  private val sync = new AnyRef

  private var funs  = Vec   .empty[Interpreter => Unit]
  private var inOpt = Option.empty[Interpreter]

  //   object Result {
  //      var value : Any = ()
  //   }
  //
  //   def wrap( code: String ) : String = {
  //      "de.sciss.mellite.gui.impl.InterpreterSingleton.Result.value = {" + code + "}"
  //   }

  def apply(fun: Interpreter => Unit): Unit =
    sync.synchronized {
      inOpt match {
        case Some(in) =>
          fun(in)
        case _ =>
          makeOne
          funs :+= fun
      }
    }

  private lazy val makeOne: Unit = {
    val cfg = Interpreter.Config()
    cfg.imports ++= Seq(
      "de.sciss.synth",
      "synth._",
      "ugen._"
    )
    //      cfg.bindings += NamedParam( Result )
    Interpreter.async(cfg).foreach { in =>
      sync.synchronized {
        inOpt = Some(in)
        val f = funs
        funs  = Vector.empty
        f.foreach(_.apply(in))
      }
    }
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy