import { Injectable , NgZone } from '@angular/core' ; import { asyncScheduler , Observable , Operator , queueScheduler , SchedulerAction , SchedulerLike , Subscriber , Subscription , TeardownLogic } from 'rxjs' ; import { observeOn , subscribeOn , tap } from 'rxjs/operators' ; function noop ( ) { } /**
 * Schedules tasks so that they are invoked inside the Zone that is passed in the constructor.
 */ export class ɵZoneScheduler implements SchedulerLike { constructor ( private zone , private delegate = queueScheduler ) { } now ( ) { return this . delegate . now ( ) ; } schedule ( work , delay ? , state ? ) { const targetZone = this . zone ; const workInZone = function ( this , state ) { targetZone . runGuarded ( ( ) => { work . apply ( this , [ state ] ) ; } ) ; } ; return this . delegate . schedule ( workInZone , delay , state ) ; } } class BlockUntilFirstOperator < T > implements Operator < T , T > { private task : MacroTask | null = null ; constructor ( private zone ) { } call ( subscriber , source ) { const unscheduleTask = this . unscheduleTask . bind ( this ) ; this . task = this . zone . run ( ( ) => Zone . current . scheduleMacroTask ( 'firebaseZoneBlock' , noop , { } , noop , noop ) ) ; return source . pipe ( tap ( { next : unscheduleTask , complete : unscheduleTask , error : unscheduleTask } ) ) . subscribe ( subscriber ) . add ( unscheduleTask ) ; } private unscheduleTask ( ) { setTimeout ( ( ) => { if ( this . task != null && this . task . state === 'scheduled' ) { this . task . invoke ( ) ; this . task = null ; } } , 10 ) ; } } @ Injectable ( { providedIn : 'root' , } ) export class ɵAngularFireSchedulers { public readonly outsideAngular : ɵZoneScheduler ; public readonly insideAngular : ɵZoneScheduler ; constructor ( public ngZone ) { this . outsideAngular = ngZone . runOutsideAngular ( ( ) => new ɵZoneScheduler ( Zone . current ) ) ; this . insideAngular = ngZone . run ( ( ) => new ɵZoneScheduler ( Zone . current , asyncScheduler ) ) ; globalThis . ɵAngularFireScheduler ||= this ; } } function getSchedulers ( ) { const schedulers = globalThis . ɵAngularFireScheduler as ɵAngularFireSchedulers | undefined ; if ( ! schedulers ) { throw new Error ( `Either AngularFireModule has not been provided in your AppModule (this can be done manually or implictly using
provideFirebaseApp) or you're calling an AngularFire method outside of an NgModule (which is not supported).` ) ; } return schedulers ; } function runOutsideAngular < T > ( fn ) { return getSchedulers ( ) . ngZone . runOutsideAngular ( ( ) => fn ( ) ) ; } function run < T > ( fn ) { return getSchedulers ( ) . ngZone . run ( ( ) => fn ( ) ) ; } export function observeOutsideAngular < T > ( obs$ ) { return obs$ . pipe ( observeOn ( getSchedulers ( ) . outsideAngular ) ) ; } export function observeInsideAngular < T > ( obs$ ) { return obs$ . pipe ( observeOn ( getSchedulers ( ) . insideAngular ) ) ; } export function keepUnstableUntilFirst < T > ( obs$ ) { const scheduler = getSchedulers ( ) ; return ɵkeepUnstableUntilFirstFactory ( getSchedulers ( ) ) ( obs$ ) ; } /**
 * Operator to block the zone until the first value has been emitted or the observable
 * has completed/errored. This is used to make sure that universal waits until the first
 * value from firebase but doesn't block the zone forever since the firebase subscription
 * is still alive.
 */ export function ɵkeepUnstableUntilFirstFactory ( schedulers ) { return function keepUnstableUntilFirst < T > ( obs$ ) : Observable < T > { obs$ = obs$ . lift ( new BlockUntilFirstOperator ( schedulers . ngZone ) ) ; return obs$ . pipe ( subscribeOn ( schedulers . outsideAngular ) , observeOn ( schedulers . insideAngular ) ) ; } ; } const zoneWrapFn = ( it , macrotask ) => { const _this = this ; return function ( ) { if ( macrotask ) { setTimeout ( ( ) => { if ( macrotask . state === 'scheduled' ) { macrotask . invoke ( ) ; } } , 10 ) ; } return run ( ( ) => it . apply ( _this , arguments ) ) ; } ; } ; export const ɵzoneWrap = < T = unknown > ( it , blockUntilFirst ) : T => { return function ( ) { let macrotask ; for ( let i = 0 ; i < arguments . length ; i ++ ) { if ( typeof arguments [ i ] === 'function' ) { if ( blockUntilFirst ) { macrotask ||= run ( ( ) => Zone . current . scheduleMacroTask ( 'firebaseZoneBlock' , noop , { } , noop , noop ) ) ; } arguments [ i ] = zoneWrapFn ( arguments [ i ] , macrotask ) ; } } const ret = runOutsideAngular ( ( ) => ( it as any ) . apply ( this , arguments ) ) ; if ( ! blockUntilFirst ) { if ( ret instanceof Observable ) { const schedulers = getSchedulers ( ) ; return ret . pipe ( subscribeOn ( schedulers . outsideAngular ) , observeOn ( schedulers . insideAngular ) ) ; } else { return run ( ( ) => ret ) ; } } if ( ret instanceof Observable ) { return ret . pipe ( keepUnstableUntilFirst ) as any ; } else if ( ret instanceof Promise ) { return run ( ( ) => new Promise ( ( resolve , reject ) => ret . then ( it => run ( ( ) => resolve ( it ) ) , reason => run ( ( ) => reject ( reason ) ) ) ) ) ; } else if ( typeof ret === 'function' && macrotask ) { return function ( ) { setTimeout ( ( ) => { if ( macrotask && macrotask . state === 'scheduled' ) { macrotask . invoke ( ) ; } } , 10 ) ; return ret . apply ( this , arguments ) ; } ; } else { return run ( ( ) => ret ) ; } } as any ; } ;