Skip to content

Commit

Permalink
- Adding locks around time amount updates for Persistent Timer Overla…
Browse files Browse the repository at this point in the history
…y Widget
  • Loading branch information
Matthew Olivo committed Oct 13, 2024
1 parent f64d9fe commit eba127c
Showing 1 changed file with 31 additions and 16 deletions.
47 changes: 31 additions & 16 deletions MixItUp.Base/Model/Overlay/OverlayPersistentTimerV3Model.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ public class OverlayPersistentTimerV3Model : OverlayEventCountingV3ModelBase
[JsonIgnore]
private bool paused;

[JsonIgnore]
private object amountLock = new object();

public OverlayPersistentTimerV3Model() : base(OverlayItemV3Type.PersistentTimer) { }

public override async Task Initialize()
Expand Down Expand Up @@ -109,18 +112,21 @@ public override async Task ProcessEvent(UserV2ViewModel user, double amount)
if (!this.paused || this.AllowAdjustmentWhilePaused)
{
amount = Math.Round(amount);
if (this.MaxAmount > 0 && amount > 0)
{
int previousAmount = this.CurrentAmount;
this.CurrentAmount = Math.Min(this.CurrentAmount + (int)amount, this.MaxAmount);
amount = this.CurrentAmount - previousAmount;
}
else
lock (amountLock)
{
this.CurrentAmount += (int)amount;
}
if (this.MaxAmount > 0 && amount > 0)
{
int previousAmount = this.CurrentAmount;
this.CurrentAmount = Math.Min(this.CurrentAmount + (int)amount, this.MaxAmount);
amount = this.CurrentAmount - previousAmount;
}
else
{
this.CurrentAmount += (int)amount;
}

this.CurrentAmount = Math.Max(this.CurrentAmount, 1);
this.CurrentAmount = Math.Max(this.CurrentAmount, 1);
}

if (amount != 0)
{
Expand Down Expand Up @@ -178,18 +184,27 @@ private async Task BackgroundTimer(CancellationToken cancellationToken)
{
await Task.Delay(1000);

if (this.CurrentAmount > 0)
bool timerCompleted = false;
lock (amountLock)
{
this.CurrentAmount--;
if (this.CurrentAmount == 0)
if (this.CurrentAmount > 0)
{
await ServiceManager.Get<CommandService>().Queue(this.TimerCompletedCommandID);
if (this.DisableOnCompletion)
this.CurrentAmount--;
if (this.CurrentAmount == 0)
{
await ServiceManager.Get<OverlayV3Service>().GetWidget(this.ID).Disable();
timerCompleted = true;
}
}
}

if (timerCompleted)
{
await ServiceManager.Get<CommandService>().Queue(this.TimerCompletedCommandID);
if (this.DisableOnCompletion)
{
await ServiceManager.Get<OverlayV3Service>().GetWidget(this.ID).Disable();
}
}
}
}
}
Expand Down

0 comments on commit eba127c

Please sign in to comment.