-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
50 lines (38 loc) · 1.1 KB
/
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
38
39
40
41
42
43
44
45
46
47
48
49
50
'use strict';
import eventEmitter from 'wolfy87-eventemitter';
export default class Resize extends eventEmitter {
constructor() {
super();
this.onResizeHandle = this.onResize.bind(this);
window.addEventListener('resize', this.onResizeHandle);
window.addEventListener('orientationchange', this.onResizeHandle);
}
onResize() {
if (!this.started) {
this.started = true;
this.times = 0;
this.emitEvent('resize:start');
}
if (this.handle != null) {
this.times = 0;
window.cancelAnimationFrame(this.handle);
}
this.handle = window.requestAnimationFrame(function tick() {
if (++this.times === 10) {
this.handle = null;
this.started = false;
this.times = 0;
this.emitEvent('resize:end');
} else {
this.handle = window.requestAnimationFrame(
tick.bind(this)
);
}
}.bind(this));
}
destroy() {
window.removeEventListener('resize', this.onResizeHandle);
window.removeEventListener('orientationchange', this.onResizeHandle);
this.removeAllListeners();
}
}