import moment from 'moment-timezone' ; import { merge , Subscription } from 'rxjs' ; import { tap } from 'rxjs/operators' ; import { IUiSettingsClient } from '../../ui_settings' ; import { CoreService } from '../../../types' ; interface StartDeps { uiSettings : IUiSettingsClient ; } internal export class MomentService implements CoreService { private uiSettingsSubscription ? : Subscription ; public async setup ( ) { } public async start ( { uiSettings } ) { const setDefaultTimezone = ( tz ) => { const zone = moment . tz . zone ( tz ) ; if ( zone ) moment . tz . setDefault ( zone . name ) ; } ; const setStartDayOfWeek = ( day ) => { const dow = moment . weekdays ( ) . indexOf ( day ) ; moment . updateLocale ( moment . locale ( ) , { week : { dow } } as any ) ; } ; this . uiSettingsSubscription = merge ( uiSettings . get$ ( 'dateFormat:tz' ) . pipe ( tap ( setDefaultTimezone ) ) , uiSettings . get$ ( 'dateFormat:dow' ) . pipe ( tap ( setStartDayOfWeek ) ) ) . subscribe ( ) ; } public async stop ( ) { if ( this . uiSettingsSubscription ) { this . uiSettingsSubscription . unsubscribe ( ) ; this . uiSettingsSubscription = undefined ; } } }