-
Notifications
You must be signed in to change notification settings - Fork 0
Add step-based progress tracking #9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -17,6 +17,16 @@ trait Progressable { | |
| */ | ||
| protected float $progress = 0; | ||
|
|
||
| /** | ||
| * Total steps for the process. | ||
| */ | ||
| protected ?int $totalSteps = null; | ||
|
|
||
| /** | ||
| * Current step of the process. | ||
| */ | ||
| protected int $currentStep = 0; | ||
|
|
||
| /** | ||
| * The callback function for saving cache data. | ||
| * | ||
|
|
@@ -90,6 +100,64 @@ trait Progressable { | |
| */ | ||
| protected mixed $onComplete = null; | ||
|
|
||
| /** | ||
| * Set the total number of steps. | ||
| * | ||
| * @return $this | ||
| */ | ||
| public function setTotalSteps(int $steps): static { | ||
| if ($steps <= 0) { | ||
| throw new \InvalidArgumentException('Total steps must be greater than 0'); | ||
| } | ||
| $this->totalSteps = $steps; | ||
|
|
||
| return $this; | ||
| } | ||
|
|
||
| /** | ||
| * Increment the current step. | ||
| * | ||
| * @return $this | ||
| */ | ||
| public function incrementStep(int $amount = 1): static { | ||
| if ($this->totalSteps === null) { | ||
| throw new \LogicException('Total steps not set. Call setTotalSteps() first.'); | ||
| } | ||
| $this->currentStep += $amount; | ||
| $progress = ($this->currentStep / $this->totalSteps) * 100; | ||
|
|
||
| return $this->setLocalProgress($progress); | ||
| } | ||
|
|
||
| /** | ||
| * Set the current step. | ||
| * | ||
| * @return $this | ||
| */ | ||
| public function setStep(int $step): static { | ||
| if ($this->totalSteps === null) { | ||
| throw new \LogicException('Total steps not set. Call setTotalSteps() first.'); | ||
| } | ||
| $this->currentStep = $step; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Useful? React with 👍 / 👎. |
||
| $progress = ($this->currentStep / $this->totalSteps) * 100; | ||
|
|
||
| return $this->setLocalProgress($progress); | ||
| } | ||
|
|
||
| /** | ||
| * Get the total number of steps. | ||
| */ | ||
| public function getTotalSteps(): ?int { | ||
| return $this->totalSteps; | ||
| } | ||
|
|
||
| /** | ||
| * Get the current step. | ||
| */ | ||
| public function getCurrentStep(): int { | ||
| return $this->currentStep; | ||
| } | ||
|
|
||
| /** | ||
| * Set the callback function for saving cache data. | ||
| * | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Calling
setTotalSteps()updates onlytotalStepsand leavescurrentStepuntouched, so reusing the same instance for a new task carries over the old step counter. In that scenario, the nextincrementStep()computes progress from stale state (often jumping straight to 100%), which makes the new run report incorrect progress data.Useful? React with 👍 / 👎.