Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Server-side generic interop and support other namespaces #58

Open
rhodon-jargon opened this issue Oct 4, 2024 · 1 comment
Open

Server-side generic interop and support other namespaces #58

rhodon-jargon opened this issue Oct 4, 2024 · 1 comment

Comments

@rhodon-jargon
Copy link

Hi, this looks very nice!

However, I wanted a generic implementation of sessionStorage that also supports server side Blazor. I thought I could easily generate that myself, as follows:

namespace MyApp;

[JSAutoGenericInterop(
    TypeName = "Storage",
    Implementation = "window.sessionStorage",
    HostingModel = BlazorHostingModel.Server,
    OnlyGeneratePureJS = true,
    Url = "https://developer.mozilla.org/docs/Web/API/Window/sessionStorage",
    GenericMethodDescriptors = [
        "getItem",
        "setItem:value"
    ])]
public partial interface ISessionStorageService;

That almost works, except for two things:

  1. The generated implementation, in SessionStorageService.g.cs, doesn't contain a using Microsoft.JSInterop; statement, so it doesn't work in another namespace.
  2. The FromJson extension method doesn't work on ValueTask<string>, so the currently generated code doesn't work.
    (Specifically in the implementation of GetItemAsync:
    ValueTask<TValue?> ISessionStorageService.GetItemAsync<TValue>(string key, JsonSerializerOptions? options)
      where TValue : default => _javaScript.InvokeAsync<string?>("window.sessionStorage.getItem", key).FromJson<TValue>(options);
    )

As an additional note, I'd personally prefer if it wasn't required to add the FromJson and ToJson methods as extension methods to use the generic interop, as those methods now show up in the Intellisense of every object in my project (using Resharper).

Thanks for your help, and your work on this project!

@IEvangelist
Copy link
Owner

Yeah, it's a bit limited right now. You could get this to work by setting the namespace of your interface to Microsoft.JSInterop:

namespace Microsoft.JSInterop;

[JSAutoGenericInterop(
    TypeName = "Storage",
    Implementation = "window.sessionStorage",
    HostingModel = BlazorHostingModel.Server,
    OnlyGeneratePureJS = true,
    Url = "https://developer.mozilla.org/docs/Web/API/Window/sessionStorage",
    GenericMethodDescriptors = [
        "getItem",
        "setItem:value"
    ])]
public partial interface ISessionStorageService;

As for the extension methods, yeah, that's a good callout. I'm still thinking on how to best address that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants