Skip to content

Commit 19884cf

Browse files
committed
More documentation
1 parent 2fa69fd commit 19884cf

File tree

11 files changed

+163
-1
lines changed

11 files changed

+163
-1
lines changed

shared/src/main/scala/reactify/State.scala

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,37 @@ case class State[T](owner: Reactive[T], index: Long, function: () => T) extends
2121
ReactionStatus.Continue
2222
}
2323

24+
/**
25+
* The previous state before this one
26+
*/
2427
def previousState: Option[State[T]] = _previousState
2528

29+
/**
30+
* The next state after this one if this is not active
31+
*/
2632
def nextState: Option[State[T]] = _nextState
2733

34+
/**
35+
* True if it is the currently active state
36+
*/
2837
def active: Boolean = nextState.isEmpty
38+
39+
/**
40+
* The currently active state
41+
*/
2942
def activeState: State[T] = nextState match {
3043
case Some(next) => next.activeState
3144
case None => this
3245
}
3346

47+
/**
48+
* Currently cached value derived from state function
49+
*/
3450
def cached: Option[T] = _value
3551

52+
/**
53+
* Current value of this state
54+
*/
3655
def value: T = {
3756
StateCounter.referenced(this)
3857
updatingState match {
@@ -53,8 +72,14 @@ case class State[T](owner: Reactive[T], index: Long, function: () => T) extends
5372
previousState.flatMap(_.updatingState)
5473
}
5574

75+
/**
76+
* All states referenced in the function deriving this state's value
77+
*/
5678
def references: List[State[_]] = _references
5779

80+
/**
81+
* Updates the derived value of this state
82+
*/
5883
def update(previous: Option[State[T]] = _previousState): Unit = synchronized {
5984
if (!updating.get()) {
6085
clearReferences()
@@ -108,6 +133,9 @@ case class State[T](owner: Reactive[T], index: Long, function: () => T) extends
108133
state.owner.asInstanceOf[Reactive[Any]].reactions -= this
109134
}
110135

136+
/**
137+
* Clears all references to other states from this state
138+
*/
111139
def clearReferences(): Unit = synchronized {
112140
references.foreach(removeReference)
113141
_references = Nil

shared/src/main/scala/reactify/StateCounter.scala

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ object StateCounter {
2828
}
2929
}
3030

31+
/**
32+
* Called by a State when it is referenced to get the value
33+
*/
3134
def referenced(state: State[_]): Unit = {
3235
instance.get().foreach { counter =>
3336
counter.references = state :: counter.references

shared/src/main/scala/reactify/bind/BindSet.scala

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,22 @@ package reactify.bind
22

33
sealed trait BindSet
44

5+
/**
6+
* BindSet defines how a binding should be applied when first defined
7+
*/
58
object BindSet {
9+
/**
10+
* The left value is assigned to the right
11+
*/
612
case object LeftToRight extends BindSet
13+
14+
/**
15+
* The right value is assigned to the left
16+
*/
717
case object RightToLeft extends BindSet
18+
19+
/**
20+
* Values are not modified at bind-time
21+
*/
822
case object None extends BindSet
923
}

shared/src/main/scala/reactify/bind/Binding.scala

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,13 @@ package reactify.bind
33
import reactify.Var
44
import reactify.reaction.Reaction
55

6+
/**
7+
* Binding represents a two-way binding between two `Var`s
8+
*/
69
class Binding[L, R](left: Var[L], right: Var[R], leftToRight: Reaction[L], rightToLeft: Reaction[R]) {
10+
/**
11+
* Detaches the binding
12+
*/
713
def detach(): Unit = {
814
left.reactions -= leftToRight
915
right.reactions -= rightToLeft

shared/src/main/scala/reactify/group/Group.scala

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ package reactify.group
22

33
import reactify.Reactive
44

5+
/**
6+
* Group represents a simple grouping of multiple underlying instances
7+
*/
58
trait Group[T, R <: Reactive[T]] {
69
def items: List[R]
710
}

shared/src/main/scala/reactify/package.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ package object reactify {
44
implicit def val2Value[T](v: Val[T]): T = v()
55

66
/**
7-
* Syntactic sugar for mutating collections in a `StateChannel`
7+
* Syntactic sugar for mutating collections in a `Var`
88
*/
99
implicit class ListVar[T](v: Var[List[T]]) {
1010
def +=(t: T): Unit = v := (v() ::: List(t))

shared/src/main/scala/reactify/reaction/Reaction.scala

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,26 @@ package reactify.reaction
22

33
import reactify.Priority
44

5+
/**
6+
* Reaction may be thought of similar to `Observer` or `Listener` in other libraries. A Reaction may be added to a
7+
* Reactive in order react when a value is fired upon it.
8+
*
9+
* @tparam T the type received by this Reaction
10+
*/
511
trait Reaction[T] extends Ordered[Reaction[T]] {
12+
/**
13+
* Invoked when a new value is received by the associated Reactive
14+
*
15+
* @param value the new value
16+
* @param previous the previous value, if one was defined
17+
* @return
18+
*/
619
def apply(value: T, previous: Option[T]): ReactionStatus
20+
21+
/**
22+
* Priority of this Reaction in contrast to other Reactions attached to the same Reactive. A higher value represents
23+
* a higher position in reactions list. See Priority for pre-defined values.
24+
*/
725
def priority: Double = Priority.Normal
826

927
override def compare(that: Reaction[T]): Int = this.priority.compare(that.priority)

shared/src/main/scala/reactify/reaction/ReactionStatus.scala

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,18 @@ package reactify.reaction
22

33
trait ReactionStatus
44

5+
/**
6+
* ReactionStatus is utilized during the propagation of a fired value against a Reactive. This value may be returned by
7+
* a Reaction or set via `status` on the Reactive (on the same thread while the fired value is propagating)
8+
*/
59
object ReactionStatus {
10+
/**
11+
* Continue propagation
12+
*/
613
case object Continue extends ReactionStatus
14+
15+
/**
16+
* Stop propagation
17+
*/
718
case object Stop extends ReactionStatus
819
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,26 @@
11
package reactify.reaction
22

3+
/**
4+
* Reactions represents a list of Reaction instances specifically associated with a Reactive
5+
*/
36
trait Reactions[T] {
7+
/**
8+
* Return all Reactions associated with this Reactive
9+
*/
410
def apply(): List[Reaction[T]]
11+
12+
/**
13+
* Add a Reaction
14+
*/
515
def +=(reaction: Reaction[T]): Reaction[T]
16+
17+
/**
18+
* Remove a Reaction
19+
*/
620
def -=(reaction: Reaction[T]): Boolean
21+
22+
/**
23+
* Remove all Reactions
24+
*/
725
def clear(): Unit
826
}

shared/src/main/scala/reactify/transaction/Transaction.scala

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ import reactify.Var
55
class Transaction {
66
private var map = Map.empty[Var[_], TransactionChange]
77

8+
/**
9+
* Called when the value of a Var changes
10+
*/
811
def change[T](owner: Var[T], oldFunction: () => T, newFunction: () => T): Unit = {
912
val change = map.get(owner) match {
1013
case Some(c) => c.copy(apply = () => owner := newFunction())
@@ -13,50 +16,83 @@ class Transaction {
1316
map += owner -> change
1417
}
1518

19+
/**
20+
* Gets the `TransactionChange` for the supplied `Var` if one is defined
21+
*/
1622
def get[T](v: Var[T]): Option[TransactionChange] = map.get(v)
1723

24+
/**
25+
* Returns the `TransactionChange` for the supplied `Var` or throws an exception if none exists
26+
*/
1827
def apply[T](v: Var[T]): TransactionChange = get(v).getOrElse(throw new RuntimeException(s"No reference in transaction for $v"))
1928

29+
/**
30+
* Commits all changes in this Transaction and then clears the transaction
31+
*/
2032
def commit(): Unit = {
2133
map.keys.foreach { v =>
2234
commit(v.asInstanceOf[Var[Any]])
2335
}
2436
map = Map.empty
2537
}
2638

39+
/**
40+
* Reverts all changes in this Transaction and then clears the transaction
41+
*/
2742
def revert(): Unit = {
2843
map.keys.foreach { v =>
2944
revert(v.asInstanceOf[Var[Any]])
3045
}
3146
map = Map.empty
3247
}
3348

49+
/**
50+
* Undoes all changes that occurred within this Transaction. Unlike `revert`, this doesn't clear the transaction.
51+
* This allows `redo` to run to re-apply the transaction in the future.
52+
*/
3453
def undo(): Unit = {
3554
map.keys.foreach { v =>
3655
undo(v.asInstanceOf[Var[Any]])
3756
}
3857
}
3958

59+
/**
60+
* Redoes all changes that occurred within this Transaction. Unlike `commit`, this doesn't clear the transaction.
61+
* This allows `undo` to un-apply the transaction in the future.
62+
*/
4063
def redo(): Unit = {
4164
map.keys.foreach { v =>
4265
redo(v.asInstanceOf[Var[Any]])
4366
}
4467
}
4568

69+
/**
70+
* Redoes the transaction for this `Var` and then clears it from the transaction.
71+
*
72+
* @return true if a change was applied
73+
*/
4674
def commit[T](v: Var[T]): Boolean = if (redo(v)) {
4775
map -= v
4876
true
4977
} else {
5078
false
5179
}
5280

81+
/**
82+
* Undoes the transaction for this `Var` and then clears it from the transaction.
83+
*
84+
* @return true if a change was applied
85+
*/
5386
def revert[T](v: Var[T]): Boolean = if (undo(v)) {
5487
map -= v
5588
true
5689
} else {
5790
false
5891
}
5992

93+
/**
94+
* Undoes the transaction for this `Var`.
95+
*/
6096
def undo[T](v: Var[T]): Boolean = get(v) match {
6197
case Some(change) => {
6298
change.unapply()
@@ -65,6 +101,9 @@ class Transaction {
65101
case None => false
66102
}
67103

104+
/**
105+
* Redoes the transaction for this `Var`.
106+
*/
68107
def redo[T](v: Var[T]): Boolean = get(v) match {
69108
case Some(change) => {
70109
change.apply()
@@ -74,13 +113,26 @@ class Transaction {
74113
}
75114
}
76115

116+
/**
117+
* Transaction allows access to undo, redo, revert, and commit changes to `Var`s
118+
*/
77119
object Transaction {
78120
private val threadLocal = new ThreadLocal[Option[Transaction]] {
79121
override def initialValue(): Option[Transaction] = None
80122
}
81123

124+
/**
125+
* True if a Transaction is currently active on the current thread
126+
*/
82127
def active: Boolean = threadLocal.get().nonEmpty
83128

129+
/**
130+
* Creates a new Transaction if one isn't already active or re-uses an existing one if a Transaction is already
131+
* in-progress for this thread.
132+
*
133+
* @param f the function to run within a Transaction
134+
* @return Transaction
135+
*/
84136
def apply(f: => Unit): Transaction = {
85137
val created = !active
86138
val transaction = threadLocal.get().getOrElse {
@@ -95,6 +147,9 @@ object Transaction {
95147
transaction
96148
}
97149

150+
/**
151+
* Called when the value of a Var changes
152+
*/
98153
def change[T](owner: Var[T], oldFunction: () => T, newFunction: () => T): Unit = {
99154
threadLocal.get().foreach(_.change(owner, oldFunction, newFunction))
100155
}

0 commit comments

Comments
 (0)