@@ -31,7 +31,7 @@ class OTBranch(
31
31
private val store : IDeserializingKeyValueStore ,
32
32
) : IBranch {
33
33
private var currentOperations: MutableList <IAppliedOperation > = ArrayList ()
34
- private val completedChanges: MutableList <Pair < List < IAppliedOperation >, ITree > > = ArrayList ()
34
+ private val completedChanges: MutableList <OpsAndTree > = ArrayList ()
35
35
private val id: String = branch.getId()
36
36
37
37
fun operationApplied (op : IAppliedOperation ) {
@@ -52,10 +52,10 @@ class OTBranch(
52
52
fun getPendingChanges (): Pair <List <IAppliedOperation >, ITree> {
53
53
return runSynchronized(completedChanges) {
54
54
val result = when (completedChanges.size) {
55
- 0 -> emptyList< IAppliedOperation >() to computeReadT { it.tree }
56
- 1 -> completedChanges[ 0 ]
57
- else -> completedChanges.flatMap { it.first } to completedChanges.last().second
58
- }
55
+ 0 -> OpsAndTree ( computeReadT { it.tree })
56
+ 1 -> completedChanges.single()
57
+ else -> completedChanges.last().merge( completedChanges.dropLast( 1 ))
58
+ }.asPair()
59
59
completedChanges.clear()
60
60
result
61
61
}
@@ -91,7 +91,7 @@ class OTBranch(
91
91
try {
92
92
val result = computable()
93
93
runSynchronized(completedChanges) {
94
- completedChanges + = currentOperations to t.tree
94
+ completedChanges + = OpsAndTree ( currentOperations, t.tree)
95
95
}
96
96
result
97
97
} finally {
@@ -128,4 +128,12 @@ class OTBranch(
128
128
}
129
129
130
130
protected fun checkNotEDT () {}
131
+
132
+ private class OpsAndTree (val ops : List <IAppliedOperation >, val tree : ITree ) {
133
+ constructor (tree: ITree ) : this (emptyList(), tree)
134
+ fun asPair (): Pair <List <IAppliedOperation >, ITree> = ops to tree
135
+ fun merge (previous : List <OpsAndTree >): OpsAndTree {
136
+ return OpsAndTree ((previous + this ).flatMap { it.ops }.toList(), tree)
137
+ }
138
+ }
131
139
}
0 commit comments