chiquitinxx / grooscript

Converts your Groovy code to Javascript
https://www.grooscript.org
Other
221 stars 18 forks source link

Async and Await #67

Open cdrchops opened 3 years ago

cdrchops commented 3 years ago

I have a bunch of groovy code I'm converting using grooscript. However, I was wondering if there's a way to add async and await to the scripts in some way. I know I could probably add a metaclass item and try that. I just didn't know if anyone had thought about this as an addition.

Right now, i'm having to edit the converted files directly to add async and await. Which, along these lines I also have fetch to include.

current js file: async function checkWord(word) { var url =/jsonlookup/${word}`;

let response = await fetch(url);

let content;

if (!response.ok) {
    throw new Error(`HTTP error! status: ${response.status}`);
}

content = await response.text();

return content;

}

async function lookupWordInCED(word) { var content = await checkWord(word);

let values = await Promise.all([content]);

if (values.length > 0 && values[0] !== "null") {
    return values;
} else {
    return [];
}

} `

Here's the grooscript code I'm modifying: AjaxCall.checkWord = async function (word) { var url =http://localhost:8080/jsonlookup/${word}`;

let response = await fetch(url);

let content;

if (!response.ok) {
    throw new Error(`HTTP error! status: ${response.status}`);
}

content = await response.text();

return content;

}

AjaxCall.lookupWordInCED = async function(word) { var content = await AjaxCall.checkWord(word);

let values = await Promise.all([content]);

if (values.length > 0 && values[0] !== "null") {
    return gs.list(values);
} else {
    return gs.list([]);
}

}`

obviously, ideally, I'd like to make the javascript calls and promises seamless. I'll read through the groocsript source and see if I can narrow it down some.

Great job and I use this in a lot of my projects.

chiquitinxx commented 3 years ago

Hey! Nice :)

Well, we have support to include javascript code in your groovy classes with @GsNative https://www.grooscript.org/doc.html#_gsnative. But I think you need something like create async functions or work with promises. We don't have this kind of code that is converted to something specific in javascript. Maybe you can create annotations to do that, something like annotate methods with a new annotation ¿@JsAsync? It seems a good feature you can work on :)

cdrchops commented 3 years ago

awesome! I'll check that out. thanks!

cdrchops commented 3 years ago

I was able to make the changes I needed and test.

I built the plugin jar with the reference to the new grooscript jar I made. I then went to include the conversion in my project so I could test against my code and make sure everything was working the way it should be. I'm having issues getting the conversion to load without an error.

Did you do something special to test locally? I've been using Groovy 16 years and 9 years with gradle and this is the first time I've looked at a gradle plugin to include locally in a project. If you have an easier way to test locally I'd appreciate it.

Here are the changes I made locally settings.gradle pluginManagement { repositories { mavenLocal() } }

build.gradle (also note my local version is 1.3.2 so I could make sure i wasn't pulling something accidentally `buildscript { // classpath files('C:\Users\torr\.m2\repository\org\grooscript\gradle-plugin\1.3.2\gradle-plugin-1.3.2.jar') repositories { flatDir name: 'libs', dirs: "./build/libs" }

//"org.grooscript.conversion" version "1.3.2"
dependencies {
    classpath 'org.grooscript:grooscript:1.3.2'
    classpath 'org.grooscript.conversion:gradle-plugin:1.3.2'
}

}`

error I get: `Plugin [id: 'org.grooscript.conversion', version: '1.3.2'] was not found in any of the following sources:

I didn't know if you'd ran into anything like this before

thanks

chiquitinxx commented 3 years ago

Hey! To test locally I can recommend staying with 1.3.1-SNAPSHOT version, snapshot versions are updated always. Be sure you install both grooscript and plugin snapshots to your local repository (./gradlew build or install, I forgot :)). About the problem with the plugin, maybe can be because you are using plugin { ... } in your build.gradle. I suggest you use the other way to add the dependency, (https://github.com/chiquitinxx/grooscript-plugins/tree/master/gradle-plugin):

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'org.grooscript:gradle-plugin:1.3.0'
    }
}

apply plugin: 'org.grooscript.conversion'
cdrchops commented 3 years ago

I'll give that a try. I did publish to local - because I'd made changes to grooscript for the JsAsync work - and so I wanted to update the plugin and run the whole conversion. The code looks good and the conversion seems to be fine. I'll let you know. thanks for the help