import { AATree , OrderStats } from "augmented-aa-tree" ; import * as Char from "./char" ; import * as Identifier from "./identifier" ; import { Crdt , LocalChange } from "./crdt" ; type Position = Identifier . t [ ] ; function comparePosition ( p1 , p2 ) { switch ( Char . comparePosition ( p1 , p2 ) ) { case 0 : return "eq" ; case 1 : return "gt" ; case - 1 : return "lt" ; default : throw Error ( "should be 0, 1 or -1" ) ; } } function newline ( char ) { return char . value === "\n" ? 1 : 0 ; } class LineOrderStats implements OrderStats < Position , Char . t > { constructor ( public lineCount ) { } public of ( key , value , left , right ) { return new LineOrderStats ( newline ( value ) + ( left ? left . lineCount : 0 ) + ( right ? right . lineCount : 0 ) ) ; } } export class TreeCrdt implements Crdt { private crdt : AATree < Position , Char . t > ; public init ( init ) { this . crdt = new AATree < Position , Char . t > ( comparePosition , new LineOrderStats ( 0 ) ) ; } public toString ( ) { return Array . from ( this . crdt . iter ( ) ) . map ( ( [ key , char ] ) => char ) . join ( "" ) ; } public remoteInsert ( char ) { if ( this . crdt . find ( char . position ) ) { return null ; } else { return null ; } } public remoteDelete ( char ) { return null ; } public localInsert ( lamport , site , change ) { return [ ] ; } public localDelete ( change ) { return [ ] ; } }