import * as Y from "yjs" ; import { Atom , createAtom } from "../observableProvider" ; const xmlsObserved = new WeakSet < Y . XmlFragment > ( ) ; export function observeXml ( value ) { if ( xmlsObserved . has ( value ) ) { return value ; } xmlsObserved . add ( value ) ; let atom ; const handler = ( event ) => { if ( event . changes . added . size || event . changes . deleted . size || event . changes . keys . size || event . changes . delta . length ) { atom ! . reportChanged ( ) ; } } ; atom = createAtom ( "xml" , ( ) => { value . observe ( handler ) ; } , ( ) => { value . unobserve ( handler ) ; } ) ; function patch ( method ) { const originalFunction = value [ method ] ; value [ method ] = function ( ) { atom ! . reportObserved ( this . _implicitObserver ) ; const ret = Reflect . apply ( originalFunction , this , arguments ) ; return ret ; } ; } function patchGetter ( method ) { let target = value ; let descriptor = Object . getOwnPropertyDescriptor ( target , method ) ! ; if ( ! descriptor ) { target = Object . getPrototypeOf ( target ) ; descriptor = Object . getOwnPropertyDescriptor ( target , method ) ! ; } if ( ! descriptor ) { target = Object . getPrototypeOf ( target ) ; descriptor = Object . getOwnPropertyDescriptor ( target , method ) ! ; } if ( ! descriptor ) { throw new Error ( "property not found" ) ; } const originalFunction = descriptor . get ! ; descriptor . get = function ( ) { atom ! . reportObserved ( this . _implicitObserver ) ; const ret = Reflect . apply ( originalFunction , this , arguments ) ; return ret ; } ; Object . defineProperty ( target , method , descriptor ) ; } patch ( "toString" ) ; patch ( "toDOM" ) ; patch ( "toArray" ) ; patch ( "getAttribute" ) ; patchGetter ( "firstChild" ) ; return value ; }