Skip to content

Commit 21483ed

Browse files
committed
Merge branch 'static'
2 parents a85d474 + 5e706f5 commit 21483ed

File tree

14 files changed

+162
-0
lines changed

14 files changed

+162
-0
lines changed

package-lock.json

+6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
"core-js": "^3.8.3",
1515
"js-cookie": "^3.0.1",
1616
"locutus": "^2.0.16",
17+
"mitt": "^3.0.1",
1718
"vue": "^2.6.14",
1819
"vue-i18n": "^8.27.2",
1920
"vue-router": "^3.5.1",

src/app/middlewares/fresh.js

+3
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ export class Fresh extends Middleware
1414
await this.restoreFromCache()
1515
await this.restoreFromCookie()
1616
}
17+
else {
18+
this.app.$timer.offAll()
19+
}
1720
next()
1821
}
1922

src/app/providers/bus/index.js

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import {createBus} from '@/app/support/bus'
2+
3+
export const bus = createBus()

src/app/providers/index.js

+6
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ import {i18n, localization} from './settings'
99
import {store} from './store'
1010
import {router} from './router'
1111
import {url} from './url'
12+
import {bus} from './bus'
13+
import {pageVisibility} from './page-visibility'
14+
import {timer} from './timer'
1215

1316
// Should be maintained in order
1417
export const providers = {
@@ -24,4 +27,7 @@ export const providers = {
2427
store,
2528
router,
2629
url,
30+
bus,
31+
pageVisibility,
32+
timer,
2733
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import {createPageVisibility} from '@/app/support/page-visibility'
2+
3+
export const pageVisibility = createPageVisibility()

src/app/providers/timer/index.js

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import {createTimer} from '@/app/support/timer'
2+
3+
export const timer = createTimer()

src/app/support/bus/create-bus.js

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import {registerGlobalPropertyFactory} from '@/app/support/helpers'
2+
import mitt from 'mitt'
3+
4+
export function createBus() {
5+
return {
6+
installer: {
7+
install(Vue) {
8+
registerGlobalPropertyFactory(
9+
Vue,
10+
'$bus',
11+
() => mitt(),
12+
)
13+
},
14+
},
15+
}
16+
}

src/app/support/bus/index.js

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from './create-bus'
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
export function createPageVisibility() {
2+
return {
3+
installer: {
4+
install(Vue) {
5+
document.addEventListener('visibilitychange', () => {
6+
if (document.visibilityState === 'visible') {
7+
Vue.prototype.$bus.emit('pageVisible')
8+
}
9+
})
10+
},
11+
},
12+
}
13+
}
+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from './create-page-visibility'

src/app/support/timer/create-timer.js

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import {registerGlobalPropertyFactory} from '@/app/support/helpers'
2+
import {Timer} from './timer'
3+
4+
export function createTimer() {
5+
return {
6+
installer: {
7+
install(Vue) {
8+
registerGlobalPropertyFactory(
9+
Vue,
10+
'$timer',
11+
() => new Timer(),
12+
)
13+
},
14+
},
15+
}
16+
}

src/app/support/timer/index.js

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from './create-timer'

src/app/support/timer/timer.js

+89
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
export class Timer
2+
{
3+
constructor() {
4+
this.timeouts = {}
5+
this.intervals = {}
6+
this.animations = {}
7+
}
8+
9+
registerTimeout(handler, timeout = null, ...args) {
10+
const id = window.setTimeout(handler, timeout, ...args)
11+
this.timeouts[id] = true
12+
return id
13+
}
14+
15+
offTimeout(id = 0) {
16+
if (id) {
17+
if (id in this.timeouts) {
18+
window.clearTimeout(id)
19+
delete this.timeouts[id]
20+
}
21+
return this
22+
}
23+
return this.offAllTimeouts()
24+
}
25+
26+
offAllTimeouts() {
27+
Object.keys(this.timeouts).forEach(id => {
28+
window.clearTimeout(Number(id))
29+
delete this.timeouts[id]
30+
})
31+
return this
32+
}
33+
34+
registerInterval(handler, timeout = null, ...args) {
35+
const id = window.setInterval(handler, timeout, ...args)
36+
this.intervals[id] = true
37+
return id
38+
}
39+
40+
offInterval(id = 0) {
41+
if (id) {
42+
if (id in this.intervals) {
43+
window.clearInterval(id)
44+
delete this.intervals[id]
45+
}
46+
return this
47+
}
48+
return this.offAllIntervals()
49+
}
50+
51+
offAllIntervals() {
52+
Object.keys(this.intervals).forEach(id => {
53+
window.clearInterval(Number(id))
54+
delete this.intervals[id]
55+
})
56+
return this
57+
}
58+
59+
registerAnimation(handler) {
60+
const id = window.requestAnimationFrame(handler)
61+
this.animations[id] = true
62+
return id
63+
}
64+
65+
offAnimation(id = 0) {
66+
if (id) {
67+
if (id in this.animations) {
68+
window.cancelAnimationFrame(id)
69+
delete this.animations[id]
70+
}
71+
return this
72+
}
73+
return this.offAllTimeouts()
74+
}
75+
76+
offAllAnimations() {
77+
Object.keys(this.animations).forEach(id => {
78+
window.cancelAnimationFrame(Number(id))
79+
delete this.animations[id]
80+
})
81+
return this
82+
}
83+
84+
offAll() {
85+
return this.offAllTimeouts()
86+
.offAllIntervals()
87+
.offAllAnimations()
88+
}
89+
}

0 commit comments

Comments
 (0)