@@ -3,7 +3,6 @@ import { Timestamps } from 'util/timestamps';
33
44import { Identity } from '../../identity' ;
55import { Hash , HashedObject , MutableObject , MutationOp , MutableContentEvents , ClassRegistry } from '../../model' ;
6- import { RefUpdateOp } from '../mutable/MutableReference' ;
76
87import { Authorizer } from '../../model/causal/Authorization' ;
98import { Verification } from '../../model/causal/Authorization' ;
@@ -26,9 +25,10 @@ function sig(op: CausalRefUpdateOp<any>) {
2625// the following should return -1 if u2 comes after u1;
2726
2827function compareUpdateSigs ( u1 : UpdateSig , u2 : UpdateSig ) {
28+
2929 if ( u2 . sequence > u1 . sequence ) {
3030 return - 1 ;
31- } else if ( u1 . sequence < u2 . sequence ) {
31+ } else if ( u1 . sequence > u2 . sequence ) {
3232 return 1 ;
3333 } else { // u2.sequence === u1.sequence
3434 if ( Timestamps . after ( u2 . timestamp , u1 . timestamp ) ) {
@@ -54,7 +54,7 @@ class CausalReference<T> extends BaseCausalCollection<T> {
5454 _largestSequence ?: number ;
5555
5656 constructor ( config ?: CausalCollectionConfig ) {
57- super ( [ CausalRefUpdateOp . className ] , config ) ;
57+ super ( [ CausalRefUpdateOp . className ] , { ... config , supportsUndo : true } ) ;
5858
5959 this . setRandomId ( ) ;
6060
@@ -104,34 +104,33 @@ class CausalReference<T> extends BaseCausalCollection<T> {
104104
105105 let mutated = false ;
106106
107- if ( op instanceof RefUpdateOp ) {
107+ if ( op instanceof CausalRefUpdateOp ) {
108+
109+ //console.log('processing sequence ' + op.sequence + ', valid=' + valid)
108110
109111 const up = sig ( op ) ;
110112
111113 let idx : number ; // the position of op in the array
112114
113- if ( ! this . _allAppliedOps . has ( up . opHash ) ) {
115+ // find the right place for the op in the array:
116+ const length = this . _causallyOrderedUpdates . length
117+ idx = length ;
114118
115- // if the op has not been applied, we find the right place for it:
116- const length = this . _causallyOrderedUpdates . length
117- idx = length ;
119+ while ( idx > 0 && compareUpdateSigs ( this . _causallyOrderedUpdates [ idx - 1 ] , up ) > 0 ) {
120+ idx = idx - 1 ;
121+ }
118122
119- while ( idx > 0 && compareUpdateSigs ( this . _causallyOrderedUpdates [ idx - 1 ] , up ) > 0 ) {
120- idx = idx - 1 ;
121- }
123+ // and then insert it there, if it was not there already:
122124
123- // and then insert it there:
125+ // NOTE: since the compare above failed, upds[idx-1] <= up
126+ if ( idx === 0 || this . _causallyOrderedUpdates [ idx - 1 ] . opHash !== up . opHash ) {
127+ // upds[idx-1] < up => insert in position idx
124128 this . _causallyOrderedUpdates . splice ( idx , 0 , up ) ;
125129 } else {
126-
127- // else, we just go through the array until we find it
128- idx = length - 1 ;
129-
130- while ( idx > 0 && this . _causallyOrderedUpdates [ idx - 1 ] . opHash !== up . opHash ) {
131- idx = idx - 1 ;
132- }
130+ // upds[idx-1] === up => use old position idx-1
131+ idx = idx - 1 ;
133132 }
134-
133+
135134 let newValueIdx : number | undefined ;
136135 let newValueOp : CausalRefUpdateOp < T > | undefined ;
137136
@@ -148,13 +147,14 @@ class CausalReference<T> extends BaseCausalCollection<T> {
148147 if ( this . _latestValidIdx === idx ) {
149148 // the current value has been invalidated, look for the next-best
150149
151- let nextValueIdx = length ;
150+ let nextValueIdx = length - 1 ;
152151
153152 while ( nextValueIdx >= 0 && ! this . isValidOp ( this . _causallyOrderedUpdates [ nextValueIdx ] . opHash ) ) {
154153 nextValueIdx = nextValueIdx - 1 ;
155154 }
156155
157156 if ( nextValueIdx >= 0 ) {
157+
158158 newValueIdx = nextValueIdx ;
159159 newValueOp = await this . loadOp ( this . _causallyOrderedUpdates [ nextValueIdx ] . opHash ) as CausalRefUpdateOp < T > ;
160160 } else {
0 commit comments