1 import { Injectable } from '@angular/core'
4 export class ScreenService {
5 private windowInnerWidth: number
6 private lastFunctionCallTime: number
7 private cacheForMs = 500
10 this.refreshWindowInnerWidth()
14 return this.getWindowInnerWidth() < 800
18 return this.getWindowInnerWidth() < 500
22 return 'ontouchstart' in window || navigator.msMaxTouchPoints
25 // Cache window inner width, because it's an expensive call
26 private getWindowInnerWidth () {
27 if (this.cacheWindowInnerWidthExpired()) this.refreshWindowInnerWidth()
29 return this.windowInnerWidth
32 private refreshWindowInnerWidth () {
33 this.lastFunctionCallTime = new Date().getTime()
35 this.windowInnerWidth = window.innerWidth
38 private cacheWindowInnerWidthExpired () {
39 if (!this.lastFunctionCallTime) return true
41 return new Date().getTime() > (this.lastFunctionCallTime + this.cacheForMs)