import * as Y from "yjs" ; import { observeArray } from "./types/array" ; import { observeDoc } from "./types/doc" ; import { observeMap } from "./types/map" ; import { observeText } from "./types/text" ; import { observeXml } from "./types/xml" ; export function isYType ( element ) { return element instanceof Y . AbstractType || Object . prototype . hasOwnProperty . call ( element , "autoLoad" ) ; } export function observeYJS ( element ) { if ( element instanceof Y . XmlText ) { return observeText ( element ) ; } else if ( element instanceof Y . Text ) { return observeText ( element ) ; } else if ( element instanceof Y . Array ) { return observeArray ( element ) ; } else if ( element instanceof Y . Map ) { return observeMap ( element ) ; } else if ( element instanceof Y . Doc || Object . prototype . hasOwnProperty . call ( element , "autoLoad" ) ) { return observeDoc ( element as any as Y . Doc ) ; } else if ( element instanceof Y . XmlFragment ) { return observeXml ( element ) ; } else if ( element instanceof Y . XmlElement ) { return observeXml ( element ) ; } else { if ( element . _item === null && element . _start === null ) { } else { } } return element ; } function makeYDocRootLevelTypesObservable ( doc ) { doc . share . forEach ( ( type ) => { if ( type . constructor !== Y . AbstractType ) { observeYJS ( type ) ; } } ) ; } function makeStructsObservable ( structs , startPos ) { for ( let i = structs . length - 1 ; i >= startPos ; i -- ) { let struct = structs [ i ] ; if ( ! struct . deleted ) { if ( struct instanceof Y . GC ) { continue ; } struct . content ?. getContent ( ) . forEach ( ( content ) => { if ( content instanceof Y . AbstractType ) { observeYJS ( content ) ; } } ) ; } } } const docsObserved = new WeakSet < Y . Doc > ( ) ; export function makeYDocObservable ( doc ) { if ( docsObserved . has ( doc ) ) { return ; } docsObserved . add ( doc ) ; observeYJS ( doc ) ; doc . store . clients . forEach ( ( entry ) => { if ( entry ) { makeStructsObservable ( entry , 0 ) ; } } ) ; makeYDocRootLevelTypesObservable ( doc ) ; doc . on ( "beforeObserverCalls" , ( tr ) => { makeYDocRootLevelTypesObservable ( doc ) ; tr . afterState . forEach ( ( clock , client ) => { const beforeClock = tr . beforeState . get ( client ) || 0 ; if ( beforeClock !== clock ) { const structs = tr . doc . store . clients . get ( client ) ; if ( ! structs ) { return ; } const firstChangePos = Y . findIndexSS ( structs , beforeClock ) ; makeStructsObservable ( structs , firstChangePos ) ; } } ) ; } ) ; } export { enableMobxBindings , enableReactiveBindings , enableVueBindings } from "./observableProvider" ;