Skip to content

Bimble/EventHorizon.Blazor.TypeScript.Interop.Generator

 
 

Repository files navigation

Build Status codecov EventHorizon.Blazor.TypeScript.Interop.Generator

GitHub GitHub tag (latest SemVer pre-release)

EventHorizon.Blazor.TypeScript.Interop.Generator EventHorizon.Blazor.TypeScript.Interop.Generator.Model EventHorizon.Blazor.TypeScript.Interop.Generator.Writers.Project

EventHorizon.Blazor.TypeScript.Interop.Tool

About EventHorizon Blazor TypeScript Interop Generator

This project generates a C# Blazor Interop proxy using a TypeScript definition file.

Details

The generated project can be used with Blazor WASM to interface with JavaScript from C#, this gives most JavaScript libraries an easy to use interface from C#. It uses the JSRuntime to interop directly with the underlying JavaScript from C#, this is done with a custom interop abstraction.

The interop project can be found in the canhorn/EventHorizon.Blazor.Interop repository, it gives the generated code access to a common set of access patterns it then uses to interface with the JavaScript.

Tech Used

Supported API's Generated

Below is a list of API that will be generated.

API Details Example Support
Constructor You can use the default constructor of Classes, causes new call in JavaScript. new BabylonJS.Engine() ✔️
Constructor with arguments Same as Constructor, but can also pass in arguments. new BabylonJS.Engine("canvas-window-id") ✔️
Property You can get or set properties supplied by the object. var isDisabled = engine.disableManifestCheck and engine.disableManifestCheck = true ✔️
Property You can set properties supplied by created objects. engine.disableManifestCheck = true ✔️
Static Property You can get Static properties of a Class. var isDisabled = Engine.ALPHA_DISABLE ✔️
Method You can call a method supplied by an object. var ratio = engine.getScreenAspectRatio() ✔️
Static Method You can call a Static method provided by a Class. engine.DefaultLoadingScreenFactory(canvas) ✔️
Callback Method You can supply a callback action to a method supplied by an object. observer.add(() => doSomething()) ✔️
Static Callback Method You can call a Static method provided by a Class. Engine.AudioEngineFactory() ✔️
Get Instance Accessor You can have access to the get accessor on an object. var check = engine.disableManifestCheck ✔️
Set Instance Accessor You can use the set accessor on an objects. engine.onCanvasPointerOutObservable.add(() => doSomething()) ✔️
Action Callback You can run Async based code. meshLoader.OnSuccess(new ActionCallback<Mesh>(mesh => { return Task.CompletedTask; })) ✔️
Action Callback in Literal You can run Async based code. new HeightMapMesh({ onReady = new ActionCallback<Mesh>(mesh => { return Task.CompletedTask; }) }) ✔️

Notes on the framework, it might not have the exact API supplied by a TypeScript definition file, in that it might transform the API into something more general and friendly to C#. I used C# as my main source of inspiration for the generated code.

Future Planned Work

  • Promise Result Types -> Ability to async/await Promised response typed Methods.

Example

Checkout /Sample for a BabylonJS working example solution. The solution includes a BabylonJS generated proxy, with a working Blazor WASM site. You can also checkout the website on this repository for deployed website using the generated BabylonJS.

About

This project shows off creating a Blazor Interop C# Generator, has a sample against the BabylonJS library.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • C# 91.4%
  • TypeScript 8.6%