-
Notifications
You must be signed in to change notification settings - Fork 346
/
generator.ts
87 lines (71 loc) 路 1.59 KB
/
generator.ts
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/**
* Copyright (c) 2017 ~ present NAVER Corp.
* billboard.js project is licensed under the MIT license
*/
import {window} from "./browser";
import {isArray} from "./util";
const {setTimeout, clearTimeout} = window;
/**
* Generate resize queue function
* @returns {Fucntion}
* @private
*/
export function generateResize() {
const fn: any[] = [];
let timeout;
const callResizeFn = function() {
// Delay all resize functions call, to prevent unintended excessive call from resize event
callResizeFn.clear();
timeout = setTimeout(() => {
fn.forEach((f: Function) => f());
}, 200);
};
callResizeFn.clear = () => {
if (timeout) {
clearTimeout(timeout);
timeout = null;
}
};
callResizeFn.add = f => fn.push(f);
callResizeFn.remove = f => fn.splice(fn.indexOf(f), 1);
return callResizeFn;
}
/**
* Generate transition queue function
* @returns {Function}
* @private
*/
export function generateWait() {
let transitionsToWait: any = [];
const f = function(t, callback) {
let timer;
// eslint-disable-next-line
function loop() {
let done = 0;
for (let i = 0, t; (t = transitionsToWait[i]); i++) {
if (t === true || (t.empty && t.empty())) {
done++;
continue;
}
try {
t.transition();
} catch (e) {
done++;
}
}
timer && clearTimeout(timer);
if (done === transitionsToWait.length) {
callback && callback();
} else {
timer = setTimeout(loop, 50);
}
}
loop();
};
f.add = function(t) {
isArray(t) ?
(transitionsToWait = transitionsToWait.concat(t)) :
transitionsToWait.push(t);
};
return f;
}