Access Blazor object[] from Javascript without passing data through unmarshalled call - javascript

Just want to highlight that I have been at this for about a week and am new to dealing with the heap and pointers and have not had to interact directly with it before. Could really use some help as I have not made any progress.
Problem statement: Need to access an object[] that I created on Blazor side from Javascript without the use of JSUnmarshalled call from Blazor to pass the data in.
Why? Using the JSUnmarshalled call is still too slow for how large the object[] is even though it only takes .25ms (yes .25ms) to actually create the entire array. The array takes 2 seconds to pass to JS. This is a problem because I am rapidly making adjustments based on user input and most time is spent on waiting for the data to be passed over which causes poor user experience.
What I am currently doing:
c# side
JSUnmarshalledRuntime.InvokeUnmarshalled<object[], object>("InitializeGeometry", RenderModel.GetCombined());
InitializeGeometry calls this JS function to process the object[]
JS side
function getInt32Heap(data) {
var m = data + 12;
var r = Module.HEAP32[m >> 2]
return Array.from(new Int32Array(Module.HEAP32.buffer, m + 4, r));}
This is working fine but is quite slow. What I have tried to find is a way to create a pointer for the object[] on the Blazor side and then pass it into JS. Then on the JS side figure out how to use that pointer to get the same data out that I am currently getting.
I have reached out to the .Net team and this is my original question.
https://github.com/dotnet/aspnetcore/issues/41490
Here is what I have looked through
https://github.com/dotnet/aspnetcore/blob/main/src/Components/Web.JS/src/Rendering/RenderBatch/SharedMemoryRenderBatch.ts
Also found this
https://github.com/SteveSandersonMS/BlazorInputFile/blob/db10236b049ea05de28de9b7806447a3c84c8465/BlazorInputFile/wwwroot/inputfile.js#L113
And this
https://blog.elmah.io/using-js-object-references-in-blazor-wasm-to-wrap-js-libraries/?msclkid=9826568bcf8d11ecbdab91d116441786
Again, I am new to this so the answers may be in here but I am not seeing them. I know this is due to my lack of understanding but so far I have not read anything that puts things into perspective for me. Any help would be greatly appreciated!

Related

JavaScript object has different value in source page than in console

So my application is wrote in java and Spring and I need to pass a hash map with [data,value] from backend to frontend , using THYMELEAF, so I can render them into a graph. These are my lines in my controller
model.addAttribute("hashValues",resultHash.values());
model.addAttribute("hashTime",resultHash.keySet());
When trying to access them in javascript, the hashValues seem to be right, but the time list has a problem. When using console.log to print hashTime my console says something like [1996,1997,1997] when in reality my dates are [2021-10-15, 2021-10-14, 2021-10-13]. I will add some images for a better understanding.
This is how I get the lists from the model.
var time = [[${hashTime}]]
var values = [[${hashValues}]]
console.log(time)
console.log(values)
I've tried using JSON.parse but I have the same problem. Any help is appreciated cause I've been working with JavaScript for some time and I never encountered this type of problem before.

Instantiating a class dynamically from a string

Good afternoon fellow developers,
I am currently trying to develop a function that instantiates business objects dynamically based on the value of the string it receives as a parameter. I know this can be done in JavaScript as I have done it before and, just to be sure, I even tested it again in Visual Studio Code after having encountered this issue in my SAPUI5 app. My function's code looks somewhat like this:
createObject: function (sObject) {
var newObject = new this[sObject]();
// var newObject = new [sObject](); I also tried this way.
};
For the sake of testing this function, the sObject string currently contains the hardcoded value "Order" and I am importing my Order.js object into the file where i'm trying to instantiate this objects dynamically. No matter what I try I keep getting this error when debugging my code:
TypeError: this[sObject] is not a constructor
I was wondering if some of you might have tried something similar before and might be able to point me in the right direction. Even if there are ways for me to work around this issue, it would be really nice if I learnt how to do this dynamically since I was planning on using this approach on several different scenarios. I look forward to reading from you!
It is a really unsafe practice to instantiate objects from a string and an ATROCIOUS one to do it from a parameter a user can supply. If you have a limited set of objects its much safer to have a big switch statement.
switch(name) {
"objA": return new ObjA();
}

Calling COM methods from unreferenced assemblies

I'm migrating an install script from js to vb.net and I'm facing the following problem.
I'm having issues as JS is late-binded so I need to research the datatype returned by each method in order to make the strong typed counterpart in vb.net (or C# for the matter)
In the original js install script, I'm getting to a point where I don't know how to port this 2 calls:
var objWMIService = GetObject("winmgmts:\\\\.\\root\\cimv2");
var objAccount = objWMIService.Get("Win32_SID.SID='S-1-1-0'");
If I leave data type inference, I get COMType, so I can't call the Get method from the generated object.
I think I could solve this by using IDispatch, but It's been like 10 years since I used IDispatch and I can't rememeber exactly how to do it.
Also, after further research, I've seen that the internal COM is actually a SWBemServicesEX object, which I don't know what to reference in order to generate a COM interop system.
I would appreciate it a lot, if anyone can provide any ideas or solutions to this issue.
Current vb.NET code is just:
Dim objWMIService = GetObject("winmgmts:\\.\root\cimv2")
not sure how to be able to call the Get method from objWMIService, and how to determine what this method actually returns!
I ended doing what Steve says in his answer and it solved the issue for me. I'm not very happy with runtime late binding, but it gets the job done!
for reference:
Try using Dim objWMIService as Object = GetObject("winmgmts:\.\root\cimv2") instead. Then you can use Dim objAccount as Object = objWMIService.Get("Win32_SID.SID='S-1-1-0'"). When doing late-binding, declare everything as an object. You can then step through the code in debug and sometimes see what the return is. Then search for the methods/properties/etc for that type and write your next line. Takes a little work but you don't need references to any dlls. Theres just an assumption that the dll are registered (COM) or in your path that .Net follows (CLR)

What is the reason behind the use of Javascript in Wikitude SDK for Android?

I've been comparing several SDKs in order to add some location-based AR in one of my Android Apps. So far I've really liked the overall simplicity of Wikitude, however there are things like them asking you to put some of the logic of the AR part in javascript files that confuse me.
What are you supposed to do with the javascript, what role does it serves in the overall flow between an Android Activity and the AR?
Also, as a side question, what sense is there to make out of this line in the Android documentation of Wikitude:
architectView.callJavascript(newData('" + poiDataAsJson +")')
at http://developer.wikitude.com/documentation/android. I assume there is a typo or mistake somewhere as newData is not defined anywhere in the Android code.
Thank you!
In order to enable cross platform augmented reality experiences, web technologies were chosen. The application flow is usually that the ArchtiectView is initialised and displayed by the native application. An AR experience is loaded which consists of an html file that executes javascript functions to create the objects in AR.
callJavascript method is used natively to communicate with the loaded AR exprience. It executes the passed javascript in the context of the loaded AR experience. Therefore it can be used to pass POI data as json to a function declared in the Javascript part.
There is an error in the posted java code
architectView.callJavascript(newData('" + poiDataAsJson +")')
should include 2 more double quotes
architectView.callJavascript("newData('" + poiDataAsJson +")'")
This executes the function newData declared globally in javascript and passes 1 parameter, the json encoded poi data.
Disclaimer: I work for Wikitude

How to manage ASP.NET parameters and Javascript keys

So here is my scenario.
Im using ASP.NET MVC 3 along with HTML, CSS, JavaScript/JQuery to make a web application.
Im using Visual Studio 2010
We have already released the product (its in 1.0), however now that we are in "maintenance" mode for the project, I have a feeling that as the project has new features added, that it will be harder to maintain the set of constants between both the C# (ASP.NET MVC) and the JavaScript.
For example, in the JavaScript I would create a $.post and have it link to the MVC url Controller/Action and then I would pass in parameters { key1: value1, key2: value2}
The issue is that if the C# parameter names change or if the position of parameters in the signature change, I will only know at run-time that the JavaScript needs to be updated (im assuming that im a programmer that doesn't know the architecture well enough to do this before run time).
So my question is, how do you manage the JavaScript side more easily so that i can stay "in-sync" with changes made on the C# side. Can the compiler do this for me in some way, or is there a plug-in that can help me out?
Thanks.
Your question asks about syncing C# constants and JavaScript constants, but then also talks about parameter names and positions.
The positions of parameters matter less in the MVC world than the names, and I've not found a good way of keep those in sync short of extensive unit and integration testing. You are doing those tests, right? ;)
As far as actual constants and enums, I've taken to using T4 templates to generate both a .cs and a (namespaced) .js file for the constants/enums I need (in my case, out of a database, but could just as easily be anything else).
I can't think of any easy way, but here is something that may help. when I usually develop some website , first of all I try to write as least possible javascript code in views and have them all in .js file, this way you can be sure that you can reuse many codes and since all codes are pure javascript there won't be any problem you mentioned. I also keep the record of all actions with their controller and area name in database and use them for manage permissions and security issues. for your problem you can add all this method to database and later with a piece of code check if this method exist anymore.
adding to DB:(in base controller, so you don't need to do anything manually )
protected override void OnActionExecuting(ActionExecutingContext filterContext)
{
var area = filterContext.RouteData.DataTokens["area"];
string areaName = area != null ? area.ToString() : "";
var controllerName = filterContext.ActionDescriptor.ControllerDescriptor.ControllerName;
string actionName = filterContext.ActionDescriptor.ActionName;
//Add to DB
base.OnActionExecuting(filterContext);
}
check if that exist:
bool exist = false;
try
{
HttpWebRequest request = (HttpWebRequest)System.Net.WebRequest.Create("http://www.example.com/image.jpg");
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
{
exist = response.StatusCode == HttpStatusCode.OK;
}
}
catch
{
}
Your best option is integration tests. You'll be able to test exactly the actions your users would do. Seleno is a good option (it wraps Selenium) for writing integration tests.
It's worth doing. If you have good integration test coverage you'll run into fewer bugs in production.

Categories

Resources