import { BehaviorSubject, Observable, of, Subject } from 'rxjs'; import { debounceTime, mergeMap } from 'rxjs/operators'; export class SectionsSubscriber { private static _refSubject = new BehaviorSubject< { intance: any; key: string }[] >(undefined!); private static _changeRefTrigger = () => SectionsSubscriber._refSubject.asObservable(); private static _changeDetector = new Subject(); private static _changeDetectorTrigger = () => SectionsSubscriber._changeDetector.asObservable(); private static _refresh = new Subject<{ sectionId: string; action: any }>(); private static _refreshTrigger = () => SectionsSubscriber._refresh.asObservable(); private static _elementTextSbj = new Subject(); private static _changehElementText = () => SectionsSubscriber._elementTextSbj.asObservable(); static changeElementText(element: HTMLElement) { SectionsSubscriber._elementTextSbj.next(element); } static onChangeElementText() { return SectionsSubscriber._changehElementText().pipe( mergeMap((ref) => { return of(ref); }) ); } private static _itemSbj = new Subject<{ value: string; element: HTMLElement; }>(); static getItem = (): Observable<{ value: string; element: HTMLElement; }> => SectionsSubscriber._itemSbj.asObservable(); static changeSections(sections: { intance: any; key: string }[]) { SectionsSubscriber._refSubject.next(sections); } static getSections() { return SectionsSubscriber._refSubject.value; } // internal references static detectChange(sectionId: string) { return SectionsSubscriber._changeDetector.next(sectionId); } // internal references static refresh(sectionId: string, action: any = {}) { return SectionsSubscriber._refresh.next({ sectionId, action }); } static change() { return SectionsSubscriber._changeRefTrigger().pipe( mergeMap((components) => { return of(components); }) ); } static onChangeDectect() { return SectionsSubscriber._changeDetectorTrigger() .pipe(debounceTime(500)) .pipe( mergeMap((ref) => { return of(ref); }) ); } // internal references static onRefresh() { return SectionsSubscriber._refreshTrigger().pipe( mergeMap((ref) => { return of(ref); }) ); } static getSectionByKey(sectionKey: string) { if (!sectionKey) return; let currentSection = SectionsSubscriber.getSections(); let section = currentSection.find((section) => section.key === sectionKey); return section; } static setItem(value: string, element: HTMLElement) { return SectionsSubscriber._itemSbj.next({ value, element, }); } }