-
Notifications
You must be signed in to change notification settings - Fork 8
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
Showing
26 changed files
with
516 additions
and
324 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
using System.Diagnostics.CodeAnalysis; | ||
using Microsoft.Extensions.Logging; | ||
using Microsoft.JSInterop; | ||
|
||
namespace Toolbelt.Blazor.HotKeys2.Extensions; | ||
|
||
[SuppressMessage("Usage", "CA2254:Template should be a static expression")] | ||
internal static class JS | ||
{ | ||
public static async ValueTask<IJSObjectReference> ImportScriptAsync(this IJSRuntime jsRuntime, ILogger logger) | ||
{ | ||
var scriptPath = "./_content/Toolbelt.Blazor.HotKeys2/script.min.js"; | ||
try | ||
{ | ||
var isOnLine = await jsRuntime.InvokeAsync<bool>("Toolbelt.Blazor.getProperty", "navigator.onLine"); | ||
if (isOnLine) scriptPath += "?v=" + VersionInfo.VersionText; | ||
} | ||
catch (JSException e) { logger.LogError(e, e.Message); } | ||
|
||
return await jsRuntime.InvokeAsync<IJSObjectReference>("import", scriptPath); | ||
} | ||
|
||
public static async ValueTask InvokeSafeAsync(Func<ValueTask> action, ILogger logger) | ||
{ | ||
try { await action(); } | ||
catch (JSDisconnectedException) { } // Ignore this exception because it is thrown when the user navigates to another page. | ||
catch (Exception ex) { logger.LogError(ex, ex.Message); } | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,22 @@ | ||
using Microsoft.Extensions.Logging; | ||
|
||
namespace Toolbelt.Blazor.HotKeys2.Extensions; | ||
|
||
internal static class SemaphoreSlimExtensions | ||
{ | ||
public static async ValueTask<T> InvokeAsync<T>(this SemaphoreSlim semaphore, Func<ValueTask<T>> asyncAction, ILogger? logger = null) | ||
{ | ||
await semaphore.WaitAsync(); | ||
try { return await asyncAction.Invoke(); } | ||
catch (Exception ex) | ||
{ | ||
if (logger != null) | ||
{ | ||
logger.LogError(ex, ex.Message); | ||
return default!; | ||
} | ||
throw; | ||
} | ||
finally { semaphore.Release(); } | ||
} | ||
} |
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
Oops, something went wrong.