@ng-native/desktop 0.0.0-stage → 0.23.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (393) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +51 -2
  3. package/app.plugin.cjs +17 -0
  4. package/bin/app-config.mjs +168 -0
  5. package/bin/autolink.mjs +224 -0
  6. package/bin/cli.mjs +158 -0
  7. package/bin/host.mjs +427 -0
  8. package/bin/ng-native-desktop.mjs +4 -0
  9. package/bin/package.mjs +971 -0
  10. package/bin/plist.mjs +68 -0
  11. package/bin/prebuilt.mjs +328 -0
  12. package/bin/toolchain.mjs +192 -0
  13. package/native/CMakeLists.txt +276 -0
  14. package/native/appkit/CMakeLists.txt +109 -0
  15. package/native/appkit/address-search.h +26 -0
  16. package/native/appkit/address-search.mm +277 -0
  17. package/native/appkit/animation-frames.h +14 -0
  18. package/native/appkit/animation-frames.mm +106 -0
  19. package/native/appkit/app-info.h +12 -0
  20. package/native/appkit/app-info.mm +98 -0
  21. package/native/appkit/app-updater.mm +128 -0
  22. package/native/appkit/app-windows.h +81 -0
  23. package/native/appkit/app-windows.mm +318 -0
  24. package/native/appkit/appkit-backend.h +73 -0
  25. package/native/appkit/appkit-backend.mm +276 -0
  26. package/native/appkit/apple-authentication.h +19 -0
  27. package/native/appkit/apple-authentication.mm +248 -0
  28. package/native/appkit/apple-button-view.h +10 -0
  29. package/native/appkit/apple-button-view.mm +37 -0
  30. package/native/appkit/apple-id-button.h +16 -0
  31. package/native/appkit/apple-id-button.mm +50 -0
  32. package/native/appkit/audio.h +16 -0
  33. package/native/appkit/audio.mm +275 -0
  34. package/native/appkit/battery.h +13 -0
  35. package/native/appkit/battery.mm +92 -0
  36. package/native/appkit/bookmarks.h +17 -0
  37. package/native/appkit/bookmarks.mm +145 -0
  38. package/native/appkit/calendar.h +25 -0
  39. package/native/appkit/calendar.mm +660 -0
  40. package/native/appkit/camera-view.h +15 -0
  41. package/native/appkit/camera-view.mm +130 -0
  42. package/native/appkit/camera.h +39 -0
  43. package/native/appkit/camera.mm +362 -0
  44. package/native/appkit/cg-canvas.h +17 -0
  45. package/native/appkit/cg-canvas.mm +518 -0
  46. package/native/appkit/component-view.h +17 -0
  47. package/native/appkit/crypto.h +19 -0
  48. package/native/appkit/crypto.mm +78 -0
  49. package/native/appkit/desktop-expo.h +29 -0
  50. package/native/appkit/desktop-expo.mm +385 -0
  51. package/native/appkit/expo-image-drawing.h +46 -0
  52. package/native/appkit/expo-image-drawing.mm +131 -0
  53. package/native/appkit/expo-image-view.h +15 -0
  54. package/native/appkit/expo-image-view.mm +182 -0
  55. package/native/appkit/expo-ui-renderer.h +68 -0
  56. package/native/appkit/expo-ui-renderer.mm +101 -0
  57. package/native/appkit/expo-ui-view.h +26 -0
  58. package/native/appkit/expo-ui-view.mm +134 -0
  59. package/native/appkit/expo-ui.swift +1284 -0
  60. package/native/appkit/file-system.h +16 -0
  61. package/native/appkit/file-system.mm +140 -0
  62. package/native/appkit/fonts.h +12 -0
  63. package/native/appkit/fonts.mm +48 -0
  64. package/native/appkit/geocoder.h +19 -0
  65. package/native/appkit/geocoder.mm +101 -0
  66. package/native/appkit/glass-effect.h +22 -0
  67. package/native/appkit/glass-effect.mm +63 -0
  68. package/native/appkit/glass-view.h +8 -0
  69. package/native/appkit/glass-view.mm +65 -0
  70. package/native/appkit/header-toolbar.h +60 -0
  71. package/native/appkit/header-toolbar.mm +629 -0
  72. package/native/appkit/host-view.h +54 -0
  73. package/native/appkit/host-view.mm +1200 -0
  74. package/native/appkit/image-manipulator.h +14 -0
  75. package/native/appkit/image-manipulator.mm +191 -0
  76. package/native/appkit/image-request.h +22 -0
  77. package/native/appkit/image-request.mm +62 -0
  78. package/native/appkit/image-view.h +8 -0
  79. package/native/appkit/image-view.mm +142 -0
  80. package/native/appkit/input-replay.h +25 -0
  81. package/native/appkit/input-replay.mm +418 -0
  82. package/native/appkit/language-model.h +14 -0
  83. package/native/appkit/language-model.mm +99 -0
  84. package/native/appkit/language-model.swift +472 -0
  85. package/native/appkit/local-authentication.h +15 -0
  86. package/native/appkit/local-authentication.mm +133 -0
  87. package/native/appkit/location.h +16 -0
  88. package/native/appkit/location.mm +214 -0
  89. package/native/appkit/main.mm +1515 -0
  90. package/native/appkit/map-view.h +15 -0
  91. package/native/appkit/map-view.mm +109 -0
  92. package/native/appkit/maps.h +35 -0
  93. package/native/appkit/maps.mm +343 -0
  94. package/native/appkit/menu-bar.h +22 -0
  95. package/native/appkit/menu-bar.mm +208 -0
  96. package/native/appkit/modal-focus.h +16 -0
  97. package/native/appkit/modal-focus.mm +26 -0
  98. package/native/appkit/modal-host-view.h +79 -0
  99. package/native/appkit/modal-host-view.mm +442 -0
  100. package/native/appkit/native-controls.h +22 -0
  101. package/native/appkit/native-controls.mm +431 -0
  102. package/native/appkit/network-state.h +12 -0
  103. package/native/appkit/network-state.mm +64 -0
  104. package/native/appkit/network.h +14 -0
  105. package/native/appkit/network.mm +396 -0
  106. package/native/appkit/notifications.h +19 -0
  107. package/native/appkit/notifications.mm +309 -0
  108. package/native/appkit/photo-library.h +33 -0
  109. package/native/appkit/photo-library.mm +548 -0
  110. package/native/appkit/pointer-buttons.h +32 -0
  111. package/native/appkit/presentation-label.h +15 -0
  112. package/native/appkit/presentation-label.mm +12 -0
  113. package/native/appkit/presented-screen.h +19 -0
  114. package/native/appkit/presented-screen.mm +181 -0
  115. package/native/appkit/printing.h +42 -0
  116. package/native/appkit/printing.mm +393 -0
  117. package/native/appkit/push.h +19 -0
  118. package/native/appkit/push.mm +81 -0
  119. package/native/appkit/red-box.h +26 -0
  120. package/native/appkit/red-box.mm +387 -0
  121. package/native/appkit/screen-capture.h +21 -0
  122. package/native/appkit/screen-capture.mm +92 -0
  123. package/native/appkit/scroll-view.h +13 -0
  124. package/native/appkit/scroll-view.mm +251 -0
  125. package/native/appkit/scrollers.h +25 -0
  126. package/native/appkit/scrollers.mm +43 -0
  127. package/native/appkit/search-field.h +34 -0
  128. package/native/appkit/search-field.mm +291 -0
  129. package/native/appkit/search-toolbar-item.h +19 -0
  130. package/native/appkit/search-toolbar-item.mm +269 -0
  131. package/native/appkit/share-picker.h +34 -0
  132. package/native/appkit/share-picker.mm +121 -0
  133. package/native/appkit/split-view.h +77 -0
  134. package/native/appkit/split-view.mm +1206 -0
  135. package/native/appkit/store-review.h +13 -0
  136. package/native/appkit/store-review.mm +28 -0
  137. package/native/appkit/surface-view.h +45 -0
  138. package/native/appkit/surface-view.mm +558 -0
  139. package/native/appkit/svg-view.h +17 -0
  140. package/native/appkit/svg-view.mm +57 -0
  141. package/native/appkit/symbol-drawing.h +24 -0
  142. package/native/appkit/symbol-drawing.mm +117 -0
  143. package/native/appkit/symbol-view.h +8 -0
  144. package/native/appkit/symbol-view.mm +33 -0
  145. package/native/appkit/system-access.h +34 -0
  146. package/native/appkit/system-access.mm +210 -0
  147. package/native/appkit/system.h +24 -0
  148. package/native/appkit/system.mm +403 -0
  149. package/native/appkit/test-launch.h +36 -0
  150. package/native/appkit/test-launch.mm +115 -0
  151. package/native/appkit/text-input-field.h +76 -0
  152. package/native/appkit/text-input-field.mm +444 -0
  153. package/native/appkit/text-layout.h +58 -0
  154. package/native/appkit/text-layout.mm +477 -0
  155. package/native/appkit/text-recognition.h +13 -0
  156. package/native/appkit/text-recognition.mm +102 -0
  157. package/native/appkit/tracking.h +13 -0
  158. package/native/appkit/tracking.mm +51 -0
  159. package/native/appkit/tray-and-dock.h +45 -0
  160. package/native/appkit/tray-and-dock.mm +130 -0
  161. package/native/appkit/undo.h +23 -0
  162. package/native/appkit/undo.mm +125 -0
  163. package/native/appkit/updates.h +17 -0
  164. package/native/appkit/updates.mm +71 -0
  165. package/native/appkit/video-view.h +10 -0
  166. package/native/appkit/video-view.mm +42 -0
  167. package/native/appkit/video.h +38 -0
  168. package/native/appkit/video.mm +404 -0
  169. package/native/appkit/web-browser.h +12 -0
  170. package/native/appkit/web-browser.mm +97 -0
  171. package/native/appkit/web-view-component.h +9 -0
  172. package/native/appkit/web-view-component.mm +56 -0
  173. package/native/appkit/web-view.h +31 -0
  174. package/native/appkit/web-view.mm +394 -0
  175. package/native/cmake/libraries/react-native-reanimated.cmake +45 -0
  176. package/native/cmake/libraries/react-native-worklets.cmake +46 -0
  177. package/native/cmake/libraries.cmake +109 -0
  178. package/native/cmake/linked-modules.cmake +112 -0
  179. package/native/cmake/react-native.cmake +175 -0
  180. package/native/cmake/runtime-dependencies.cmake +34 -0
  181. package/native/cmake/swift.cmake +43 -0
  182. package/native/cmake/third-party.cmake +74 -0
  183. package/native/core/include/ngn/absent-views.h +27 -0
  184. package/native/core/include/ngn/accessibility.h +140 -0
  185. package/native/core/include/ngn/address-search.h +69 -0
  186. package/native/core/include/ngn/animation-frames.h +39 -0
  187. package/native/core/include/ngn/app-info.h +52 -0
  188. package/native/core/include/ngn/app-menus.h +146 -0
  189. package/native/core/include/ngn/app-windows.h +181 -0
  190. package/native/core/include/ngn/apple-authentication.h +116 -0
  191. package/native/core/include/ngn/asset.h +28 -0
  192. package/native/core/include/ngn/audio.h +125 -0
  193. package/native/core/include/ngn/back-swipe.h +112 -0
  194. package/native/core/include/ngn/battery.h +48 -0
  195. package/native/core/include/ngn/bytes.h +59 -0
  196. package/native/core/include/ngn/calendar.h +180 -0
  197. package/native/core/include/ngn/camera.h +128 -0
  198. package/native/core/include/ngn/control-sizes.h +111 -0
  199. package/native/core/include/ngn/core-modules.h +105 -0
  200. package/native/core/include/ngn/crypto.h +38 -0
  201. package/native/core/include/ngn/default-menu.h +91 -0
  202. package/native/core/include/ngn/desktop-expo.h +193 -0
  203. package/native/core/include/ngn/desktop-modal.h +109 -0
  204. package/native/core/include/ngn/desktop-props.h +75 -0
  205. package/native/core/include/ngn/desktop-view-props.h +110 -0
  206. package/native/core/include/ngn/dev-server.h +33 -0
  207. package/native/core/include/ngn/download.h +24 -0
  208. package/native/core/include/ngn/expo-image.h +192 -0
  209. package/native/core/include/ngn/expo-ui.h +133 -0
  210. package/native/core/include/ngn/file-system.h +274 -0
  211. package/native/core/include/ngn/focus-order.h +63 -0
  212. package/native/core/include/ngn/fonts.h +64 -0
  213. package/native/core/include/ngn/gestures.h +305 -0
  214. package/native/core/include/ngn/glass.h +67 -0
  215. package/native/core/include/ngn/header-bar.h +112 -0
  216. package/native/core/include/ngn/host-state.h +243 -0
  217. package/native/core/include/ngn/image-manipulator.h +70 -0
  218. package/native/core/include/ngn/input-dispatcher.h +211 -0
  219. package/native/core/include/ngn/input-script.h +56 -0
  220. package/native/core/include/ngn/input.h +112 -0
  221. package/native/core/include/ngn/key-value-store.h +41 -0
  222. package/native/core/include/ngn/language-model.h +69 -0
  223. package/native/core/include/ngn/launch-frame.h +47 -0
  224. package/native/core/include/ngn/launch-settings.h +98 -0
  225. package/native/core/include/ngn/layout-animations.h +24 -0
  226. package/native/core/include/ngn/library-components.h +13 -0
  227. package/native/core/include/ngn/library-glue.h +53 -0
  228. package/native/core/include/ngn/live-regions.h +54 -0
  229. package/native/core/include/ngn/local-authentication.h +74 -0
  230. package/native/core/include/ngn/location.h +89 -0
  231. package/native/core/include/ngn/maps.h +226 -0
  232. package/native/core/include/ngn/module-registry.h +51 -0
  233. package/native/core/include/ngn/mounting-backend.h +96 -0
  234. package/native/core/include/ngn/mounting-manager.h +155 -0
  235. package/native/core/include/ngn/native-controls.h +161 -0
  236. package/native/core/include/ngn/native-module.h +109 -0
  237. package/native/core/include/ngn/network-state.h +44 -0
  238. package/native/core/include/ngn/network.h +100 -0
  239. package/native/core/include/ngn/notifications.h +185 -0
  240. package/native/core/include/ngn/photo-library.h +130 -0
  241. package/native/core/include/ngn/printing.h +69 -0
  242. package/native/core/include/ngn/processes.h +114 -0
  243. package/native/core/include/ngn/red-box.h +119 -0
  244. package/native/core/include/ngn/reload-thread.h +258 -0
  245. package/native/core/include/ngn/runtime.h +161 -0
  246. package/native/core/include/ngn/safe-areas.h +77 -0
  247. package/native/core/include/ngn/screen-capture.h +35 -0
  248. package/native/core/include/ngn/screen-presentation.h +85 -0
  249. package/native/core/include/ngn/screen-stacks.h +80 -0
  250. package/native/core/include/ngn/scrollers.h +106 -0
  251. package/native/core/include/ngn/sharing.h +39 -0
  252. package/native/core/include/ngn/spawn-rules.h +78 -0
  253. package/native/core/include/ngn/splash-screen.h +41 -0
  254. package/native/core/include/ngn/split-view.h +270 -0
  255. package/native/core/include/ngn/sqlite.h +38 -0
  256. package/native/core/include/ngn/store-review.h +26 -0
  257. package/native/core/include/ngn/surface-book.h +101 -0
  258. package/native/core/include/ngn/svg.h +393 -0
  259. package/native/core/include/ngn/symbol.h +90 -0
  260. package/native/core/include/ngn/system.h +102 -0
  261. package/native/core/include/ngn/tabs-host.h +171 -0
  262. package/native/core/include/ngn/text-input.h +125 -0
  263. package/native/core/include/ngn/text-measurer.h +38 -0
  264. package/native/core/include/ngn/text-recognition.h +55 -0
  265. package/native/core/include/ngn/tracking.h +34 -0
  266. package/native/core/include/ngn/tray-and-dock.h +107 -0
  267. package/native/core/include/ngn/undo.h +37 -0
  268. package/native/core/include/ngn/updates.h +253 -0
  269. package/native/core/include/ngn/video.h +126 -0
  270. package/native/core/include/ngn/view-box.h +126 -0
  271. package/native/core/include/ngn/view-transform.h +26 -0
  272. package/native/core/include/ngn/view-tree.h +85 -0
  273. package/native/core/include/ngn/web-browser.h +45 -0
  274. package/native/core/include/ngn/web-view.h +133 -0
  275. package/native/core/include/ngn/worklet-host.h +98 -0
  276. package/native/core/src/absent-views.cpp +41 -0
  277. package/native/core/src/accessibility.cpp +374 -0
  278. package/native/core/src/animation-frames.cpp +49 -0
  279. package/native/core/src/app-menus.cpp +352 -0
  280. package/native/core/src/app-windows.cpp +444 -0
  281. package/native/core/src/back-swipe.cpp +91 -0
  282. package/native/core/src/bytes.cpp +158 -0
  283. package/native/core/src/control-sizes.cpp +232 -0
  284. package/native/core/src/default-menu.cpp +96 -0
  285. package/native/core/src/desktop-modal.cpp +162 -0
  286. package/native/core/src/desktop-view-props.cpp +80 -0
  287. package/native/core/src/dev-server-helper.cpp +152 -0
  288. package/native/core/src/dev-server.cpp +103 -0
  289. package/native/core/src/download.cpp +53 -0
  290. package/native/core/src/expo-image.cpp +548 -0
  291. package/native/core/src/expo-ui.cpp +203 -0
  292. package/native/core/src/focus-order.cpp +441 -0
  293. package/native/core/src/gestures.cpp +1806 -0
  294. package/native/core/src/glass.cpp +49 -0
  295. package/native/core/src/header-bar.cpp +104 -0
  296. package/native/core/src/host-services.cpp +50 -0
  297. package/native/core/src/host-state.cpp +61 -0
  298. package/native/core/src/input-dispatcher.cpp +727 -0
  299. package/native/core/src/input-script.cpp +123 -0
  300. package/native/core/src/key-value-store.cpp +169 -0
  301. package/native/core/src/launch-settings.cpp +148 -0
  302. package/native/core/src/layout-animations.cpp +62 -0
  303. package/native/core/src/libraries/RNCWebViewSpec.cpp +80 -0
  304. package/native/core/src/libraries/react-native-reanimated.cpp +216 -0
  305. package/native/core/src/libraries/react-native-worklets.cpp +192 -0
  306. package/native/core/src/libraries/rnscreens.cpp +125 -0
  307. package/native/core/src/libraries/rnsvg.cpp +345 -0
  308. package/native/core/src/libraries/safeareacontext.cpp +70 -0
  309. package/native/core/src/libraries/worklets-idle-frames.js +70 -0
  310. package/native/core/src/library-glue.cpp +35 -0
  311. package/native/core/src/live-regions.cpp +122 -0
  312. package/native/core/src/module-registry.cpp +50 -0
  313. package/native/core/src/modules/accessibility-manager.cpp +98 -0
  314. package/native/core/src/modules/address-search.cpp +219 -0
  315. package/native/core/src/modules/alert-manager.cpp +140 -0
  316. package/native/core/src/modules/app-info.cpp +74 -0
  317. package/native/core/src/modules/app-modules.cpp +405 -0
  318. package/native/core/src/modules/apple-authentication.cpp +188 -0
  319. package/native/core/src/modules/asset.cpp +111 -0
  320. package/native/core/src/modules/async-storage.cpp +192 -0
  321. package/native/core/src/modules/audio.cpp +541 -0
  322. package/native/core/src/modules/battery.cpp +88 -0
  323. package/native/core/src/modules/calendar.cpp +457 -0
  324. package/native/core/src/modules/camera.cpp +221 -0
  325. package/native/core/src/modules/core-modules.cpp +62 -0
  326. package/native/core/src/modules/crypto.cpp +117 -0
  327. package/native/core/src/modules/desktop-expo.cpp +527 -0
  328. package/native/core/src/modules/file-system.cpp +941 -0
  329. package/native/core/src/modules/fonts.cpp +163 -0
  330. package/native/core/src/modules/image-loader.cpp +105 -0
  331. package/native/core/src/modules/image-manipulator.cpp +305 -0
  332. package/native/core/src/modules/language-model.cpp +195 -0
  333. package/native/core/src/modules/local-authentication.cpp +140 -0
  334. package/native/core/src/modules/location.cpp +186 -0
  335. package/native/core/src/modules/maps.cpp +500 -0
  336. package/native/core/src/modules/network-state.cpp +72 -0
  337. package/native/core/src/modules/networking.cpp +387 -0
  338. package/native/core/src/modules/notifications.cpp +549 -0
  339. package/native/core/src/modules/photo-library.cpp +371 -0
  340. package/native/core/src/modules/printing.cpp +201 -0
  341. package/native/core/src/modules/screen-capture.cpp +71 -0
  342. package/native/core/src/modules/sharing.cpp +59 -0
  343. package/native/core/src/modules/splash-screen.cpp +70 -0
  344. package/native/core/src/modules/sqlite.cpp +421 -0
  345. package/native/core/src/modules/store-review.cpp +23 -0
  346. package/native/core/src/modules/text-recognition.cpp +107 -0
  347. package/native/core/src/modules/tracking.cpp +46 -0
  348. package/native/core/src/modules/undo.cpp +59 -0
  349. package/native/core/src/modules/video.cpp +366 -0
  350. package/native/core/src/modules/web-browser.cpp +100 -0
  351. package/native/core/src/modules/web-socket.cpp +266 -0
  352. package/native/core/src/mounting-manager.cpp +380 -0
  353. package/native/core/src/native-controls.cpp +129 -0
  354. package/native/core/src/native-module.cpp +250 -0
  355. package/native/core/src/network.cpp +219 -0
  356. package/native/core/src/posix-launcher.cpp +366 -0
  357. package/native/core/src/processes.cpp +324 -0
  358. package/native/core/src/red-box-errors.js +83 -0
  359. package/native/core/src/red-box.cpp +306 -0
  360. package/native/core/src/runtime.cpp +422 -0
  361. package/native/core/src/safe-areas.cpp +130 -0
  362. package/native/core/src/screen-presentation.cpp +61 -0
  363. package/native/core/src/screen-stacks.cpp +254 -0
  364. package/native/core/src/scrollers.cpp +181 -0
  365. package/native/core/src/spawn-rules.cpp +292 -0
  366. package/native/core/src/split-view.cpp +343 -0
  367. package/native/core/src/svg.cpp +1883 -0
  368. package/native/core/src/symbol.cpp +94 -0
  369. package/native/core/src/tabs-host.cpp +234 -0
  370. package/native/core/src/text-input.cpp +126 -0
  371. package/native/core/src/text-layout-manager.cpp +63 -0
  372. package/native/core/src/tray-and-dock.cpp +154 -0
  373. package/native/core/src/updates.cpp +965 -0
  374. package/native/core/src/view-box.cpp +788 -0
  375. package/native/core/src/view-transform.cpp +45 -0
  376. package/native/core/src/view-tree.cpp +234 -0
  377. package/native/core/src/web-view.cpp +143 -0
  378. package/native/core/src/worklet-host.cpp +10 -0
  379. package/native/scripts/build-hermes.sh +44 -0
  380. package/native/scripts/build.sh +171 -0
  381. package/native/scripts/cache-dir.sh +18 -0
  382. package/native/scripts/codegen-library.sh +193 -0
  383. package/native/scripts/compiler-launcher.sh +17 -0
  384. package/native/scripts/fetch.sh +174 -0
  385. package/native/scripts/hermes-built.sh +10 -0
  386. package/native/scripts/host-dir.sh +16 -0
  387. package/native/scripts/macos-target.sh +8 -0
  388. package/native/scripts/make-prebuilt.mjs +360 -0
  389. package/native/scripts/prebuilt-host-cache.mjs +158 -0
  390. package/native/shim/openssl/sha.h +22 -0
  391. package/native/shim/view-platform/react/renderer/components/view/HostPlatformViewTraitsInitializer.h +37 -0
  392. package/package.json +70 -3
  393. package/prebuilt.json +33 -0
@@ -0,0 +1,500 @@
1
+ #include "ngn/maps.h"
2
+
3
+ #include "ngn/crypto.h"
4
+ #include "ngn/desktop-view-props.h"
5
+
6
+ #include <algorithm>
7
+ #include <cctype>
8
+ #include <cmath>
9
+ #include <random>
10
+ #include <string_view>
11
+ #include <unordered_map>
12
+
13
+ using namespace facebook::react;
14
+ using folly::dynamic;
15
+
16
+ namespace ngn {
17
+
18
+ const char MapComponentName[] = "ViewManagerAdapter_ExpoAppleMaps";
19
+
20
+ MapProps::MapProps(const PropsParserContext &context, const MapProps &sourceProps, const RawProps &rawProps)
21
+ : ViewProps(context, sourceProps, rawProps),
22
+ HasDesktopProps{parseDesktopProps(context, sourceProps.desktop, rawProps)},
23
+ values(sourceProps.values) {
24
+ auto raw = rawProps.toDynamic();
25
+ if (!raw.isObject()) {
26
+ return;
27
+ }
28
+ for (const auto &[key, value] : raw.items()) {
29
+ if (value.isNull()) {
30
+ values.erase(key);
31
+ } else {
32
+ values[key] = value;
33
+ }
34
+ }
35
+ }
36
+
37
+ namespace {
38
+
39
+ constexpr double kPi = 3.14159265358979323846;
40
+ // The mean radius, in metres.
41
+ constexpr double kEarthRadius = 6371008.8;
42
+
43
+ const dynamic &field(const dynamic &object, const char *key) {
44
+ static const dynamic none;
45
+ if (!object.isObject()) {
46
+ return none;
47
+ }
48
+ const auto *found = object.get_ptr(key);
49
+ return found ? *found : none;
50
+ }
51
+
52
+ double number(const dynamic &object, const char *key, double otherwise) {
53
+ const auto &value = field(object, key);
54
+ return value.isNumber() ? value.asDouble() : otherwise;
55
+ }
56
+
57
+ bool flag(const dynamic &object, const char *key, bool otherwise) {
58
+ const auto &value = field(object, key);
59
+ return value.isBool() ? value.asBool() : otherwise;
60
+ }
61
+
62
+ std::string text(const dynamic &object, const char *key) {
63
+ const auto &value = field(object, key);
64
+ return value.isString() ? value.asString() : "";
65
+ }
66
+
67
+ std::optional<double> optionalNumber(const dynamic &object, const char *key) {
68
+ const auto &value = field(object, key);
69
+ return value.isNumber() ? std::optional(value.asDouble()) : std::nullopt;
70
+ }
71
+
72
+ MapCoordinate coordinateOf(const dynamic &value) {
73
+ return {number(value, "latitude", 0), number(value, "longitude", 0)};
74
+ }
75
+
76
+ std::vector<MapCoordinate> coordinatesOf(const dynamic &value) {
77
+ std::vector<MapCoordinate> out;
78
+ if (value.isArray()) {
79
+ for (const auto &one : value) {
80
+ out.push_back(coordinateOf(one));
81
+ }
82
+ }
83
+ return out;
84
+ }
85
+
86
+ dynamic coordinateValue(MapCoordinate coordinate) {
87
+ return dynamic::object("latitude", coordinate.latitude)("longitude", coordinate.longitude);
88
+ }
89
+
90
+ dynamic coordinatesValue(const std::vector<MapCoordinate> &coordinates) {
91
+ dynamic out = dynamic::array();
92
+ for (const auto &one : coordinates) {
93
+ out.push_back(coordinateValue(one));
94
+ }
95
+ return out;
96
+ }
97
+
98
+ // What an item with no id is called: a new UUID each time, as on iOS.
99
+ std::string newId() {
100
+ static std::mutex mutex;
101
+ static std::random_device device;
102
+ std::lock_guard lock(mutex);
103
+ std::string bytes(16, '\0');
104
+ for (auto &byte : bytes) {
105
+ byte = static_cast<char>(device() & 0xff);
106
+ }
107
+ return uuidV4(bytes);
108
+ }
109
+
110
+ std::string idOf(const dynamic &object) {
111
+ auto id = text(object, "id");
112
+ return id.empty() ? newId() : id;
113
+ }
114
+
115
+ const dynamic &givenColor(const dynamic &object, const char *key) {
116
+ return field(object, key);
117
+ }
118
+
119
+ dynamic orNull(const dynamic &value) {
120
+ return value.isNull() ? dynamic(nullptr) : value;
121
+ }
122
+
123
+ std::optional<std::vector<std::string>> categories(const dynamic &value) {
124
+ if (!value.isArray()) {
125
+ return std::nullopt;
126
+ }
127
+ std::vector<std::string> out;
128
+ for (const auto &one : value) {
129
+ if (one.isString()) {
130
+ out.push_back(mapPointOfInterestCategory(one.asString()));
131
+ }
132
+ }
133
+ return out;
134
+ }
135
+
136
+ // Web Mercator, as MKMapPoint is, with both axes 0 to 1.
137
+ struct Projected {
138
+ double x;
139
+ double y;
140
+ };
141
+
142
+ Projected project(MapCoordinate c) {
143
+ double latitude = std::clamp(c.latitude, -85.0511287798, 85.0511287798) * kPi / 180;
144
+ return {(c.longitude + 180) / 360, (1 - std::log(std::tan(latitude) + 1 / std::cos(latitude)) / kPi) / 2};
145
+ }
146
+
147
+ MapCoordinate unproject(Projected p) {
148
+ return {std::atan(std::sinh(kPi * (1 - 2 * p.y))) * 180 / kPi, p.x * 360 - 180};
149
+ }
150
+
151
+ // Metres from `tap` to the segment from `a` to `b`, the projection taken on the flat map, as
152
+ // expo-maps measures it with MKMapPoints.
153
+ double distanceToSegment(MapCoordinate tap, MapCoordinate a, MapCoordinate b) {
154
+ auto p = project(tap);
155
+ auto pa = project(a);
156
+ auto pb = project(b);
157
+ double dx = pb.x - pa.x;
158
+ double dy = pb.y - pa.y;
159
+ if (dx == 0 && dy == 0) {
160
+ return mapDistance(tap, a);
161
+ }
162
+ double t = std::clamp(((p.x - pa.x) * dx + (p.y - pa.y) * dy) / (dx * dx + dy * dy), 0.0, 1.0);
163
+ return mapDistance(tap, unproject({pa.x + t * dx, pa.y + t * dy}));
164
+ }
165
+
166
+ bool insidePolygon(MapCoordinate tap, const std::vector<MapCoordinate> &corners) {
167
+ bool inside = false;
168
+ size_t n = corners.size();
169
+ for (size_t i = 0, j = n - 1; i < n; j = i++) {
170
+ const auto &vi = corners[i];
171
+ const auto &vj = corners[j];
172
+ if ((vi.latitude > tap.latitude) != (vj.latitude > tap.latitude) &&
173
+ tap.longitude < (vj.longitude - vi.longitude) * (tap.latitude - vi.latitude) / (vj.latitude - vi.latitude) + vi.longitude) {
174
+ inside = !inside;
175
+ }
176
+ }
177
+ return inside;
178
+ }
179
+
180
+ bool onPolyline(MapCoordinate tap, const MapPolyline &line, double threshold) {
181
+ std::vector<MapCoordinate> points;
182
+ if (line.geodesic) {
183
+ for (size_t i = 0; i + 1 < line.coordinates.size(); i++) {
184
+ auto arc = mapGreatCircle(line.coordinates[i], line.coordinates[i + 1]);
185
+ points.insert(points.end(), arc.begin(), arc.end() - 1);
186
+ }
187
+ if (!line.coordinates.empty()) {
188
+ points.push_back(line.coordinates.back());
189
+ }
190
+ } else {
191
+ points = line.coordinates;
192
+ }
193
+ for (size_t i = 0; i + 1 < points.size(); i++) {
194
+ if (distanceToSegment(tap, points[i], points[i + 1]) < threshold) {
195
+ return true;
196
+ }
197
+ }
198
+ return false;
199
+ }
200
+
201
+ const std::unordered_map<std::string, uint32_t> &namedColors() {
202
+ static const std::unordered_map<std::string, uint32_t> colors{
203
+ {"transparent", 0x00000000}, {"black", 0xff000000}, {"white", 0xffffffff}, {"red", 0xffff0000},
204
+ {"green", 0xff008000}, {"blue", 0xff0000ff}, {"yellow", 0xffffff00}, {"orange", 0xffffa500},
205
+ {"purple", 0xff800080}, {"pink", 0xffffc0cb}, {"gray", 0xff808080}, {"grey", 0xff808080},
206
+ {"brown", 0xffa52a2a}, {"cyan", 0xff00ffff}, {"magenta", 0xffff00ff}, {"lime", 0xff00ff00},
207
+ {"navy", 0xff000080}, {"teal", 0xff008080}, {"maroon", 0xff800000}, {"olive", 0xff808000},
208
+ {"silver", 0xffc0c0c0}, {"aqua", 0xff00ffff}, {"fuchsia", 0xffff00ff},
209
+ };
210
+ return colors;
211
+ }
212
+
213
+ } // namespace
214
+
215
+ std::optional<uint32_t> mapColor(const dynamic &value) {
216
+ if (value.isNumber()) {
217
+ return static_cast<uint32_t>(static_cast<int64_t>(value.asDouble()));
218
+ }
219
+ if (!value.isString()) {
220
+ return std::nullopt;
221
+ }
222
+ std::string text = value.asString();
223
+ std::transform(text.begin(), text.end(), text.begin(), [](unsigned char c) { return std::tolower(c); });
224
+ if (auto named = namedColors().find(text); named != namedColors().end()) {
225
+ return named->second;
226
+ }
227
+ if (text.empty() || text[0] != '#') {
228
+ return std::nullopt;
229
+ }
230
+ std::string hex = text.substr(1);
231
+ if (hex.empty() || !std::all_of(hex.begin(), hex.end(), [](unsigned char c) { return std::isxdigit(c); })) {
232
+ return std::nullopt;
233
+ }
234
+ if (hex.size() == 3 || hex.size() == 4) {
235
+ std::string wide;
236
+ for (char c : hex) {
237
+ wide += std::string(2, c);
238
+ }
239
+ hex = wide;
240
+ }
241
+ if (hex.size() != 6 && hex.size() != 8) {
242
+ return std::nullopt;
243
+ }
244
+ auto rgba = static_cast<uint32_t>(std::stoul(hex, nullptr, 16));
245
+ if (hex.size() == 6) {
246
+ return 0xff000000u | rgba;
247
+ }
248
+ return (rgba >> 8) | ((rgba & 0xff) << 24);
249
+ }
250
+
251
+ std::string mapPointOfInterestCategory(const std::string &expoName) {
252
+ static const std::unordered_map<std::string, std::string> initialisms{{"EV", "EV"}, {"ATM", "ATM"}, {"RV", "RV"}};
253
+ std::string out = "MKPOICategory";
254
+ size_t start = 0;
255
+ while (start <= expoName.size()) {
256
+ size_t end = expoName.find('_', start);
257
+ if (end == std::string::npos) {
258
+ end = expoName.size();
259
+ }
260
+ std::string word = expoName.substr(start, end - start);
261
+ if (auto kept = initialisms.find(word); kept != initialisms.end()) {
262
+ out += kept->second;
263
+ } else if (!word.empty()) {
264
+ out += static_cast<char>(std::toupper(static_cast<unsigned char>(word[0])));
265
+ for (size_t i = 1; i < word.size(); i++) {
266
+ out += static_cast<char>(std::tolower(static_cast<unsigned char>(word[i])));
267
+ }
268
+ }
269
+ start = end + 1;
270
+ }
271
+ return out;
272
+ }
273
+
274
+ MapSpan mapSpanForZoom(double zoom) {
275
+ double longitude = std::min(360 / std::pow(2.0, zoom), 360.0);
276
+ return {std::min(longitude, 180.0), longitude};
277
+ }
278
+
279
+ double mapZoomForSpan(double longitudeDelta) {
280
+ return std::log2(360 / longitudeDelta);
281
+ }
282
+
283
+ std::optional<MapCamera> mapCameraOf(const dynamic &position) {
284
+ const auto &coordinates = field(position, "coordinates");
285
+ if (!coordinates.isObject()) {
286
+ return std::nullopt;
287
+ }
288
+ return MapCamera{coordinateOf(coordinates), number(position, "zoom", 1)};
289
+ }
290
+
291
+ MapModel mapModel(const dynamic &props) {
292
+ MapModel map;
293
+ map.givenMarkers = field(props, "markers");
294
+ map.givenShapes = dynamic::array(field(props, "polylines"), field(props, "polygons"), field(props, "circles"));
295
+ if (map.givenMarkers.isArray()) {
296
+ for (const auto &one : map.givenMarkers) {
297
+ map.markers.push_back({
298
+ .id = idOf(one),
299
+ .coordinates = coordinateOf(field(one, "coordinates")),
300
+ .title = text(one, "title"),
301
+ .systemImage = text(one, "systemImage"),
302
+ .monogram = text(one, "monogram"),
303
+ .tint = mapColor(field(one, "tintColor")),
304
+ .given = one,
305
+ });
306
+ }
307
+ }
308
+ if (const auto &lines = field(props, "polylines"); lines.isArray()) {
309
+ for (const auto &one : lines) {
310
+ map.polylines.push_back({
311
+ .id = idOf(one),
312
+ .coordinates = coordinatesOf(field(one, "coordinates")),
313
+ .color = mapColor(field(one, "color")),
314
+ .width = number(one, "width", 4),
315
+ .geodesic = text(one, "contourStyle") == "GEODESIC",
316
+ .given = one,
317
+ });
318
+ }
319
+ }
320
+ if (const auto &polygons = field(props, "polygons"); polygons.isArray()) {
321
+ for (const auto &one : polygons) {
322
+ map.polygons.push_back({
323
+ .id = idOf(one),
324
+ .coordinates = coordinatesOf(field(one, "coordinates")),
325
+ .color = mapColor(field(one, "color")),
326
+ .lineColor = mapColor(field(one, "lineColor")),
327
+ .lineWidth = number(one, "lineWidth", 0),
328
+ .given = one,
329
+ });
330
+ }
331
+ }
332
+ if (const auto &circles = field(props, "circles"); circles.isArray()) {
333
+ for (const auto &one : circles) {
334
+ map.circles.push_back({
335
+ .id = idOf(one),
336
+ .center = coordinateOf(field(one, "center")),
337
+ .radius = number(one, "radius", 0),
338
+ .color = mapColor(field(one, "color")),
339
+ .lineColor = mapColor(field(one, "lineColor")),
340
+ .lineWidth = number(one, "lineWidth", 0),
341
+ .given = one,
342
+ });
343
+ }
344
+ }
345
+ map.camera = mapCameraOf(field(props, "cameraPosition"));
346
+
347
+ const auto &properties = field(props, "properties");
348
+ map.myLocation = flag(properties, "isMyLocationEnabled", false);
349
+ auto type = text(properties, "mapType");
350
+ map.type = type == "HYBRID" ? MapType::Hybrid : type == "IMAGERY" ? MapType::Imagery : MapType::Standard;
351
+ map.traffic = flag(properties, "isTrafficEnabled", false);
352
+ map.selection = flag(properties, "selectionEnabled", true);
353
+ map.polylineTapThreshold = number(properties, "polylineTapThreshold", 20);
354
+ map.realisticElevation = text(properties, "elevation") == "REALISTIC";
355
+ map.mutedEmphasis = text(properties, "emphasis") == "MUTED";
356
+ const auto &points = field(properties, "pointsOfInterest");
357
+ map.pointsIncluding = categories(field(points, "including"));
358
+ if (!map.pointsIncluding) {
359
+ map.pointsExcluding = categories(field(points, "excluding"));
360
+ }
361
+
362
+ const auto &ui = field(props, "uiSettings");
363
+ map.compass = flag(ui, "compassEnabled", true);
364
+ map.scaleBar = flag(ui, "scaleBarEnabled", false);
365
+ map.pitchToggle = flag(ui, "togglePitchEnabled", true);
366
+ map.userLocationButton = flag(ui, "myLocationButtonEnabled", true);
367
+
368
+ auto scheme = text(props, "colorScheme");
369
+ map.colorScheme = scheme == "LIGHT" ? MapColorScheme::Light : scheme == "DARK" ? MapColorScheme::Dark : MapColorScheme::Automatic;
370
+ return map;
371
+ }
372
+
373
+ double mapDistance(MapCoordinate a, MapCoordinate b) {
374
+ double la = a.latitude * kPi / 180;
375
+ double lb = b.latitude * kPi / 180;
376
+ double dLat = lb - la;
377
+ double dLon = (b.longitude - a.longitude) * kPi / 180;
378
+ double h = std::sin(dLat / 2) * std::sin(dLat / 2) + std::cos(la) * std::cos(lb) * std::sin(dLon / 2) * std::sin(dLon / 2);
379
+ return 2 * kEarthRadius * std::asin(std::min(1.0, std::sqrt(h)));
380
+ }
381
+
382
+ std::vector<MapCoordinate> mapGreatCircle(MapCoordinate a, MapCoordinate b, int segments) {
383
+ double lat1 = a.latitude * kPi / 180;
384
+ double lon1 = a.longitude * kPi / 180;
385
+ double lat2 = b.latitude * kPi / 180;
386
+ double lon2 = b.longitude * kPi / 180;
387
+ double d = std::acos(std::clamp(std::sin(lat1) * std::sin(lat2) + std::cos(lat1) * std::cos(lat2) * std::cos(lon2 - lon1), -1.0, 1.0));
388
+ if (d <= 1e-10) {
389
+ return {a, b};
390
+ }
391
+ std::vector<MapCoordinate> out;
392
+ for (int i = 0; i <= segments; i++) {
393
+ double f = static_cast<double>(i) / segments;
394
+ double ka = std::sin((1 - f) * d) / std::sin(d);
395
+ double kb = std::sin(f * d) / std::sin(d);
396
+ double x = ka * std::cos(lat1) * std::cos(lon1) + kb * std::cos(lat2) * std::cos(lon2);
397
+ double y = ka * std::cos(lat1) * std::sin(lon1) + kb * std::cos(lat2) * std::sin(lon2);
398
+ double z = ka * std::sin(lat1) + kb * std::sin(lat2);
399
+ out.push_back({std::atan2(z, std::sqrt(x * x + y * y)) * 180 / kPi, std::atan2(y, x) * 180 / kPi});
400
+ }
401
+ return out;
402
+ }
403
+
404
+ MapEvent mapClickAt(const MapModel &map, MapCoordinate tap) {
405
+ for (const auto &polygon : map.polygons) {
406
+ if (insidePolygon(tap, polygon.coordinates)) {
407
+ return {"polygonClick",
408
+ dynamic::object("id", polygon.id)("color", orNull(givenColor(polygon.given, "color")))(
409
+ "lineColor", orNull(givenColor(polygon.given, "lineColor")))("lineWidth", optionalNumber(polygon.given, "lineWidth") ? dynamic(polygon.lineWidth) : dynamic(nullptr))(
410
+ "coordinates", coordinatesValue(polygon.coordinates))};
411
+ }
412
+ }
413
+ for (const auto &circle : map.circles) {
414
+ if (mapDistance(tap, circle.center) <= circle.radius) {
415
+ return {"circleClick",
416
+ dynamic::object("id", circle.id)("color", orNull(givenColor(circle.given, "color")))(
417
+ "lineColor", orNull(givenColor(circle.given, "lineColor")))("lineWidth", optionalNumber(circle.given, "lineWidth") ? dynamic(circle.lineWidth) : dynamic(nullptr))(
418
+ "radius", circle.radius)("coordinates", coordinateValue(circle.center))};
419
+ }
420
+ }
421
+ for (const auto &line : map.polylines) {
422
+ if (onPolyline(tap, line, map.polylineTapThreshold)) {
423
+ return {"polylineClick",
424
+ dynamic::object("id", line.id)("color", orNull(givenColor(line.given, "color")))("width", line.width)(
425
+ "contourStyle", line.geodesic ? "GEODESIC" : "STRAIGHT")("coordinates", coordinatesValue(line.coordinates))};
426
+ }
427
+ }
428
+ return {"mapClick", dynamic::object("coordinates", coordinateValue(tap))};
429
+ }
430
+
431
+ MapEvent mapMarkerClick(const MapMarker &marker) {
432
+ return {"markerClick",
433
+ dynamic::object("id", marker.id)("title", marker.title)("tintColor", orNull(givenColor(marker.given, "tintColor")))(
434
+ "systemImage", marker.systemImage)("monogram", marker.monogram)("coordinates", coordinateValue(marker.coordinates))};
435
+ }
436
+
437
+ MapEvent mapCameraMove(MapCoordinate center, MapSpan span, double pitch, double heading) {
438
+ return {"cameraMove",
439
+ dynamic::object("coordinates", coordinateValue(center))("longitudeDelta", span.longitudeDelta)("latitudeDelta", span.latitudeDelta)(
440
+ "zoom", mapZoomForSpan(span.longitudeDelta))("tilt", pitch)("bearing", heading)};
441
+ }
442
+
443
+ bool mapShowsUserLocation(const MapModel &map, LocationSource::Authorization authorization) {
444
+ return map.myLocation && authorization == LocationSource::Authorization::Granted;
445
+ }
446
+
447
+ void MapViews::attach(int tag, std::shared_ptr<MapController> map) {
448
+ std::lock_guard lock(mutex_);
449
+ maps_[tag] = std::move(map);
450
+ }
451
+
452
+ void MapViews::detach(int tag) {
453
+ std::lock_guard lock(mutex_);
454
+ maps_.erase(tag);
455
+ }
456
+
457
+ std::shared_ptr<MapController> MapViews::find(int tag) const {
458
+ std::lock_guard lock(mutex_);
459
+ auto found = maps_.find(tag);
460
+ return found == maps_.end() ? nullptr : found->second;
461
+ }
462
+
463
+ std::shared_ptr<NativeModule> makeMapsModule(std::shared_ptr<MapViews> views) {
464
+ struct Maps : NativeModule {
465
+ explicit Maps(std::shared_ptr<MapViews> views) : NativeModule("NgNativeMaps") {
466
+ auto find = [views](const dynamic &args, JsPromise &promise) -> std::shared_ptr<MapController> {
467
+ int tag = !args.empty() && args[0].isNumber() ? static_cast<int>(args[0].asDouble()) : -1;
468
+ auto map = views->find(tag);
469
+ if (!map) {
470
+ promise.reject("Unable to find the map view with tag " + std::to_string(tag));
471
+ }
472
+ return map;
473
+ };
474
+ promise("setCameraPosition", [find](const dynamic &args, JsPromise promise) {
475
+ auto map = find(args, promise);
476
+ if (!map) {
477
+ return;
478
+ }
479
+ map->setCamera(mapCameraOf(args.size() > 1 ? args[1] : dynamic(nullptr)));
480
+ promise.resolve(nullptr);
481
+ });
482
+ promise("selectMarker", [find](const dynamic &args, JsPromise promise) {
483
+ auto map = find(args, promise);
484
+ if (!map) {
485
+ return;
486
+ }
487
+ std::optional<std::string> id;
488
+ if (args.size() > 1 && args[1].isString()) {
489
+ id = args[1].asString();
490
+ }
491
+ const dynamic options = args.size() > 2 ? args[2] : dynamic(nullptr);
492
+ map->selectMarker(id, optionalNumber(options, "zoom"), flag(options, "moveCamera", true));
493
+ promise.resolve(nullptr);
494
+ });
495
+ }
496
+ };
497
+ return std::make_shared<Maps>(std::move(views));
498
+ }
499
+
500
+ } // namespace ngn
@@ -0,0 +1,72 @@
1
+ #include "ngn/network-state.h"
2
+
3
+ #include <mutex>
4
+ #include <optional>
5
+ #include <vector>
6
+
7
+ namespace ngn {
8
+
9
+ folly::dynamic networkStateOf(const NetworkPath &path) {
10
+ const char *type = !path.satisfied ? "NONE"
11
+ : path.wifi ? "WIFI"
12
+ : path.ethernet ? "ETHERNET"
13
+ : path.cellular ? "CELLULAR"
14
+ : path.other ? "OTHER"
15
+ : "UNKNOWN";
16
+ return folly::dynamic::object("type", type)("isConnected", path.satisfied)("isInternetReachable", path.satisfied);
17
+ }
18
+
19
+ std::shared_ptr<NativeModule> makeNetworkModule(std::shared_ptr<PathMonitor> monitor) {
20
+ struct Network : NativeModule {
21
+ std::mutex mutex;
22
+ std::optional<NetworkPath> path;
23
+ std::vector<JsPromise> waiting;
24
+ // Kept for `stop`, which may need it.
25
+ std::shared_ptr<PathMonitor> monitor;
26
+ std::function<void()> stop;
27
+
28
+ explicit Network(std::shared_ptr<PathMonitor> watched) : NativeModule("NgNativeNetwork"), monitor(std::move(watched)) {
29
+ listeners();
30
+ promise("getNetworkStateAsync", [this](const folly::dynamic &, JsPromise promise) {
31
+ std::unique_lock lock(mutex);
32
+ if (!path) {
33
+ waiting.push_back(std::move(promise));
34
+ return;
35
+ }
36
+ auto state = networkStateOf(*path);
37
+ lock.unlock();
38
+ promise.resolve(std::move(state));
39
+ });
40
+ stop = monitor->watch([this](const NetworkPath &next) { changed(next); });
41
+ }
42
+
43
+ ~Network() override {
44
+ if (stop) {
45
+ stop();
46
+ }
47
+ }
48
+
49
+ void changed(const NetworkPath &next) {
50
+ std::unique_lock lock(mutex);
51
+ bool first = !path.has_value();
52
+ if (!first && *path == next) {
53
+ return;
54
+ }
55
+ path = next;
56
+ auto answered = std::move(waiting);
57
+ waiting.clear();
58
+ lock.unlock();
59
+ auto state = networkStateOf(next);
60
+ for (auto &promise : answered) {
61
+ promise.resolve(state);
62
+ }
63
+ // The first path is what getNetworkStateAsync answers; only a change after it is an event.
64
+ if (!first) {
65
+ emit("ngNativeNetwork", state);
66
+ }
67
+ }
68
+ };
69
+ return std::make_shared<Network>(std::move(monitor));
70
+ }
71
+
72
+ } // namespace ngn