diff options
author | Chocobozzz <me@florianbigard.com> | 2018-06-18 10:04:08 +0200 |
---|---|---|
committer | Chocobozzz <me@florianbigard.com> | 2018-06-18 10:26:20 +0200 |
commit | fc11a44ec9d12af915ac72d8106cb934cfcdcbcd (patch) | |
tree | 8a1c2600f29fd87cfd975aa26b7c5732c4de19d8 /client/src/app | |
parent | 1ee156b2c5e1a7354fb843d99c17761bcc733580 (diff) | |
download | PeerTube-fc11a44ec9d12af915ac72d8106cb934cfcdcbcd.tar.gz PeerTube-fc11a44ec9d12af915ac72d8106cb934cfcdcbcd.tar.zst PeerTube-fc11a44ec9d12af915ac72d8106cb934cfcdcbcd.zip |
Improve screen cache service
Diffstat (limited to 'client/src/app')
-rw-r--r-- | client/src/app/shared/misc/screen.service.ts | 34 |
1 files changed, 24 insertions, 10 deletions
diff --git a/client/src/app/shared/misc/screen.service.ts b/client/src/app/shared/misc/screen.service.ts index 5b17a914a..2e01839b2 100644 --- a/client/src/app/shared/misc/screen.service.ts +++ b/client/src/app/shared/misc/screen.service.ts | |||
@@ -1,23 +1,37 @@ | |||
1 | import { Injectable, NgZone } from '@angular/core' | 1 | import { Injectable } from '@angular/core' |
2 | 2 | ||
3 | @Injectable() | 3 | @Injectable() |
4 | export class ScreenService { | 4 | export class ScreenService { |
5 | private windowInnerWidth: number | 5 | private windowInnerWidth: number |
6 | private lastFunctionCallTime: number | ||
7 | private cacheForMs = 500 | ||
6 | 8 | ||
7 | constructor (private zone: NgZone) { | 9 | constructor () { |
8 | this.windowInnerWidth = window.innerWidth | 10 | this.refreshWindowInnerWidth() |
9 | |||
10 | // Try to cache a little bit window.innerWidth | ||
11 | this.zone.runOutsideAngular(() => { | ||
12 | setInterval(() => this.windowInnerWidth = window.innerWidth, 500) | ||
13 | }) | ||
14 | } | 11 | } |
15 | 12 | ||
16 | isInSmallView () { | 13 | isInSmallView () { |
17 | return this.windowInnerWidth < 600 | 14 | return this.getWindowInnerWidth() < 600 |
18 | } | 15 | } |
19 | 16 | ||
20 | isInMobileView () { | 17 | isInMobileView () { |
21 | return this.windowInnerWidth < 500 | 18 | return this.getWindowInnerWidth() < 500 |
19 | } | ||
20 | |||
21 | // Cache window inner width, because it's an expensive call | ||
22 | private getWindowInnerWidth () { | ||
23 | if (this.cacheWindowInnerWidthExpired()) this.refreshWindowInnerWidth() | ||
24 | |||
25 | return this.windowInnerWidth | ||
26 | } | ||
27 | |||
28 | private refreshWindowInnerWidth () { | ||
29 | this.lastFunctionCallTime = new Date().getTime() | ||
30 | |||
31 | this.windowInnerWidth = window.innerWidth | ||
32 | } | ||
33 | |||
34 | private cacheWindowInnerWidthExpired () { | ||
35 | return new Date().getTime() > (this.lastFunctionCallTime + this.cacheForMs) | ||
22 | } | 36 | } |
23 | } | 37 | } |