Skip to content

Commit

Permalink
Pushing changes from the other night, before closing visual studio.
Browse files Browse the repository at this point in the history
  • Loading branch information
IEvangelist committed May 19, 2022
1 parent 96885c5 commit 241a2ba
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 9 deletions.
77 changes: 77 additions & 0 deletions .tours/test.tour
Original file line number Diff line number Diff line change
@@ -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"
}
Original file line number Diff line number Diff line change
Expand Up @@ -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) =>
Expand All @@ -59,18 +60,25 @@ internal void InvokeOnEnded(string key) =>
static void OnInvokeCallback<T>(
string key,
ConcurrentDictionary<Guid, T> callbackRegistry,
Action<T?> handleCallback)
Action<T?> handleCallback,
bool remove = true)
{
if (key is null or { Length: 0 } ||
callbackRegistry is null or { Count: 0 })
{
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);
}
}
}
}
15 changes: 11 additions & 4 deletions src/Blazor.SpeechRecognition/DefaultSpeechRecognitionService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,8 @@ public void OnSpeechRecongizedAsync(string key, string transcript, bool isFinal)
static Task OnInvokeCallbackAsync<T>(
string key,
ConcurrentDictionary<Guid, T> callbackRegistry,
Func<T, Task> handleCallback)
Func<T, Task> handleCallback,
bool remove = true)
{
if (key is null or { Length: 0 })
{
Expand All @@ -130,10 +131,16 @@ static Task OnInvokeCallbackAsync<T>(
}

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;
Expand Down

0 comments on commit 241a2ba

Please sign in to comment.