forked from Blazored/Toast
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Matija Gluhak
committed
Feb 21, 2023
1 parent
f4ec68b
commit 56d020b
Showing
8 changed files
with
136 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,57 +1,72 @@ | ||
namespace Blazored.Toast; | ||
using System.Threading; | ||
|
||
namespace Blazored.Toast; | ||
internal class CountdownTimer : IDisposable | ||
{ | ||
private readonly PeriodicTimer _timer; | ||
private PeriodicTimer _timer; | ||
private readonly int _ticksToTimeout; | ||
private readonly CancellationToken _cancellationToken; | ||
private readonly int _extendedTimeout; | ||
private int _percentComplete; | ||
|
||
private bool _isPaused; | ||
private Func<int, Task>? _tickDelegate; | ||
private Action? _elapsedDelegate; | ||
|
||
internal CountdownTimer(int timeout, CancellationToken cancellationToken = default) | ||
internal CountdownTimer(int timeout, int extendedTimeout = 0, CancellationToken cancellationToken = default) | ||
{ | ||
_ticksToTimeout = 100; | ||
_timer = new PeriodicTimer(TimeSpan.FromMilliseconds(timeout * 10)); | ||
_cancellationToken = cancellationToken; | ||
_extendedTimeout = extendedTimeout; | ||
} | ||
|
||
internal CountdownTimer OnTick(Func<int, Task> updateProgressDelegate) | ||
{ | ||
_tickDelegate = updateProgressDelegate; | ||
return this; | ||
} | ||
|
||
internal CountdownTimer OnElapsed(Action elapsedDelegate) | ||
{ | ||
_elapsedDelegate = elapsedDelegate; | ||
return this; | ||
} | ||
|
||
internal async Task StartAsync() | ||
{ | ||
_percentComplete = 0; | ||
await DoWorkAsync(); | ||
} | ||
|
||
internal void Pause() | ||
{ | ||
_isPaused = true; | ||
} | ||
internal async Task UnPause() | ||
{ | ||
_isPaused = false; | ||
if (_extendedTimeout > 0) | ||
{ | ||
_timer?.Dispose(); | ||
_timer = new PeriodicTimer(TimeSpan.FromMilliseconds(_extendedTimeout * 10)); | ||
await StartAsync(); | ||
} | ||
} | ||
private async Task DoWorkAsync() | ||
{ | ||
while (await _timer.WaitForNextTickAsync(_cancellationToken) && !_cancellationToken.IsCancellationRequested) | ||
{ | ||
_percentComplete++; | ||
|
||
if (!_isPaused) | ||
{ | ||
_percentComplete++; | ||
} | ||
if (_tickDelegate != null) | ||
{ | ||
await _tickDelegate(_percentComplete); | ||
} | ||
//await _tickDelegate?.Invoke(_percentComplete)!; | ||
|
||
if (_percentComplete == _ticksToTimeout) | ||
{ | ||
_elapsedDelegate?.Invoke(); | ||
} | ||
} | ||
} | ||
|
||
public void Dispose() => _timer.Dispose(); | ||
} |