swiftLoader.ts 925 B

1234567891011121314151617181920212223
  1. import type { ComputerUseAPI } from '@ant/computer-use-swift'
  2. let cached: ComputerUseAPI | undefined
  3. /**
  4. * Package's js/index.js reads COMPUTER_USE_SWIFT_NODE_PATH (baked by
  5. * build-with-plugins.ts on darwin targets, unset otherwise — falls through to
  6. * the node_modules prebuilds/ path). We cache the loaded native module.
  7. *
  8. * The four @MainActor methods (captureExcluding, captureRegion,
  9. * apps.listInstalled, resolvePrepareCapture) dispatch to DispatchQueue.main
  10. * and will hang under libuv unless CFRunLoop is pumped — call sites wrap
  11. * these in drainRunLoop().
  12. */
  13. export function requireComputerUseSwift(): ComputerUseAPI {
  14. if (process.platform !== 'darwin') {
  15. throw new Error('@ant/computer-use-swift is macOS-only')
  16. }
  17. // eslint-disable-next-line @typescript-eslint/no-require-imports
  18. return (cached ??= require('@ant/computer-use-swift') as ComputerUseAPI)
  19. }
  20. export type { ComputerUseAPI }