-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprogress-source.js
More file actions
249 lines (215 loc) · 7 KB
/
Copy pathprogress-source.js
File metadata and controls
249 lines (215 loc) · 7 KB
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
import {
TransferController,
} from './simulation.js'
import {
clampSeriesIndex,
} from './series.js'
import { TransferTimerScheduler } from './timer-scheduler.js'
import { TransferSeriesSelection } from './series-selection.js'
const TRANSFER_UI_DEFAULTS = {
totalSize: 256 * 1024 * 1024,
seriesCount: 16,
}
class FakeProgressSource {
constructor(options) {
const opts = options || {}
const {
controller,
simulationOptions,
totalSize,
now,
schedule,
unschedule,
onStart,
onProgress,
onSeriesReplace,
onFinish,
onCancel,
onPauseState,
onControls,
onOutOfBounds,
seriesCount,
seriesAverageActiveIndex,
} = opts
this.simulationOptions = simulationOptions
this.totalSize = totalSize || TRANSFER_UI_DEFAULTS.totalSize
this.now = typeof now === 'function' ? now : Date.now
this.schedule = typeof schedule === 'function' ? schedule : setTimeout
this.unschedule = typeof unschedule === 'function' ? unschedule : clearTimeout
this.transferGraphController = controller || null
this.onStart = typeof onStart === 'function' ? onStart : function () {}
this.onProgress = typeof onProgress === 'function' ? onProgress : function () {}
this.onSeriesReplace = typeof onSeriesReplace === 'function' ? onSeriesReplace : function () {}
this.onFinish = typeof onFinish === 'function' ? onFinish : function () {}
this.onCancel = typeof onCancel === 'function' ? onCancel : function () {}
this.onPauseState = typeof onPauseState === 'function' ? onPauseState : function () {}
this.onControls = typeof onControls === 'function' ? onControls : function () {}
this.onOutOfBounds = typeof onOutOfBounds === 'function' ? onOutOfBounds : function (seriesIndex) {
console.warn('Generated size out of bounds at index ' + seriesIndex)
}
this.seriesSelection = new TransferSeriesSelection({
clampSeriesIndex,
seriesCount: seriesCount || TRANSFER_UI_DEFAULTS.seriesCount || 1,
seriesActiveIndex: seriesAverageActiveIndex || 1,
})
this.seriesCount = this.seriesSelection.seriesCount
this.controller = null
this.scheduler = new TransferTimerScheduler(this.schedule, this.unschedule)
this.notifyControls()
}
getControlsView() {
return this.seriesSelection.getControlsView()
}
notifyControls() {
this.onControls(this.getControlsView())
}
syncStart(event) {
if (this.transferGraphController) {
this.transferGraphController.startTransfer({ totalSize: event.totalSize, nowMs: event.nowMs })
}
this.onStart(event)
}
syncProgress(event) {
if (this.transferGraphController) {
this.transferGraphController.pushProgress(event)
}
this.onProgress(event)
}
syncSeriesReplace(event) {
if (this.transferGraphController) {
this.transferGraphController.replaceRenderedSeries(event)
}
this.onSeriesReplace(event)
}
syncFinish(event) {
if (this.transferGraphController) {
this.transferGraphController.finishTransfer(event)
}
this.onFinish(event)
}
syncCancel(event) {
if (this.transferGraphController) {
this.transferGraphController.cancel()
}
this.onCancel(event)
}
syncPauseState(isPaused) {
if (this.transferGraphController) {
if (isPaused) this.transferGraphController.pause()
else this.transferGraphController.resume()
}
this.onPauseState(isPaused)
}
getActiveTransferredBytes() {
return this.seriesSelection.getActiveTransferredBytes(this.controller)
}
getActiveSeriesPoints() {
return this.seriesSelection.getActiveSeriesPoints(this.controller)
}
clearTimer() {
this.scheduler.clear()
}
scheduleTick() {
if (!this.controller || this.controller.isPaused() || this.controller.isFinished()) return
const frameMs = this.controller.nextFrameMs()
this.scheduler.scheduleOnce(frameMs, () => {
this.tick(frameMs)
})
}
tick(frameMs) {
if (!this.controller) return
const result = this.controller.runStep({ frameMs, nowMs: this.now() })
if (!result.advanced) return
if (result.outOfBoundsIndex) {
this.onOutOfBounds(result.outOfBoundsIndex - 1)
}
this.syncProgress({
transferredBytes: this.getActiveTransferredBytes(),
totalSize: this.controller.getTotalSize(),
elapsedMs: result.elapsedMs,
nowMs: this.now(),
})
if (result.finished) {
this.syncFinish({
transferredBytes: this.getActiveTransferredBytes(),
totalSize: this.controller.getTotalSize(),
elapsedMs: result.elapsedMs,
nowMs: this.now(),
})
return
}
this.scheduleTick()
}
start(mode) {
this.clearTimer()
this.controller = new TransferController({
...this.simulationOptions,
totalSize: this.totalSize,
seriesCount: this.seriesCount,
mode: mode || 'random',
})
this.controller.start(this.now())
this.syncStart({
mode: mode || 'random',
totalSize: this.controller.getTotalSize(),
nowMs: this.now(),
})
this.syncPauseState(false)
this.scheduleTick()
}
setSeriesAverageActiveIndex(nextIndex) {
if (!this.seriesSelection.setActiveIndex(nextIndex)) return
this.notifyControls()
// If fake transfer is active, immediately re-render from selected series.
if (this.controller && this.controller.isStarted()) {
this.syncSeriesReplace({
series: this.getActiveSeriesPoints(),
totalSize: this.controller.getTotalSize(),
elapsedMs: this.controller.getElapsed(this.now()),
finished: this.controller.isFinished(),
nowMs: this.now(),
})
}
}
cancel() {
if (!this.controller) return
this.clearTimer()
this.controller.cancel()
this.syncCancel({ nowMs: this.now() })
}
pause() {
if (!this.controller || this.controller.isFinished() || this.controller.isPaused()) return
this.controller.pause(this.now())
this.clearTimer()
this.syncPauseState(true)
}
resume() {
if (!this.controller || this.controller.isFinished() || !this.controller.isPaused()) return
this.controller.resume(this.now())
this.syncPauseState(false)
this.scheduleTick()
}
togglePause() {
if (!this.controller || this.controller.isFinished()) return false
if (this.controller.isPaused()) {
this.resume()
} else {
this.pause()
}
return this.controller.isPaused()
}
isPaused() {
return this.controller ? this.controller.isPaused() : false
}
isActive() {
return !!this.controller && this.controller.isStarted() && !this.controller.isFinished()
}
}
function createFakeProgressSource(options) {
return new FakeProgressSource(options)
}
export {
TRANSFER_UI_DEFAULTS,
FakeProgressSource,
createFakeProgressSource,
}