RandomSkunk.JSInterop 1.0.0

dotnet add package RandomSkunk.JSInterop --version 1.0.0
NuGet\Install-Package RandomSkunk.JSInterop -Version 1.0.0
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="RandomSkunk.JSInterop" Version="1.0.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add RandomSkunk.JSInterop --version 1.0.0
#r "nuget: RandomSkunk.JSInterop, 1.0.0"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install RandomSkunk.JSInterop as a Cake Addin
#addin nuget:?package=RandomSkunk.JSInterop&version=1.0.0

// Install RandomSkunk.JSInterop as a Cake Tool
#tool nuget:?package=RandomSkunk.JSInterop&version=1.0.0

RandomSkunk.JSInterop

Makes JavaScript interop a little easier with dynamic proxy objects.

Quick Start

Convert an IJSRuntime instance to dynamic with the AsDynamic() extension method, then call JavaScript methods and properties on that object directly. Values returned by functions are also dynamic and are used in the same manner.

using Microsoft.JSInterop;
using RandomSkunk.JSInterop;

async Task MountStripeCardElement(
    IJSRuntime jsRuntime,
    string stripePublicKey,
    string cardElementId = "#card-element")
{
    var js = jsRuntime.AsDynamic();
    var stripe = await js.Stripe(stripePublicKey);
    var elements = await stripe.elements();
    var cardElement = await elements.create("card");
    await cardElement.mount(cardElementId);
}

(Example taken from Stripe's documentation on how to setup a payment form)

Async vs. Sync

The dynamic proxy returned by the IJSRuntime.AsDynamic() extension method is always an async proxy. This means that all calls with it are made asynchronously and must be awaited. Furthermore, values returned by calls made to an async proxy are always async proxy objects, which make calls that must be awaited and themselves return async proxy objects.

To make synchronous calls that don't need to be awaited, you must meet two preconditions: 1) you must know that the JavaScript function is itself synchronous, and 2) the backing IJSRuntime or IJSObjectReference must also implement IJSInProcessRuntime or IJSInProcessObjectReference respectively. If both conditions are met, call the AsSync() method on the async proxy - it method returns a sync version of the async dynamic proxy. All calls with a sync proxy are made synchronously and must not be awaited. To get an async version of a sync proxy, call the AsAsync() method on the sync proxy.

async Task MountStripeCardElement(
    IJSRuntime jsRuntime,
    string stripePublicKey,
    string cardElementId = "#card-element")
{
    // Proxies are async by default:
    var js = jsRuntime.AsDynamic();

    // Calls to async proxies are awaited and return async proxies:
    var stripe = await js.Stripe(stripePublicKey);

    // Getting a sync version of an async proxy and making non-awaited call to it:
    var elements = stripe.AsSync().elements();

    // Calls to sync proxies are not awaited and return sync proxies:
    var cardElement = elements.create("card");

    // Getting an async version of a sync proxy and awaiting a call to it:
    await cardElement.AsAsync().mount(cardElementId);
}

(This is the same example as the Quick Start, but with both sync and async calls.)

Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.0 209 11/5/2022