Skip to content

Commit 231baea

Browse files
committed
treesteps: UpdateLastOpf
Add a convenience function that automatically uses the last op, to avoid having to plumb the operation through layers of functions.
1 parent f3d324f commit 231baea

File tree

2 files changed

+22
-4
lines changed

2 files changed

+22
-4
lines changed

internal/treesteps/tree_steps_off.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,9 @@ func IsRecording(n Node) bool { return false }
3535

3636
type Op struct{}
3737

38-
func StartOpf(node Node, format string, args ...any) *Op { return nil }
39-
func (op *Op) Updatef(format string, args ...any) {}
40-
func (op *Op) Finishf(format string, args ...any) {}
38+
func StartOpf(node Node, format string, args ...any) *Op { return nil }
39+
func (op *Op) Updatef(format string, args ...any) {}
40+
func UpdateLastOpf(node Node, format string, args ...any) {}
41+
func (op *Op) Finishf(format string, args ...any) {}
4142

4243
func TreeToString(n Node) string { return "treesteps not supported in this build" }

internal/treesteps/tree_steps_on.go

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ func StartOpf(node Node, format string, args ...any) *Op {
227227
}
228228

229229
// Updatef changes the state of the operation and emits a step. The state of the
230-
// // operation shows up after the details that were provided in StartOpf.
230+
// operation shows up after the details that were provided in StartOpf.
231231
func (op *Op) Updatef(format string, args ...any) {
232232
if op == nil {
233233
return
@@ -238,6 +238,23 @@ func (op *Op) Updatef(format string, args ...any) {
238238
op.nodeState.recording.stepLockedf("%s on %s updated", firstWord(op.details), firstWord(op.nodeState.name))
239239
}
240240

241+
// UpdateLastOpf calls Updatef on the most recently started operation for this
242+
// node.
243+
func UpdateLastOpf(node Node, format string, args ...any) {
244+
if !mu.recordingInProgress.Load() {
245+
return
246+
}
247+
mu.Lock()
248+
defer mu.Unlock()
249+
state, ok := mu.nodeMap[node]
250+
if !ok || len(state.ops) == 0 {
251+
return
252+
}
253+
op := state.ops[len(state.ops)-1]
254+
op.state = fmt.Sprintf(format, args...)
255+
op.nodeState.recording.stepLockedf("%s on %s updated", firstWord(op.details), firstWord(op.nodeState.name))
256+
}
257+
241258
// Finishf changes the state of the operation, emits a step, and cleans up the
242259
// operation.The state of the operation shows up after the details that were
243260
// provided in StartOpf.

0 commit comments

Comments
 (0)