Replace external functions/commands with JS

Bringing your stacks to the web

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

alexchandel
Posts: 6
Joined: Thu Oct 11, 2018 8:16 pm

Re: Replace external functions/commands with JS

Post by alexchandel » Fri Jan 20, 2023 4:47 am

stam wrote:
Wed Jan 18, 2023 3:14 am
I wasn't asking what javascript method you're trying to replicate in LC. I was asking what the overall intention of what you're doing is - because if you think LiveCode is a good fit for whatever it is you're doing, then maybe there is a better LiveCode way of doing it. If not, then perhaps LC is the wrong tool for the job.
LC has to be the tool, because the app being deployed to HTML5 is a very extensive GUI codebase written in LC and used on desktop.
stam wrote:
Wed Jan 18, 2023 3:14 am
Again, what is it you're trying to do? …

I'm curious what this "IO" is... and why this "IO" cannot be done in LC script?
Several things. One is to use a JavaScript client for large OpenAPI, which is autogenerated from the spec via an OpenAPI generator. There is no LCScript generator for OpenAPI (google "list of openapi generators", I can't post the link), and writing one is a very laborious undertaking. Manually writing LCScript client functions would be laborious and error-prone, and they'd quickly fall outdated (as the APIs are updated regularly updated). So I'd like to call autogenerated ones, necessitating JS.
stam wrote:
Wed Jan 18, 2023 3:14 am
If it really can't, can LC script not take over some of the state you're trying to maintain?
jacque wrote:
Wed Jan 18, 2023 12:15 am
I'm out of my depth here but you can store anything you like in script local or global variables. I'd assume you can store the state of something that way. Someone who knows more about what you're trying to do may have an answer, I hope they'll chime in.
The issue with calling stateful JS code would be that the state comes from JS, either a JS object or a context, which as noted above will not survive stringification. LCScript can only call JS via "do as javascript", which only returns strings. That's a limitation, but I believe I can call this JS code statelessly (for now). The issue that currently blocks me is that the JS calls are asynchronous (they return via a callback or Promise).
stam wrote:
Wed Jan 18, 2023 3:14 am
Promises and await could theoretically be resolved by using script variables and "send in time" or some such.
How could "script variables" or "send in time" synchronize a "do as javascript" that does not return a value, but merely causes a JS callback to be executed in the JS environment some time later? AFAICT they can't.
stam wrote:
Wed Jan 18, 2023 3:14 am
But you have given zero info about what's going on.
Do you want an explicit example? I'm calling a JS function like one of these:

Code: Select all

function returnsImmediately (argFoo, argBar, callbackFunction) { ... }

async function returnsPromise (argFoo, argBar) { ... }
You could call a synchronous JS function that returns a serializable result from LCScript like so:

Code: Select all

do "JSON->stringify(someSyncFunction(" & argFoo & "," & argBar & ))" as "javascript"
put JsonImport(result()) into myVar
But how would you wrap one like returnsImmediately(), which returns nothing, but instead causes a return value to be passed to callbackFunction() later after the "do as javascript" returns and the calling LCScript ends? AFAICT you can't.

SparkOut
Posts: 2839
Joined: Sun Sep 23, 2007 4:58 pm

Re: Replace external functions/commands with JS

Post by SparkOut » Fri Jan 20, 2023 8:30 am

I have a sneaking suspicion that relevant content might have been removed prior to his death, but I know that Hermann did perform successful intercommunication between LC HTML5 and javascript, not just in a handoff, but with callbacks and live exchanges. You *might* find details in one of his threads, forum name [-hh]. It would be worth reading anything of his generally as well, there are some valuable insights there even if specific code might no longer be available (and to be clear, some code still is).

bwmilby
Posts: 438
Joined: Wed Jun 07, 2017 5:37 am
Location: Henrico, VA
Contact:

Re: Replace external functions/commands with JS

Post by bwmilby » Sat Jan 21, 2023 2:10 am

In a desktop app I would suggest a hidden browser widget. It may work for HTML5 too, but I've not tried it. The page loaded in the widget would hold your JS state. You would just need to set the JS handlers that would map to LC handlers. In your async example, the callback could be a JS function if you needed to stringify things or you could probably use livecode.MyLCFunction where MyLCFunction is defined in the script of the browser widget and listed in the JS functions.

Here is a call from a LC button:

Code: Select all

   put "queryTwetch('currentUserId');" into tJS
   do tJS in widget "browser"
Here's the corresponding code in the HTML file:

Code: Select all

		async function queryTwetch(pQuery) {
			const response = await twetch.query(`
				query {
					${pQuery}
				}
			`);
			liveCode.JStoLC(JSON.stringify(response));
		}
Here's the code for the browser widget:

Code: Select all

on JStoLC pData
   put pData & lf after fld "log"
end JStoLC
Brian Milby

Script Tracker https://github.com/bwmilby/scriptTracker

Post Reply

Return to “HTML5”