From 241a2ba77b4348cd61c8eab798b7c2d4ec12cae9 Mon Sep 17 00:00:00 2001 From: David Pine Date: Thu, 19 May 2022 08:42:38 -0500 Subject: [PATCH] Pushing changes from the other night, before closing visual studio. --- .tours/test.tour | 77 +++++++++++++++++++ .../SpeechRecognitionCallbackRegistry.cs | 18 +++-- .../DefaultSpeechRecognitionService.cs | 15 +++- 3 files changed, 101 insertions(+), 9 deletions(-) create mode 100644 .tours/test.tour diff --git a/.tours/test.tour b/.tours/test.tour new file mode 100644 index 0000000..a8ff55a --- /dev/null +++ b/.tours/test.tour @@ -0,0 +1,77 @@ +{ + "$schema": "https://aka.ms/codetour-schema", + "title": "The ILocalStorageService interface", + "steps": [ + { + "file": "src/Blazor.LocalStorage.WebAssembly/ILocalStorageService.cs", + "description": "The `ILocalStorageService` type is exposed in the file-scoped `Microsoft.JSInterop` namespace as a convenience.", + "line": 4, + "selection": { + "start": { + "line": 4, + "character": 1 + }, + "end": { + "line": 4, + "character": 31 + } + } + }, + { + "file": "src/Blazor.LocalStorage.WebAssembly/ILocalStorageService.cs", + "description": "The `JSAutoGenericInterop` attribute is source-generated by the `Blazor.SourceGenerator` project. When you decorate types with this attribute, it will signal to the source-generator to create Blazor JavaSciprt interop for the given `TypeName` and its corresponding `Implementation`.", + "line": 6, + "selection": { + "start": { + "line": 6, + "character": 2 + }, + "end": { + "line": 6, + "character": 22 + } + } + }, + { + "file": "src/Blazor.LocalStorage.WebAssembly/ILocalStorageService.cs", + "description": "The `TypeName` is set to the `\"Storage\"` type, this corresponds directly to the `Storage` interface from the [_lib.dom.d.ts_ at line 14,276](https://github.com/microsoft/TypeScript/blob/main/lib/lib.dom.d.ts#L14276-L14310).", + "line": 7 + }, + { + "file": "src/Blazor.LocalStorage.WebAssembly/ILocalStorageService.cs", + "description": "The `GenericMethodDescriptors` property allows the consumer to specify method descriptors that are used to determine which corresponding generated APIs support generics.", + "line": 10, + "selection": { + "start": { + "line": 10, + "character": 5 + }, + "end": { + "line": 10, + "character": 29 + } + } + }, + { + "file": "src/Blazor.LocalStorage.WebAssembly/ILocalStorageService.cs", + "description": "The `\"getItem\"` API exists on the `Storage` interface. When it's added as a generic method descriptor, we're telling the source-generator we want the return type to be generic.", + "line": 12 + }, + { + "file": "src/Blazor.LocalStorage.WebAssembly/ILocalStorageService.cs", + "description": "The `Implementation` is resolved as the `window.localStorage`. In a browser, open dev tools, and in the **Console** you can type `window.localStorage` to see that this implementation is available.", + "line": 8 + }, + { + "file": "src/Blazor.LocalStorage.WebAssembly/ILocalStorageService.cs", + "description": "The `\"setItem:value\"` method descriptor tells the source-generator to look for a method named `setItem` and when source-generating its parameters, the parameter with a name of `value` should be generic.", + "line": 13 + }, + { + "file": "src/Blazor.LocalStorage.WebAssembly/ILocalStorageService.cs", + "description": "The `interface` is declared as `partial`. This is required as the source-generator will generate the all of the APIs for this interface.", + "line": 15 + } + ], + "ref": "main" +} \ No newline at end of file diff --git a/src/Blazor.SpeechRecognition.WebAssembly/SpeechRecognitionCallbackRegistry.cs b/src/Blazor.SpeechRecognition.WebAssembly/SpeechRecognitionCallbackRegistry.cs index e7f21f2..e46e926 100644 --- a/src/Blazor.SpeechRecognition.WebAssembly/SpeechRecognitionCallbackRegistry.cs +++ b/src/Blazor.SpeechRecognition.WebAssembly/SpeechRecognitionCallbackRegistry.cs @@ -38,7 +38,8 @@ internal void InvokeOnRecognized( string key, string transcript) => OnInvokeCallback( key, _onResultCallbackRegistry, - callback => callback?.Invoke(transcript)); + callback => callback?.Invoke(transcript), + remove: false); internal void InvokeOnError( string key, SpeechRecognitionErrorEvent error) => @@ -59,7 +60,8 @@ internal void InvokeOnEnded(string key) => static void OnInvokeCallback( string key, ConcurrentDictionary callbackRegistry, - Action handleCallback) + Action handleCallback, + bool remove = true) { if (key is null or { Length: 0 } || callbackRegistry is null or { Count: 0 }) @@ -67,10 +69,16 @@ static void OnInvokeCallback( return; } - if (Guid.TryParse(key, out var guid) && - callbackRegistry.TryRemove(guid, out var callback)) + if (Guid.TryParse(key, out var guid)) { - handleCallback?.Invoke(callback); + if (remove && callbackRegistry.TryRemove(guid, out var callback)) + { + handleCallback?.Invoke(callback); + } + else if (!remove && callbackRegistry.TryGetValue(guid, out callback)) + { + handleCallback?.Invoke(callback); + } } } } diff --git a/src/Blazor.SpeechRecognition/DefaultSpeechRecognitionService.cs b/src/Blazor.SpeechRecognition/DefaultSpeechRecognitionService.cs index 18a2ff5..0771e89 100644 --- a/src/Blazor.SpeechRecognition/DefaultSpeechRecognitionService.cs +++ b/src/Blazor.SpeechRecognition/DefaultSpeechRecognitionService.cs @@ -117,7 +117,8 @@ public void OnSpeechRecongizedAsync(string key, string transcript, bool isFinal) static Task OnInvokeCallbackAsync( string key, ConcurrentDictionary callbackRegistry, - Func handleCallback) + Func handleCallback, + bool remove = true) { if (key is null or { Length: 0 }) { @@ -130,10 +131,16 @@ static Task OnInvokeCallbackAsync( } if (handleCallback is not null && - Guid.TryParse(key, out var guid) && - callbackRegistry.TryRemove(guid, out var callback)) + Guid.TryParse(key, out var guid)) { - return handleCallback.Invoke(callback); + if (remove && callbackRegistry.TryRemove(guid, out var callback)) + { + handleCallback?.Invoke(callback); + } + else if (!remove && callbackRegistry.TryGetValue(guid, out callback)) + { + handleCallback?.Invoke(callback); + } } return Task.CompletedTask;