node-swift icon indicating copy to clipboard operation
node-swift copied to clipboard

@MainActor code not executed ?

Open fpirsch opened this issue 1 year ago • 2 comments

Hi,

I managed to get some cool results with this great library. However now I have a problem calling MainActor-isolated APIs (e.g. NSApplication.shared). Calling anything marked as @MainActor is stuck and doesn't get executed.

(vanilla node.js / swift 6)

import NodeAPI

@MainActor func f(a: Double) -> Double {
  print("inside main actor") // <-- ❌ never gets printed
  return a * 2
}

#NodeModule(exports: [
    "f": try NodeFunction { () in
      print("entering") // <-- ✅ printed
      let b = await f(a: 3)
      print("exiting") // <-- ❌ never gets printed
      return b
    }
])

Am I doing things wrong ? I checked #17 about concurrency and actors and #4 about RunLoop & DispatchQueue but couldn't use the answers to solve my problem. Here I absolutely need to switch execution to the MainActor. The final goal is to open a CoreGraphics window.

fpirsch avatar Oct 28 '24 11:10 fpirsch

Hey! Have you found a solution?

thisislvca avatar Mar 11 '25 20:03 thisislvca

This is indeed the same issue as #4 and more recently #51. I provided a non-optimal workaround in https://github.com/kabiroberai/node-swift/issues/51#issuecomment-2989558849 but will try to come up with a better solution when I have some time.

kabiroberai avatar Jun 20 '25 01:06 kabiroberai