import { AbstractCrdt , Doc } from "./AbstractCrdt" ; import { SerializedCrdtWithId , CrdtType , SerializedList , SerializedMap , } from "./live" ; import { LiveList } from "./LiveList" ; import { LiveMap } from "./LiveMap" ; import { LiveObject } from "./LiveObject" ; import { LiveRegister } from "./LiveRegister" ; export function remove < T > ( array , item ) { for ( let i = 0 ; i < array . length ; i ++ ) { if ( array [ i ] === item ) { array . splice ( i , 1 ) ; break ; } } } export function isSameNodeOrChildOf ( node , parent ) { if ( node === parent ) { return true ; } if ( node . _parent ) { return isSameNodeOrChildOf ( node . _parent , parent ) ; } return false ; } export function deserialize ( entry , parentToChildren , doc ) { switch ( entry [ 1 ] . type ) { case CrdtType . Object : { return LiveObject . _deserialize ( entry , parentToChildren , doc ) ; } case CrdtType . List : { return LiveList . _deserialize ( entry as [ string , SerializedList ] , parentToChildren , doc ) ; } case CrdtType . Map : { return LiveMap . _deserialize ( entry as [ string , SerializedMap ] , parentToChildren , doc ) ; } case CrdtType . Register : { return LiveRegister . _deserialize ( entry as [ string , SerializedMap ] , parentToChildren , doc ) ; } default : { throw new Error ( "Unexpected CRDT type" ) ; } } } export function isCrdt ( obj ) { return ( obj instanceof LiveObject || obj instanceof LiveMap || obj instanceof LiveList || obj instanceof LiveRegister ) ; } export function selfOrRegisterValue ( obj ) { if ( obj instanceof LiveRegister ) { return obj . data ; } return obj ; } export function selfOrRegister ( obj ) { if ( obj instanceof LiveObject || obj instanceof LiveMap || obj instanceof LiveList ) { return obj ; } else if ( obj instanceof LiveRegister ) { throw new Error ( "Internal error. LiveRegister should not be created from selfOrRegister" ) ; } else { return new LiveRegister ( obj ) ; } }