import { EMPTY , Observable , Subject , catchError , defer , filter , finalize , map , of , shareReplay , tap } from "rxjs" ; import { getElement } from "~/browser" ; import { renderSourceFacts } from "~/templates" ; import { Component } from "../../_" ; import { SourceFacts , fetchSourceFacts } from "../facts" ; /**
 * Repository information
 */ export interface Source { facts : SourceFacts ; } /**
 * Repository information observable
 */ let fetch$ ; param el returns export function watchSource ( el ) { return fetch$ ||= defer ( ( ) => { const cached = __md_get < SourceFacts > ( "__source" , sessionStorage ) ; if ( cached ) return of ( cached ) ; else return fetchSourceFacts ( el . href ) . pipe ( tap ( facts => __md_set ( "__source" , facts , sessionStorage ) ) ) ; } ) . pipe ( catchError ( ( ) => EMPTY ) , filter ( facts => Object . keys ( facts ) . length > 0 ) , map ( facts => ( { facts } ) ) , shareReplay ( 1 ) ) ; } param el returns export function mountSource ( el ) { const inner = getElement ( ":scope > :last-child" , el ) ; return defer ( ( ) => { const push$ = new Subject < Source > ( ) ; push$ . subscribe ( ( { facts } ) => { inner . appendChild ( renderSourceFacts ( facts ) ) ; inner . setAttribute ( "data-md-state" , "done" ) ; } ) ; return watchSource ( el ) . pipe ( tap ( state => push$ . next ( state ) ) , finalize ( ( ) => push$ . complete ( ) ) , map ( state => ( { ref : el , ... state } ) ) ) ; } ) ; }