improve iterators
This commit is contained in:
19
src/API.ts
19
src/API.ts
@ -278,7 +278,7 @@ export class UserAPI {
|
|||||||
* @param step - The step value of the iterator
|
* @param step - The step value of the iterator
|
||||||
* @returns The current value of the iterator
|
* @returns The current value of the iterator
|
||||||
*/
|
*/
|
||||||
|
|
||||||
if (!(name in this.iterators)) {
|
if (!(name in this.iterators)) {
|
||||||
// Create new iterator with default step of 1
|
// Create new iterator with default step of 1
|
||||||
this.iterators[name] = {
|
this.iterators[name] = {
|
||||||
@ -287,16 +287,29 @@ export class UserAPI {
|
|||||||
limit
|
limit
|
||||||
};
|
};
|
||||||
} else {
|
} else {
|
||||||
|
// Check if limit has changed
|
||||||
|
if (this.iterators[name].limit !== limit) {
|
||||||
|
// Reset value to 0 and update limit
|
||||||
|
this.iterators[name].value = 0;
|
||||||
|
this.iterators[name].limit = limit;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check if step has changed
|
||||||
|
if (this.iterators[name].step !== step) {
|
||||||
|
// Update step
|
||||||
|
this.iterators[name].step = step ?? this.iterators[name].step;
|
||||||
|
}
|
||||||
|
|
||||||
// Increment existing iterator by step value
|
// Increment existing iterator by step value
|
||||||
this.iterators[name].value += this.iterators[name].step;
|
this.iterators[name].value += this.iterators[name].step;
|
||||||
|
|
||||||
// Check for limit overshoot
|
// Check for limit overshoot
|
||||||
if (this.iterators[name].limit !== undefined &&
|
if (this.iterators[name].limit !== undefined &&
|
||||||
this.iterators[name].value > this.iterators[name].limit) {
|
this.iterators[name].value > this.iterators[name].limit) {
|
||||||
this.iterators[name].value = 0;
|
this.iterators[name].value = 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Return current iterator value
|
// Return current iterator value
|
||||||
return this.iterators[name].value;
|
return this.iterators[name].value;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -705,6 +705,7 @@ function startClock() {
|
|||||||
function startOnEnter(e: KeyboardEvent) {
|
function startOnEnter(e: KeyboardEvent) {
|
||||||
if (e.code === "Enter" || e.code === "Space") startClock();
|
if (e.code === "Enter" || e.code === "Space") startClock();
|
||||||
}
|
}
|
||||||
|
|
||||||
function startOnClick(e: MouseEvent) {
|
function startOnClick(e: MouseEvent) {
|
||||||
if (e.button === 0) startClock();
|
if (e.button === 0) startClock();
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user