Skip to content

Commit 1df081a

Browse files
committed
Updates for Reactive synchronize support
1 parent b508df2 commit 1df081a

2 files changed

Lines changed: 17 additions & 9 deletions

File tree

build.sbt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,15 @@ val scala213 = "2.13.18"
55

66
val scala212 = "2.12.21"
77

8-
val scala3 = List("3.3.7")
8+
val scala3 = "3.3.7"
99

1010
val scala2 = List(scala213, scala212)
11-
val allScalaVersions = scala2 ::: scala3
11+
val allScalaVersions = scala3 :: scala2
1212

1313
ThisBuild / name := "reactify"
1414
ThisBuild / organization := "com.outr"
15-
ThisBuild / version := "4.1.6-SNAPSHOT"
16-
ThisBuild / scalaVersion := scala213
15+
ThisBuild / version := "4.2.0-SNAPSHOT"
16+
ThisBuild / scalaVersion := scala3
1717
ThisBuild / crossScalaVersions := allScalaVersions
1818

1919
ThisBuild / sonatypeCredentialHost := Sonatype.sonatypeCentralHost

reactify/src/main/scala/reactify/Reactive.scala

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
11
package reactify
22

3-
import java.util.{Timer, TimerTask}
4-
53
import reactify.reaction.{Reaction, ReactionStatus, Reactions}
64

75
import scala.annotation.tailrec
8-
import scala.concurrent.duration.FiniteDuration
9-
import scala.concurrent.{Future, Promise, TimeoutException}
6+
import scala.concurrent.{Future, Promise}
107

118
/**
129
* Reactive is the core trait for Reactify. The basic premise is that a Reactive represents an instance that can attach
@@ -19,6 +16,11 @@ trait Reactive[T] {
1916
override def initialValue(): Option[ReactionStatus] = None
2017
}
2118

19+
/**
20+
* If true, events that are fired are synchronized to avoid concurrent invocations. Defaults to false.
21+
*/
22+
protected def synchronize: Boolean = false
23+
2224
/**
2325
* If the current thread is reacting to a value currently, status represents the status of the reaction. This can be
2426
* set to ReactionStatus.Stop in order to stop propagation. This can also be achieved via stopPropagation().
@@ -105,10 +107,16 @@ trait Reactive[T] {
105107
promise.future
106108
}
107109

110+
private def doFire(f: => ReactionStatus): ReactionStatus = if (synchronize) {
111+
synchronized(f)
112+
} else {
113+
f
114+
}
115+
108116
/**
109117
* Fires the value to the Reactions
110118
*/
111-
protected def fire(value: T, previous: Option[T], reactions: List[Reaction[T]] = this.reactions()): ReactionStatus = {
119+
protected def fire(value: T, previous: Option[T], reactions: List[Reaction[T]] = this.reactions()): ReactionStatus = doFire {
112120
_status.set(Some(ReactionStatus.Continue))
113121
try {
114122
fireInternal(value, previous, reactions)

0 commit comments

Comments
 (0)