diff options
author | Martin Odersky <odersky@gmail.com> | 2010-08-30 16:02:34 +0000 |
---|---|---|
committer | Martin Odersky <odersky@gmail.com> | 2010-08-30 16:02:34 +0000 |
commit | 8964f6f1bcc8500f1bc6a2808ef70d8852d208ec (patch) | |
tree | cc558460d2f7be3238af3bf80bc95f5422313641 /src/compiler/scala/tools/nsc/interactive/Response.scala | |
parent | 41e2c237dfdcaffaa53edc6ef30b044353bce5e4 (diff) | |
download | scala-8964f6f1bcc8500f1bc6a2808ef70d8852d208ec.tar.gz scala-8964f6f1bcc8500f1bc6a2808ef70d8852d208ec.tar.bz2 scala-8964f6f1bcc8500f1bc6a2808ef70d8852d208ec.zip |
New wider interface of presentation compiler.
Diffstat (limited to 'src/compiler/scala/tools/nsc/interactive/Response.scala')
-rw-r--r-- | src/compiler/scala/tools/nsc/interactive/Response.scala | 68 |
1 files changed, 68 insertions, 0 deletions
diff --git a/src/compiler/scala/tools/nsc/interactive/Response.scala b/src/compiler/scala/tools/nsc/interactive/Response.scala new file mode 100644 index 0000000000..96e474a34a --- /dev/null +++ b/src/compiler/scala/tools/nsc/interactive/Response.scala @@ -0,0 +1,68 @@ +package scala.tools.nsc +package interactive + +import scala.concurrent.SyncVar + +/** Typical interaction, given a predicate <user-input>, a function <display>, + * and an exception handler <handle>: + * + * val TIMEOUT = 100 // (milliseconds) or something like that + * val r = new Response() + * while (!r.isComplete && !r.isCancelled) { + * if (<user-input>) r.cancel() + * else r.get(TIMEOUT) match { + * case Some(Left(data)) => <display>(data) + * case Some(Right(exc)) => <handle>(exc) + * case None => + * } + * } + */ +class Response[T] { + + private val data = new SyncVar[Either[T, Throwable]] + private var complete = false + private var cancelled = false + + /** Set provisional data, more to come + */ + def setProvisionally(x: T) = + data.set(Left(x)) + + /** Set final data, and mark resposne as complete. + */ + def set(x: T) = data.synchronized { + data.set(Left(x)) + complete = true + } + + def raise(exc: Throwable) = data.synchronized { + data.set(Right(exc)) + complete = true + } + + /** Get data, wait as long as necessary + */ + def get: Either[T, Throwable] = data.get + + /** Optionally get data within `timeout` milliseconds. + */ + def get(timeout: Long): Option[Either[T, Throwable]] = data.get(timeout) + + /** Final data set was stored + */ + def isComplete = data.synchronized { complete } + + /** Cancel action computing this response + */ + def cancel() = data.synchronized { cancelled = true } + + /** A cancel request for this response has been issued + */ + def isCancelled = data.synchronized { cancelled } + + def clear() = data.synchronized { + data.unset() + complete = false + cancelled = false + } +} |