forked from darkroomengineering/tempus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
38 lines (28 loc) · 868 Bytes
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
const isClient = typeof window !== 'undefined'
class Tempus {
constructor() {
this.callbacks = []
this.tempusId = 0
this.now = performance.now()
requestAnimationFrame(this.raf)
}
add(callback, priority = 0) {
this.tempusId++
this.callbacks.push({ callback, priority, id: this.tempusId })
this.callbacks.sort((a, b) => a.priority - b.priority)
return () => this.remove(this.tempusId)
}
remove(tempusId) {
this.callbacks = this.callbacks.filter(({ id }) => tempusId !== id)
}
raf = (now) => {
requestAnimationFrame(this.raf)
const deltaTime = now - this.now
this.now = now
for (let i = 0; i < this.callbacks.length; i++) {
this.callbacks[i].callback(now, deltaTime)
}
}
}
if (!isClient) console.warn('Tempus can be used in client side only')
export default isClient && new Tempus()