-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathindex.js
186 lines (168 loc) · 3.78 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
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
import Event from 'on-fire';
const win = window;
const sTO = win.setTimeout;
const cTO = win.clearTimeout;
function random(n) {
if (typeof n === 'object') {
const times = n[1] - n[0];
const offset = n[0];
return Math.random() * times + offset;
}
return n;
}
function main(opt = {}) {
const onFire = new Event();
const self = this;
self.on = onFire.on;
self.fire = onFire.fire;
self.handler = onFire.handler;
const {
from, to, speed, delay,
} = opt;
const cfg = {
from: from || 0,
to: to || 100,
speed: speed || 1,
delay: delay || (1000 / 11),
};
self.config = cfg;
const data = {
progress: cfg.from,
next: {
dist: cfg.from,
callback: null,
speed: cfg.speed,
delay: cfg.delay,
status: 1,
},
status: 0,
};
self.data = data;
self.timer = 0;
}
main.prototype = {
from(prg) {
const self = this;
const newPrg = random(prg);
self.config.from = newPrg;
},
progress(prg, cb, spd, dly) {
const self = this;
const cfg = self.config;
const { data } = self;
const newSpd = random(spd);
const newDly = random(dly);
const from = data.progress;
self.timer = sTO(() => {
if (from + newSpd >= prg) {
data.progress = prg;
self.fire('progress', data);
cTO(self.timer);
data.next.status = 1;
if (prg === cfg.to) {
self.fire('completed', data);
}
if (cb) {
cb(data);
}
} else {
data.progress += newSpd;
self.fire('progress', data);
self.progress(prg, cb, spd, dly);
}
}, newDly);
},
add(prg, cb, spd, dly) {
const self = this;
const cfg = self.config;
const { data } = self;
const { next } = data;
const newPrg = random(prg);
if (self.data.status === 1) { // ended
return self;
}
cTO(self.timer);
if (next.status === 0) {
self.fire('passed', data);
next.status = 1;
}
if (next.dist + newPrg > cfg.to) { // 对超出部分裁剪对齐
next.dist = cfg.to;
} else {
next.dist += newPrg;
}
next.callback = cb;
next.speed = spd || cfg.speed;
next.delay = dly || cfg.delay;
next.status = 0;
self.progress(next.dist, next.callback, next.speed, next.delay);
if (self.data.status === 2) { // complete
self.data.status = 1; // 完成后,锁住add方法
}
return self;
},
go(prg, cb, spd, dly) {
const self = this;
const { data } = self;
const newPrg = random(prg);
self.add(newPrg - data.progress, cb, spd, dly);
return self;
},
end(cb) {
const self = this;
self.data.status = 1;
cTO(self.timer);
self.fire('ended', self.data);
if (cb) {
cb(self.data);
}
return self;
},
complete(cb, spd, dly) {
const self = this;
self.data.status = 2;
cTO(self.timer);
self.go(self.config.to, cb, spd, dly);
return self;
},
reset(cb) {
const self = this;
const cfg = self.config;
cTO(self.timer);
self.handler = {};
self.data = {
progress: cfg.from,
next: {
dist: cfg.from,
callback: null,
speed: cfg.speed,
delay: cfg.delay,
status: 1,
},
status: 0,
};
self.timer = 0;
self.fire('reset', self.data);
// self.fire('progress', self.data) // 触发一次progress?
if (cb) {
cb(self.data);
}
return self;
},
destroy(cb) {
const self = this;
cTO(self.timer);
if (cb) {
cb(self.data);
}
self.fire('destroy', self.data); // 这里实际上是先回调,再destroy...
self.config = null;
self.data = null;
self.timer = 0;
self.on = null;
self.fire = null;
self.handler = null;
return self;
},
};
export default main;