-
Notifications
You must be signed in to change notification settings - Fork 0
/
compradprogvm.ts
193 lines (169 loc) · 4.95 KB
/
compradprogvm.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
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
import {
Observable,
Subscription,
combineLatest,
combineLatestAll,
concat,
filter,
interval,
map,
of,
scan,
shareReplay,
switchMap,
withLatestFrom,
} from 'rxjs'
import { tag } from 'rxjs-spy/operators'
import { ProgVm } from './progvm'
import { butterfly } from 'butterfloat'
export class CompRadProgVm {
// *** Experiment modification fields ***
minBar = 1
maxGrowthPerTick = 90
maxCatchPerTick = 15
catchSpinRate = 2
growthSpinRate = 1
spinTicks = 2
spinRate = 1
// Internal counter during ticks
#spinTickCount = 0
readonly #subscription = new Subscription()
// *** Butterflies and observables ***
readonly #progressAdded: Observable<ProgVm | null>
readonly #addProgress: (progress: ProgVm) => void
get progressAdded() {
return this.#progressAdded
}
readonly #inprogress: ProgVm[] = []
get progressCount() {
return this.#inprogress.length
}
get pausedStatus() {
return combineLatest(this.#inprogress.map((prog) => prog.paused))
}
readonly #targetPercent: Observable<number>
get targetPercent() {
return this.#targetPercent
}
readonly #targetRoundPercent: Observable<string>
get targetRoundPercent() {
return this.#targetRoundPercent
}
readonly #targetVal: Observable<number>
get targetVal() {
return this.#targetVal
}
readonly #currentVal: Observable<number>
get currentVal() {
return this.#currentVal
}
readonly #currentOffset: Observable<number>
get currentOffset() {
return this.#currentOffset
}
constructor(dial?: JQuery<HTMLElement>, ticks?: Observable<unknown>) {
;[this.#progressAdded, this.#addProgress] = butterfly<ProgVm | null>(null)
this.#targetPercent = concat(
of(0),
this.progressAdded.pipe(
switchMap(() => {
return combineLatest(
this.#inprogress.map((progress) => progress.percent),
)
}),
tag('target-percent-progresses'),
map((progresses) =>
progresses.length
? progresses.reduce((a, b) => a + b, 0) / progresses.length
: 0,
),
),
).pipe(tag('target-percent-raw'), shareReplay(1))
this.#targetRoundPercent = this.targetPercent.pipe(
map((target) => target.toLocaleString(undefined, { style: 'percent' })),
)
this.#targetVal = this.targetPercent.pipe(
map((target) => Math.round(target * 360)),
)
const current = concat(
of([0, 0]),
(ticks ?? interval(500)).pipe(
switchMap(() =>
Promise.all(this.#inprogress.map((item) => item.tick())),
),
withLatestFrom(this.targetVal),
scan(
([currentVal, currentOffset], [, targetVal]) =>
this.#onTick(currentVal, currentOffset, targetVal),
[0, 0],
),
),
).pipe(tag('vm-current'), shareReplay(1))
this.#currentVal = current.pipe(map(([currentVal]) => currentVal))
this.#currentOffset = current.pipe(
map(([, currentOffset]) => currentOffset),
)
if (dial) {
this.#subscription.add(
this.currentVal.subscribe((currentVal) =>
dial.val(currentVal).trigger('change'),
),
)
this.#subscription.add(
this.currentOffset.subscribe((currentOffset) =>
dial.trigger('configure', { angleOffset: currentOffset }),
),
)
} else {
console.warn('Unable to subscribe jQuery Knob dial to progress changes')
}
}
pauseAll() {
this.#inprogress.forEach((item) => item.pause())
}
unpauseAll() {
this.#inprogress.forEach((item) => item.unpause())
}
addItem() {
const progress = new ProgVm()
this.#inprogress.unshift(progress)
this.#addProgress(progress)
}
#onTick(
currentVal: number,
currentOffset: number,
targetVal: number,
): [currentVal: number, currentOffset: number] {
// update radial
if (currentVal < this.minBar) {
currentVal = this.minBar
}
if (targetVal > currentVal) {
const diff = Math.min(targetVal - currentVal, this.maxGrowthPerTick)
currentVal += diff
if (this.growthSpinRate) {
currentOffset = (currentOffset + this.growthSpinRate) % 360
}
this.#spinTickCount = 0
} else if (targetVal < currentVal && currentVal > this.minBar) {
const diff = Math.min(currentVal - targetVal, this.maxCatchPerTick)
currentVal = Math.max(currentVal - diff, this.minBar)
const offset = (currentOffset + diff + this.catchSpinRate) % 360
currentOffset = offset
this.#spinTickCount = 0
} else if (currentVal < 360) {
this.#spinTickCount++
if (this.#spinTickCount == this.spinTicks) {
currentOffset = (currentOffset + this.spinRate) % 360
this.#spinTickCount = 0
}
} else if (currentVal == 360 && currentOffset != 0) {
// once complete, reset the offset for the next run
currentOffset = 0
}
return [currentVal, currentOffset]
}
unsubscribe() {
this.#subscription.unsubscribe()
}
}