import { NgZone } from '@angular/core' ; import { observeOn , startWith , map } from 'rxjs/operators' ; import type { ObservableQuery , ApolloQueryResult , FetchResult , Observable as AObservable , } from '@apollo/client/core' ; import type { Subscription , SchedulerLike , SchedulerAction } from 'rxjs' ; import { Observable , queueScheduler , observable } from 'rxjs' ; import type { MutationResult } from './types' ; export function fromPromise < T > ( promiseFn ) { return new Observable < T > ( ( subscriber ) => { promiseFn ( ) . then ( ( result ) => { if ( ! subscriber . closed ) { subscriber . next ( result ) ; subscriber . complete ( ) ; } } , ( error ) => { if ( ! subscriber . closed ) { subscriber . error ( error ) ; } } ) ; return ( ) => subscriber . unsubscribe ( ) ; } ) ; } export function useMutationLoading < T > ( source , enabled ) { if ( ! enabled ) { return source . pipe ( map < FetchResult < T > , MutationResult < T > > ( ( result ) => ( { ... result , loading : false , } ) ) ) ; } return source . pipe ( startWith < MutationResult < T > > ( { loading : true , } ) , map < MutationResult < T > , MutationResult < T > > ( ( result ) => ( { ... result , loading : ! ! result . loading , } ) ) ) ; } export class ZoneScheduler implements SchedulerLike { constructor ( private zone ) { } public now = Date . now ? Date . now : ( ) => + new Date ( ) ; public schedule < T > ( work , delay = 0 , state ? ) { return this . zone . run ( ( ) => queueScheduler . schedule ( work , delay , state ) ) as Subscription ; } } export function fixObservable < T > ( obs ) ; export function fixObservable < T > ( obs ) ; export function fixObservable < T > ( obs ) { ( obs as any ) [ observable ] = ( ) => obs ; return obs as any ; } export function wrapWithZone < T > ( obs , ngZone ) { return obs . pipe ( observeOn ( new ZoneScheduler ( ngZone ) ) ) ; } export function pickFlag < TFlags , K extends keyof TFlags > ( flags , flag , defaultValue ) { return flags && typeof flags [ flag ] !== 'undefined' ? flags [ flag ] : defaultValue ; }