-
-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
A bit more clean up, and fixed a near oops.
- Loading branch information
1 parent
fa1962b
commit 9a443c6
Showing
3 changed files
with
102 additions
and
62 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,10 @@ | ||
namespace Microsoft.JSInterop; | ||
|
||
/// <summary></summary> | ||
[JSAutoInterop( | ||
TypeName = "Permissions", | ||
Implementation = "window.navigator.permissions", | ||
Url = "https://developer.mozilla.org/en-US/docs/Web/API/Navigator/permissions")] | ||
//[JSAutoInterop( | ||
// TypeName = "Permissions", | ||
// Implementation = "window.navigator.permissions", | ||
// Url = "https://developer.mozilla.org/en-US/docs/Web/API/Navigator/permissions")] | ||
public partial interface IPermissionsService | ||
{ | ||
} |
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
84 changes: 84 additions & 0 deletions
84
src/Blazor.SpeechRecognition.WebAssembly/SpeechRecognitionCallbackRegistry.cs
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,84 @@ | ||
// Copyright (c) David Pine. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
namespace Microsoft.JSInterop; | ||
|
||
internal sealed class SpeechRecognitionCallbackRegistry | ||
{ | ||
readonly ConcurrentDictionary<Guid, Action<string>> _onResultCallbackRegistry = new(); | ||
readonly ConcurrentDictionary<Guid, Action<SpeechRecognitionErrorEvent>> _onErrorCallbackRegistry = new(); | ||
readonly ConcurrentDictionary<Guid, Action> _onStartedCallbackRegistry = new(); | ||
readonly ConcurrentDictionary<Guid, Action> _onEndedCallbackRegistry = new(); | ||
|
||
internal void RegisterOnRecognized( | ||
Guid key, Action<string> callback) | ||
{ | ||
if (callback is not null) | ||
_onResultCallbackRegistry[key] = callback; | ||
} | ||
|
||
internal void RegisterOnError( | ||
Guid key, Action<SpeechRecognitionErrorEvent>? callback) | ||
{ | ||
if (callback is not null) | ||
_onErrorCallbackRegistry[key] = callback; | ||
} | ||
|
||
internal void RegisterOnStarted( | ||
Guid key, Action? callback) | ||
{ | ||
if (callback is not null) | ||
_onStartedCallbackRegistry[key] = callback; | ||
} | ||
|
||
internal void RegisterOnEnded( | ||
Guid key, Action? callback) | ||
{ | ||
if (callback is not null) | ||
_onEndedCallbackRegistry[key] = callback; | ||
} | ||
|
||
internal void InvokeOnRecognized( | ||
string key, string transcript) => | ||
OnInvokeCallback( | ||
key, _onResultCallbackRegistry, | ||
callback => callback?.Invoke(transcript)); | ||
|
||
internal void InvokeOnError( | ||
string key, SpeechRecognitionErrorEvent error) => | ||
OnInvokeCallback( | ||
key, _onErrorCallbackRegistry, | ||
callback => callback?.Invoke(error)); | ||
|
||
internal void InvokeOnStarted(string key) => | ||
OnInvokeCallback( | ||
key, _onStartedCallbackRegistry, | ||
callback => callback?.Invoke()); | ||
|
||
internal void InvokeOnEnded(string key) => | ||
OnInvokeCallback( | ||
key, _onEndedCallbackRegistry, | ||
callback => callback?.Invoke()); | ||
|
||
static void OnInvokeCallback<T>( | ||
string key, | ||
ConcurrentDictionary<Guid, T> callbackRegistry, | ||
Action<T?> action) | ||
{ | ||
if (key is null or { Length: 0 }) | ||
{ | ||
return; | ||
} | ||
|
||
if (callbackRegistry is null or { Count: 0 }) | ||
{ | ||
return; | ||
} | ||
|
||
if (Guid.TryParse(key, out var guid) && | ||
callbackRegistry.TryRemove(guid, out var callback)) | ||
{ | ||
action?.Invoke(callback); | ||
} | ||
} | ||
} |