-
Notifications
You must be signed in to change notification settings - Fork 241
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
11b8ef9
commit 24f29b6
Showing
6 changed files
with
89 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
modules/scala/scala-kernel-api/src/main/scala/almond/api/internal/Lazy.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package almond.api.internal | ||
|
||
final class Lazy[T](private var compute: () => T) { | ||
private var listeners = List.empty[Either[Throwable, T] => Unit] | ||
def onChange: (Either[Throwable, T] => Unit) => Unit = { f => | ||
listeners = f :: listeners | ||
} | ||
lazy val value: T = { | ||
val e = | ||
try Right(compute()) | ||
catch { | ||
case ex: Throwable => // catch less things here? | ||
Left(ex) | ||
} | ||
listeners.foreach(_(e)) | ||
e match { | ||
case Right(t) => | ||
compute = null | ||
t | ||
case Left(ex) => | ||
throw ex | ||
} | ||
} | ||
} |