11package reactify
22
3- import java .util .{Timer , TimerTask }
4-
53import reactify .reaction .{Reaction , ReactionStatus , Reactions }
64
75import 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