kabiroberai / node-swift

Create Node modules in Swift
MIT License
492 stars 16 forks source link

@MainActor code not executed ? #45

Open fpirsch opened 4 weeks ago

fpirsch commented 4 weeks ago

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.