import { Injectable } from '@angular/core' ; import * as _ from "lodash" ; import { Headers , RequestOptions } from "@angular/http" ; import { MatSnackBar } from "@angular/material" ; @ Injectable ( ) export class UtilsService { constructor ( private snackBar ) { } setTimeoutPromise ( milliseconds ) { const promise = new Promise ( ( resolve , reject ) => { setTimeout ( ( ) => { resolve ( resolve , reject ) ; } , milliseconds ) ; } ) ; return promise ; } isObjectEmpty = ( obj ) => { return ( Object . keys ( obj ) . length === 0 && obj . constructor === Object ) ; } ; debounce ( func , wait , immediate ) { let timeout ; return function ( ) { let context = this , args = arguments ; let later = function ( ) { timeout = null ; if ( ! immediate ) func . apply ( context , args ) ; } ; let callNow = immediate && ! timeout ; clearTimeout ( timeout ) ; timeout = setTimeout ( later , wait ) ; if ( callNow ) func . apply ( context , args ) ; } ; } catchError ( error ) { console . error ( 'An error occurred' , error ) ; return Promise . reject ( error . message || error ) ; } stubHttp ( response ) { return this . setTimeoutPromise ( 100 ) . then ( ( ) => { return response ; } ) ; } defaultSnackBar ( message ) { return this . snackBar . open ( message , 'Dismiss' , { duration : 5000 } ) ; } }