swiftwasm / JavaScriptKit

Swift framework to interact with JavaScript through WebAssembly.
https://swiftpackageindex.com/swiftwasm/JavaScriptKit/main/documentation/javascriptkit
MIT License
664 stars 44 forks source link

`JSClosure.async` doesn't work at all #224

Closed MihaelIsaev closed 1 year ago

MihaelIsaev commented 1 year ago

Hi guys, I'm trying to use JSClosure.async but it never called, while JSClosure works well in the same place. I added a lot of prints under the hood in every call related to JSClosure.async but none of them has been called.

import JavaScriptKit

@main
public struct CartonApp {
    public private(set) var text = "Hello, World!"

    public static func main() async {
        print(CartonApp().text)
        if let docObj = JSObject.global.document.object, let bodyObj = JSObject.global.document.jsValue.body.object {
            var button = JSObject.global.document.createElement.function?.callAsFunction(this: docObj, "button").jsValue
            button?.innerText = "Click me".jsValue
            JSObject.global.document.jsValue.body.appendChild.function?.callAsFunction(this: bodyObj, button)
            if let buttonObj = button?.object {
                button?.addEventListener.function?.callAsFunction(this: buttonObj, "click", JSClosure.async({ args in
                    print("clicked async") // never called
                    return .undefined
                }))
            }
        }
    }
}

Could you please tell me if there is a bug or I'm using it wrong? If I'm using it wrong could you please provide me with any working example that I could easily launch.

Thanks in advance.

@MaxDesiatov @mjburghard @PatrickPijnappel @valeriyvan @yonihemi swiftwasm/swift#5376