-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmulti-steps.js
More file actions
111 lines (94 loc) · 2.6 KB
/
Copy pathmulti-steps.js
File metadata and controls
111 lines (94 loc) · 2.6 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
let algo_queue = []
let current_algo = undefined
let current_algo_generator = undefined
const base_run_frequency = 20
const max_run_frequency = 1000
const min_run_frequency = 2
let run_frequency = base_run_frequency
function queue_algorithm(algo) {
algo_queue.push(algo)
loop()
// console.log(`Algorithm queue ${algo_queue.length}`)
}
function run_current_algorithm() {
if (current_algo == undefined) {
if (algo_queue.length == 0) {
return undefined
noLoop()
}
current_algo = algo_queue[0]
algo_queue.splice(0, 1)
run_frequency = base_run_frequency
// console.log(`Algorithm queue ${algo_queue.length}`)
}
if (current_algo == undefined) {
return undefined
noLoop()
}
if (current_algo_generator == undefined)
current_algo_generator = current_algo.run()
let it = current_algo_generator.next()
if (it.done) {
current_algo = undefined
current_algo_generator = undefined
}
return it.value
}
function run_current_algorithm_at_fps() {
let progress = undefined
const fps = frameRate()
if (fps >= 10 && run_frequency < max_run_frequency)
run_frequency *= 1.5
else if (fps < 3 && run_frequency > min_run_frequency)
run_frequency /= 2
for (let i = 0; i < run_frequency; i++) {
progress = run_current_algorithm()
if (progress == undefined)
break
}
return progress
}
function restart_current_algorithm() {
if (current_algo == undefined)
return
current_algo_generator = undefined
current_algo.restart()
}
function stop_current_algorithm() {
let algo = current_algo
current_algo = undefined
current_algo_generator = undefined
if (algo != undefined)
algo.stop()
}
function stop_all_algorithms() {
algo_queue.length = 0
stop_current_algorithm()
// console.log(`Algorithm queue ${algo_queue.length}`)
}
class MultiStepsAlgo {
constructor() {
this.stopped = false
}
// Run the algorithm, yielding descriptive text of the progress
// at regular intervals.
*run() {
this.stopped = false
}
restart() {
this.stop()
this.stopped = false
}
// Stop what the algorithm is doing.
// Algorithm sub-classes can implement this function to
// to further actions when stopped.
stop() {
this.stopped = true
}
// Called when the algorithm finishes.
// Do necessary cleanup or triggers.
finished() {
current_algo = undefined
current_algo_generator = undefined
}
}