Skip to content

Commit 392b51c

Browse files
committed
Fix Flow.repeat(); add FlowContext.peek()
1 parent d0aec36 commit 392b51c

6 files changed

Lines changed: 39 additions & 19 deletions

File tree

CHANGELOG.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,18 @@ to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
77

88
## [Unreleased]
99

10+
### Added
11+
12+
- Add `FlowContext.peek()` method to retrieve the object at the top of the stack without removing it.
13+
14+
### Changed
15+
16+
- Change `Repeat.DEFAULT_INTERVAL` to 0
17+
18+
### Fixed
19+
20+
- Fix `Flow.repeat()` for tasks that start own promises.
21+
1022
## [2.4.6] - 2026-01-20
1123

1224
### Fixed

flow/src/main/java/org/jboss/elemento/flow/FlowContext.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,18 @@ public <T> void push(T value) {
117117
stack.push(value);
118118
}
119119

120+
/**
121+
* Retrieves the object at the top of the stack without removing it.
122+
*
123+
* @param <T> The type of the object on top of the stack.
124+
* @return The object at the top of the stack.
125+
* @throws EmptyStackException if the stack is empty.
126+
*/
127+
@SuppressWarnings("unchecked")
128+
public <T> T peek() {
129+
return (T) stack.peek();
130+
}
131+
120132
/**
121133
* Removes the object at the top of the stack and returns that object.
122134
*

flow/src/main/java/org/jboss/elemento/flow/Repeat.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,17 +31,17 @@ public interface Repeat<C extends FlowContext> extends Promisable<C>, Subscripti
3131
boolean DEFAULT_FAIL_FAST = true;
3232

3333
/**
34-
* By default, the interval between the iterations is 1 second.
34+
* By default, no interval is used between the iterations.
3535
*/
36-
long DEFAULT_INTERVAL = 1_000;
36+
long DEFAULT_INTERVAL = 0;
3737

3838
/**
3939
* By default, the timeout for the loop is 10 seconds.
4040
*/
4141
long DEFAULT_TIMEOUT = 10_000;
4242

4343
/**
44-
* By default, the number of iterations are infinite.
44+
* By default, the number of iterations is infinite.
4545
*/
4646
int DEFAULT_ITERATIONS = -1;
4747

flow/src/main/java/org/jboss/elemento/flow/RepeatImpl.java

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,14 @@
2121
import elemental2.promise.Promise.PromiseExecutorCallbackFn.RejectCallbackFn;
2222
import elemental2.promise.Promise.PromiseExecutorCallbackFn.ResolveCallbackFn;
2323

24-
import static elemental2.dom.DomGlobal.clearInterval;
2524
import static elemental2.dom.DomGlobal.clearTimeout;
26-
import static elemental2.dom.DomGlobal.setInterval;
2725
import static elemental2.dom.DomGlobal.setTimeout;
2826

2927
class RepeatImpl<C extends FlowContext> extends FlowRunner<C> implements Repeat<C> {
3028

3129
private final Task<C> task;
3230
private Predicate<C> predicate;
31+
private boolean finished;
3332
private boolean failFast;
3433
private long interval;
3534
private long timeout;
@@ -43,6 +42,7 @@ class RepeatImpl<C extends FlowContext> extends FlowRunner<C> implements Repeat<
4342
super(context, 1);
4443
this.task = task;
4544
this.predicate = __ -> true;
45+
this.finished = false;
4646
this.failFast = DEFAULT_FAIL_FAST;
4747
this.interval = DEFAULT_INTERVAL;
4848
this.timeout = DEFAULT_TIMEOUT;
@@ -99,7 +99,7 @@ Promise<C> run() {
9999
}
100100

101101
private void until(ResolveCallbackFn<C> resolve, RejectCallbackFn reject) {
102-
intervalHandle = setInterval(__ -> {
102+
if (!finished) {
103103
if (failFast && lastFailure != null) {
104104
cancel(reject, lastFailure);
105105
} else {
@@ -110,6 +110,11 @@ private void until(ResolveCallbackFn<C> resolve, RejectCallbackFn reject) {
110110
if (areWeDone(c)) {
111111
finish(resolve, c);
112112
}
113+
if (interval != 0) {
114+
intervalHandle = setTimeout(__ -> until(resolve, reject), interval);
115+
} else {
116+
until(resolve, reject);
117+
}
113118
return null;
114119
})
115120
.catch_(error -> {
@@ -120,7 +125,7 @@ private void until(ResolveCallbackFn<C> resolve, RejectCallbackFn reject) {
120125
return null;
121126
});
122127
}
123-
}, interval);
128+
}
124129
}
125130

126131
// ------------------------------------------------------ helper methods
@@ -134,6 +139,7 @@ private boolean areWeDone(C context) {
134139
}
135140

136141
private void finish(ResolveCallbackFn<C> resolve, C context) {
142+
finished = true;
137143
cleanup();
138144
context.progress.finish();
139145
resolve.onInvoke(context);
@@ -145,7 +151,7 @@ private void cancel(RejectCallbackFn reject, String reason) {
145151
}
146152

147153
private void cleanup() {
148-
clearInterval(intervalHandle);
154+
clearTimeout(intervalHandle);
149155
clearTimeout(timeoutHandle);
150156
}
151157
}

samples/flow/package-lock.json

Lines changed: 0 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

samples/flow/src/main/java/org/jboss/elemento/sample/flow/ProgressWrapper.java

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717

1818
import org.jboss.elemento.IsElement;
1919
import org.jboss.elemento.flow.FlowStatus;
20-
import org.patternfly.component.help.HelperText;
2120
import org.patternfly.component.tooltip.Tooltip;
2221

2322
import elemental2.dom.HTMLElement;
@@ -76,10 +75,7 @@ public void finish() {
7675

7776
public void status(FlowStatus status) {
7877
switch (status) {
79-
case NOT_STARTED:
80-
progress.status(info);
81-
break;
82-
case IN_PROGRESS:
78+
case NOT_STARTED, IN_PROGRESS:
8379
progress.status(info);
8480
break;
8581
case SUCCESS:
@@ -96,8 +92,4 @@ public void status(FlowStatus status) {
9692
break;
9793
}
9894
}
99-
100-
public void addHelperText(HelperText helperText) {
101-
progress.addHelperText(helperText);
102-
}
10395
}

0 commit comments

Comments
 (0)