Skip to content

Commit 1357651

Browse files
Fixed #6 by using a web worker for the loop cycles instead of a plain for(;;) loop. Bug was due to tab hibernation (most browsers throttle inactive tabs' resources to a point where the generated TOTP codes would no longer be correct after a few minutes of inactivity).
1 parent 881caad commit 1357651

7 files changed

Lines changed: 101 additions & 12 deletions

File tree

TFACGUI.BlazorWebApp/Constants.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@ public static class InteropFunctionNames
5353
public const string ALERT_DIALOG = "alert";
5454
public const string CONFIRM_DIALOG = "confirm";
5555
public const string SHOW_TOAST = "ShowToast";
56+
public const string START_LOOP_WORKER = "StartLoopWorker";
57+
public const string STOP_LOOP_WORKER = "StopLoopWorker";
58+
public const string IS_WORKER_AVAILABLE = "WorkerAvailable";
5659
public const string GET_INPUT_VALUE = "GetInputValue";
5760
public const string GET_INPUT_VALUE_RADIO_BOX = "GetRadioBoxValue";
5861
public const string SET_INPUT_VALUE = "SetInputValue";
@@ -68,6 +71,7 @@ public static class InteropFunctionNames
6871
public const string GET_WINDOW_INNER_WIDTH = "GetWindowInnerWidth";
6972
public const string GET_WINDOW_INNER_HEIGHT = "GetWindowInnerHeight";
7073
public const string BLUT_ACTIVE_ELEMENT = "document.activeElement.blur";
74+
7175
}
7276

7377
public static class AesGcm

TFACGUI.BlazorWebApp/Pages/Home.razor

Lines changed: 50 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -572,6 +572,8 @@
572572
private string qrCodeScannerWidth = "90%";
573573

574574
private readonly string manualRegistrationDescTooltip = "Manually registering a 2FA secret is useful for example if your device has no camera to scan the QR code with, or if you want more control over how the entry is going to be labelled inside your TFACGUI chain.\n\nMake sure to enter everything correctly and label everything in an easily recognizable fashion!";
575+
576+
private static event Action? Looped;
575577

576578
protected override async Task OnInitializedAsync()
577579
{
@@ -581,21 +583,32 @@
581583

582584
gridView = await JS.InvokeAsync<string>(Constants.InteropFunctionNames.GET_LOCALSTORAGE_VALUE, Constants.LocalStorageIds.GRID_VIEW, "true") != "false";
583585

584-
for (;;)
586+
bool workerApiSupported = await JS.InvokeAsync<bool>(Constants.InteropFunctionNames.IS_WORKER_AVAILABLE);
587+
588+
if (workerApiSupported)
585589
{
586-
if (changedFocusedState)
587-
{
588-
changedFocusedState = false;
590+
await JS.InvokeVoidAsync(Constants.InteropFunctionNames.START_LOOP_WORKER);
589591

590-
if (focused)
592+
Looped += OnLoopCycle;
593+
}
594+
else
595+
{
596+
for (;;)
597+
{
598+
if (changedFocusedState)
591599
{
592-
userData = await UserDataService.GetUserData();
600+
changedFocusedState = false;
601+
602+
if (focused)
603+
{
604+
userData = await UserDataService.GetUserData();
605+
}
593606
}
594-
}
595607

596-
StateHasChanged();
608+
StateHasChanged();
597609

598-
await Task.Delay(focused ? 128 : 256);
610+
await Task.Delay(focused ? 128 : 256);
611+
}
599612
}
600613
}
601614

@@ -612,6 +625,34 @@
612625
changedFocusedState = true;
613626
}
614627

628+
[JSInvokable]
629+
public static void OnLoopCycleWorkerCallback(string eventArgs)
630+
{
631+
#if DEBUG
632+
Console.WriteLine($"Loop cycle occurred. Event args: {eventArgs}");
633+
#endif
634+
635+
Looped?.Invoke();
636+
}
637+
638+
private void OnLoopCycle()
639+
{
640+
if (changedFocusedState)
641+
{
642+
changedFocusedState = false;
643+
644+
if (focused)
645+
{
646+
Task.Run(async () =>
647+
{
648+
userData = await UserDataService.GetUserData();
649+
});
650+
}
651+
}
652+
653+
StateHasChanged();
654+
}
655+
615656
private async Task ResetManualRegistrationModalForm()
616657
{
617658
await JS.InvokeVoidAsync(Constants.InteropFunctionNames.SET_INPUT_VALUE, Constants.ElementIds.TOTP_CONFIG_ENTRY_NAME, string.Empty);

TFACGUI.BlazorWebApp/TFACGUI.BlazorWebApp.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
<Nullable>enable</Nullable>
66
<ImplicitUsings>enable</ImplicitUsings>
77
<Company>Glitched Polygons</Company>
8-
<AssemblyVersion>1.0.0</AssemblyVersion>
9-
<FileVersion>1.0.0</FileVersion>
8+
<AssemblyVersion>1.0.1</AssemblyVersion>
9+
<FileVersion>1.0.1</FileVersion>
1010
<ApplicationIcon>wwwroot\favicon.ico</ApplicationIcon>
1111
<!-- The following two are necessary until Microsoft fixes https://github.com/dotnet/runtime/issues/90710 -->
1212
<RunAOTCompilation>false</RunAOTCompilation>

TFACGUI.BlazorWebApp/wwwroot/index.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
<script src="js/toaster.js"></script>
3838
<script src="js/window.js"></script>
3939
<script src="js/focus.js"></script>
40+
<script src="js/loop.js"></script>
4041
</body>
4142

4243
</html>

TFACGUI.BlazorWebApp/wwwroot/js/focus.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,5 @@ function IsDocumentHidden()
66

77
document.addEventListener("visibilitychange", () =>
88
{
9-
DotNet.invokeMethodAsync('TFACGUI.BlazorWebApp', 'OnChangeVisibilityState', document.hidden)
9+
DotNet.invokeMethodAsync('TFACGUI.BlazorWebApp', 'OnChangeVisibilityState', document.hidden);
1010
});
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
let loopWorker = undefined;
2+
3+
function WorkerAvailable()
4+
{
5+
return typeof(Worker) !== 'undefined';
6+
}
7+
8+
function StartLoopWorker()
9+
{
10+
if (WorkerAvailable())
11+
{
12+
if (typeof(loopWorker) == 'undefined')
13+
{
14+
loopWorker = new Worker('js/loopworker.js');
15+
}
16+
17+
loopWorker.onmessage = function (event)
18+
{
19+
DotNet.invokeMethodAsync('TFACGUI.BlazorWebApp', 'OnLoopCycleWorkerCallback', '');
20+
};
21+
}
22+
else
23+
{
24+
console.error('Sorry, your browser does not support web workers... Please use a browser that does in order to make full use of TFACGUI!');
25+
}
26+
}
27+
28+
function StopLoopWorker()
29+
{
30+
if (loopWorker)
31+
{
32+
loopWorker.terminate();
33+
loopWorker = undefined;
34+
}
35+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+

2+
function Loop()
3+
{
4+
postMessage('');
5+
setTimeout('Loop()',128);
6+
}
7+
8+
Loop();

0 commit comments

Comments
 (0)