@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.
- package/LICENSE +21 -0
- package/README.md +51 -2
- package/app.plugin.cjs +17 -0
- package/bin/app-config.mjs +168 -0
- package/bin/autolink.mjs +224 -0
- package/bin/cli.mjs +158 -0
- package/bin/host.mjs +427 -0
- package/bin/ng-native-desktop.mjs +4 -0
- package/bin/package.mjs +971 -0
- package/bin/plist.mjs +68 -0
- package/bin/prebuilt.mjs +328 -0
- package/bin/toolchain.mjs +192 -0
- package/native/CMakeLists.txt +276 -0
- package/native/appkit/CMakeLists.txt +109 -0
- package/native/appkit/address-search.h +26 -0
- package/native/appkit/address-search.mm +277 -0
- package/native/appkit/animation-frames.h +14 -0
- package/native/appkit/animation-frames.mm +106 -0
- package/native/appkit/app-info.h +12 -0
- package/native/appkit/app-info.mm +98 -0
- package/native/appkit/app-updater.mm +128 -0
- package/native/appkit/app-windows.h +81 -0
- package/native/appkit/app-windows.mm +318 -0
- package/native/appkit/appkit-backend.h +73 -0
- package/native/appkit/appkit-backend.mm +276 -0
- package/native/appkit/apple-authentication.h +19 -0
- package/native/appkit/apple-authentication.mm +248 -0
- package/native/appkit/apple-button-view.h +10 -0
- package/native/appkit/apple-button-view.mm +37 -0
- package/native/appkit/apple-id-button.h +16 -0
- package/native/appkit/apple-id-button.mm +50 -0
- package/native/appkit/audio.h +16 -0
- package/native/appkit/audio.mm +275 -0
- package/native/appkit/battery.h +13 -0
- package/native/appkit/battery.mm +92 -0
- package/native/appkit/bookmarks.h +17 -0
- package/native/appkit/bookmarks.mm +145 -0
- package/native/appkit/calendar.h +25 -0
- package/native/appkit/calendar.mm +660 -0
- package/native/appkit/camera-view.h +15 -0
- package/native/appkit/camera-view.mm +130 -0
- package/native/appkit/camera.h +39 -0
- package/native/appkit/camera.mm +362 -0
- package/native/appkit/cg-canvas.h +17 -0
- package/native/appkit/cg-canvas.mm +518 -0
- package/native/appkit/component-view.h +17 -0
- package/native/appkit/crypto.h +19 -0
- package/native/appkit/crypto.mm +78 -0
- package/native/appkit/desktop-expo.h +29 -0
- package/native/appkit/desktop-expo.mm +385 -0
- package/native/appkit/expo-image-drawing.h +46 -0
- package/native/appkit/expo-image-drawing.mm +131 -0
- package/native/appkit/expo-image-view.h +15 -0
- package/native/appkit/expo-image-view.mm +182 -0
- package/native/appkit/expo-ui-renderer.h +68 -0
- package/native/appkit/expo-ui-renderer.mm +101 -0
- package/native/appkit/expo-ui-view.h +26 -0
- package/native/appkit/expo-ui-view.mm +134 -0
- package/native/appkit/expo-ui.swift +1284 -0
- package/native/appkit/file-system.h +16 -0
- package/native/appkit/file-system.mm +140 -0
- package/native/appkit/fonts.h +12 -0
- package/native/appkit/fonts.mm +48 -0
- package/native/appkit/geocoder.h +19 -0
- package/native/appkit/geocoder.mm +101 -0
- package/native/appkit/glass-effect.h +22 -0
- package/native/appkit/glass-effect.mm +63 -0
- package/native/appkit/glass-view.h +8 -0
- package/native/appkit/glass-view.mm +65 -0
- package/native/appkit/header-toolbar.h +60 -0
- package/native/appkit/header-toolbar.mm +629 -0
- package/native/appkit/host-view.h +54 -0
- package/native/appkit/host-view.mm +1200 -0
- package/native/appkit/image-manipulator.h +14 -0
- package/native/appkit/image-manipulator.mm +191 -0
- package/native/appkit/image-request.h +22 -0
- package/native/appkit/image-request.mm +62 -0
- package/native/appkit/image-view.h +8 -0
- package/native/appkit/image-view.mm +142 -0
- package/native/appkit/input-replay.h +25 -0
- package/native/appkit/input-replay.mm +418 -0
- package/native/appkit/language-model.h +14 -0
- package/native/appkit/language-model.mm +99 -0
- package/native/appkit/language-model.swift +472 -0
- package/native/appkit/local-authentication.h +15 -0
- package/native/appkit/local-authentication.mm +133 -0
- package/native/appkit/location.h +16 -0
- package/native/appkit/location.mm +214 -0
- package/native/appkit/main.mm +1515 -0
- package/native/appkit/map-view.h +15 -0
- package/native/appkit/map-view.mm +109 -0
- package/native/appkit/maps.h +35 -0
- package/native/appkit/maps.mm +343 -0
- package/native/appkit/menu-bar.h +22 -0
- package/native/appkit/menu-bar.mm +208 -0
- package/native/appkit/modal-focus.h +16 -0
- package/native/appkit/modal-focus.mm +26 -0
- package/native/appkit/modal-host-view.h +79 -0
- package/native/appkit/modal-host-view.mm +442 -0
- package/native/appkit/native-controls.h +22 -0
- package/native/appkit/native-controls.mm +431 -0
- package/native/appkit/network-state.h +12 -0
- package/native/appkit/network-state.mm +64 -0
- package/native/appkit/network.h +14 -0
- package/native/appkit/network.mm +396 -0
- package/native/appkit/notifications.h +19 -0
- package/native/appkit/notifications.mm +309 -0
- package/native/appkit/photo-library.h +33 -0
- package/native/appkit/photo-library.mm +548 -0
- package/native/appkit/pointer-buttons.h +32 -0
- package/native/appkit/presentation-label.h +15 -0
- package/native/appkit/presentation-label.mm +12 -0
- package/native/appkit/presented-screen.h +19 -0
- package/native/appkit/presented-screen.mm +181 -0
- package/native/appkit/printing.h +42 -0
- package/native/appkit/printing.mm +393 -0
- package/native/appkit/push.h +19 -0
- package/native/appkit/push.mm +81 -0
- package/native/appkit/red-box.h +26 -0
- package/native/appkit/red-box.mm +387 -0
- package/native/appkit/screen-capture.h +21 -0
- package/native/appkit/screen-capture.mm +92 -0
- package/native/appkit/scroll-view.h +13 -0
- package/native/appkit/scroll-view.mm +251 -0
- package/native/appkit/scrollers.h +25 -0
- package/native/appkit/scrollers.mm +43 -0
- package/native/appkit/search-field.h +34 -0
- package/native/appkit/search-field.mm +291 -0
- package/native/appkit/search-toolbar-item.h +19 -0
- package/native/appkit/search-toolbar-item.mm +269 -0
- package/native/appkit/share-picker.h +34 -0
- package/native/appkit/share-picker.mm +121 -0
- package/native/appkit/split-view.h +77 -0
- package/native/appkit/split-view.mm +1206 -0
- package/native/appkit/store-review.h +13 -0
- package/native/appkit/store-review.mm +28 -0
- package/native/appkit/surface-view.h +45 -0
- package/native/appkit/surface-view.mm +558 -0
- package/native/appkit/svg-view.h +17 -0
- package/native/appkit/svg-view.mm +57 -0
- package/native/appkit/symbol-drawing.h +24 -0
- package/native/appkit/symbol-drawing.mm +117 -0
- package/native/appkit/symbol-view.h +8 -0
- package/native/appkit/symbol-view.mm +33 -0
- package/native/appkit/system-access.h +34 -0
- package/native/appkit/system-access.mm +210 -0
- package/native/appkit/system.h +24 -0
- package/native/appkit/system.mm +403 -0
- package/native/appkit/test-launch.h +36 -0
- package/native/appkit/test-launch.mm +115 -0
- package/native/appkit/text-input-field.h +76 -0
- package/native/appkit/text-input-field.mm +444 -0
- package/native/appkit/text-layout.h +58 -0
- package/native/appkit/text-layout.mm +477 -0
- package/native/appkit/text-recognition.h +13 -0
- package/native/appkit/text-recognition.mm +102 -0
- package/native/appkit/tracking.h +13 -0
- package/native/appkit/tracking.mm +51 -0
- package/native/appkit/tray-and-dock.h +45 -0
- package/native/appkit/tray-and-dock.mm +130 -0
- package/native/appkit/undo.h +23 -0
- package/native/appkit/undo.mm +125 -0
- package/native/appkit/updates.h +17 -0
- package/native/appkit/updates.mm +71 -0
- package/native/appkit/video-view.h +10 -0
- package/native/appkit/video-view.mm +42 -0
- package/native/appkit/video.h +38 -0
- package/native/appkit/video.mm +404 -0
- package/native/appkit/web-browser.h +12 -0
- package/native/appkit/web-browser.mm +97 -0
- package/native/appkit/web-view-component.h +9 -0
- package/native/appkit/web-view-component.mm +56 -0
- package/native/appkit/web-view.h +31 -0
- package/native/appkit/web-view.mm +394 -0
- package/native/cmake/libraries/react-native-reanimated.cmake +45 -0
- package/native/cmake/libraries/react-native-worklets.cmake +46 -0
- package/native/cmake/libraries.cmake +109 -0
- package/native/cmake/linked-modules.cmake +112 -0
- package/native/cmake/react-native.cmake +175 -0
- package/native/cmake/runtime-dependencies.cmake +34 -0
- package/native/cmake/swift.cmake +43 -0
- package/native/cmake/third-party.cmake +74 -0
- package/native/core/include/ngn/absent-views.h +27 -0
- package/native/core/include/ngn/accessibility.h +140 -0
- package/native/core/include/ngn/address-search.h +69 -0
- package/native/core/include/ngn/animation-frames.h +39 -0
- package/native/core/include/ngn/app-info.h +52 -0
- package/native/core/include/ngn/app-menus.h +146 -0
- package/native/core/include/ngn/app-windows.h +181 -0
- package/native/core/include/ngn/apple-authentication.h +116 -0
- package/native/core/include/ngn/asset.h +28 -0
- package/native/core/include/ngn/audio.h +125 -0
- package/native/core/include/ngn/back-swipe.h +112 -0
- package/native/core/include/ngn/battery.h +48 -0
- package/native/core/include/ngn/bytes.h +59 -0
- package/native/core/include/ngn/calendar.h +180 -0
- package/native/core/include/ngn/camera.h +128 -0
- package/native/core/include/ngn/control-sizes.h +111 -0
- package/native/core/include/ngn/core-modules.h +105 -0
- package/native/core/include/ngn/crypto.h +38 -0
- package/native/core/include/ngn/default-menu.h +91 -0
- package/native/core/include/ngn/desktop-expo.h +193 -0
- package/native/core/include/ngn/desktop-modal.h +109 -0
- package/native/core/include/ngn/desktop-props.h +75 -0
- package/native/core/include/ngn/desktop-view-props.h +110 -0
- package/native/core/include/ngn/dev-server.h +33 -0
- package/native/core/include/ngn/download.h +24 -0
- package/native/core/include/ngn/expo-image.h +192 -0
- package/native/core/include/ngn/expo-ui.h +133 -0
- package/native/core/include/ngn/file-system.h +274 -0
- package/native/core/include/ngn/focus-order.h +63 -0
- package/native/core/include/ngn/fonts.h +64 -0
- package/native/core/include/ngn/gestures.h +305 -0
- package/native/core/include/ngn/glass.h +67 -0
- package/native/core/include/ngn/header-bar.h +112 -0
- package/native/core/include/ngn/host-state.h +243 -0
- package/native/core/include/ngn/image-manipulator.h +70 -0
- package/native/core/include/ngn/input-dispatcher.h +211 -0
- package/native/core/include/ngn/input-script.h +56 -0
- package/native/core/include/ngn/input.h +112 -0
- package/native/core/include/ngn/key-value-store.h +41 -0
- package/native/core/include/ngn/language-model.h +69 -0
- package/native/core/include/ngn/launch-frame.h +47 -0
- package/native/core/include/ngn/launch-settings.h +98 -0
- package/native/core/include/ngn/layout-animations.h +24 -0
- package/native/core/include/ngn/library-components.h +13 -0
- package/native/core/include/ngn/library-glue.h +53 -0
- package/native/core/include/ngn/live-regions.h +54 -0
- package/native/core/include/ngn/local-authentication.h +74 -0
- package/native/core/include/ngn/location.h +89 -0
- package/native/core/include/ngn/maps.h +226 -0
- package/native/core/include/ngn/module-registry.h +51 -0
- package/native/core/include/ngn/mounting-backend.h +96 -0
- package/native/core/include/ngn/mounting-manager.h +155 -0
- package/native/core/include/ngn/native-controls.h +161 -0
- package/native/core/include/ngn/native-module.h +109 -0
- package/native/core/include/ngn/network-state.h +44 -0
- package/native/core/include/ngn/network.h +100 -0
- package/native/core/include/ngn/notifications.h +185 -0
- package/native/core/include/ngn/photo-library.h +130 -0
- package/native/core/include/ngn/printing.h +69 -0
- package/native/core/include/ngn/processes.h +114 -0
- package/native/core/include/ngn/red-box.h +119 -0
- package/native/core/include/ngn/reload-thread.h +258 -0
- package/native/core/include/ngn/runtime.h +161 -0
- package/native/core/include/ngn/safe-areas.h +77 -0
- package/native/core/include/ngn/screen-capture.h +35 -0
- package/native/core/include/ngn/screen-presentation.h +85 -0
- package/native/core/include/ngn/screen-stacks.h +80 -0
- package/native/core/include/ngn/scrollers.h +106 -0
- package/native/core/include/ngn/sharing.h +39 -0
- package/native/core/include/ngn/spawn-rules.h +78 -0
- package/native/core/include/ngn/splash-screen.h +41 -0
- package/native/core/include/ngn/split-view.h +270 -0
- package/native/core/include/ngn/sqlite.h +38 -0
- package/native/core/include/ngn/store-review.h +26 -0
- package/native/core/include/ngn/surface-book.h +101 -0
- package/native/core/include/ngn/svg.h +393 -0
- package/native/core/include/ngn/symbol.h +90 -0
- package/native/core/include/ngn/system.h +102 -0
- package/native/core/include/ngn/tabs-host.h +171 -0
- package/native/core/include/ngn/text-input.h +125 -0
- package/native/core/include/ngn/text-measurer.h +38 -0
- package/native/core/include/ngn/text-recognition.h +55 -0
- package/native/core/include/ngn/tracking.h +34 -0
- package/native/core/include/ngn/tray-and-dock.h +107 -0
- package/native/core/include/ngn/undo.h +37 -0
- package/native/core/include/ngn/updates.h +253 -0
- package/native/core/include/ngn/video.h +126 -0
- package/native/core/include/ngn/view-box.h +126 -0
- package/native/core/include/ngn/view-transform.h +26 -0
- package/native/core/include/ngn/view-tree.h +85 -0
- package/native/core/include/ngn/web-browser.h +45 -0
- package/native/core/include/ngn/web-view.h +133 -0
- package/native/core/include/ngn/worklet-host.h +98 -0
- package/native/core/src/absent-views.cpp +41 -0
- package/native/core/src/accessibility.cpp +374 -0
- package/native/core/src/animation-frames.cpp +49 -0
- package/native/core/src/app-menus.cpp +352 -0
- package/native/core/src/app-windows.cpp +444 -0
- package/native/core/src/back-swipe.cpp +91 -0
- package/native/core/src/bytes.cpp +158 -0
- package/native/core/src/control-sizes.cpp +232 -0
- package/native/core/src/default-menu.cpp +96 -0
- package/native/core/src/desktop-modal.cpp +162 -0
- package/native/core/src/desktop-view-props.cpp +80 -0
- package/native/core/src/dev-server-helper.cpp +152 -0
- package/native/core/src/dev-server.cpp +103 -0
- package/native/core/src/download.cpp +53 -0
- package/native/core/src/expo-image.cpp +548 -0
- package/native/core/src/expo-ui.cpp +203 -0
- package/native/core/src/focus-order.cpp +441 -0
- package/native/core/src/gestures.cpp +1806 -0
- package/native/core/src/glass.cpp +49 -0
- package/native/core/src/header-bar.cpp +104 -0
- package/native/core/src/host-services.cpp +50 -0
- package/native/core/src/host-state.cpp +61 -0
- package/native/core/src/input-dispatcher.cpp +727 -0
- package/native/core/src/input-script.cpp +123 -0
- package/native/core/src/key-value-store.cpp +169 -0
- package/native/core/src/launch-settings.cpp +148 -0
- package/native/core/src/layout-animations.cpp +62 -0
- package/native/core/src/libraries/RNCWebViewSpec.cpp +80 -0
- package/native/core/src/libraries/react-native-reanimated.cpp +216 -0
- package/native/core/src/libraries/react-native-worklets.cpp +192 -0
- package/native/core/src/libraries/rnscreens.cpp +125 -0
- package/native/core/src/libraries/rnsvg.cpp +345 -0
- package/native/core/src/libraries/safeareacontext.cpp +70 -0
- package/native/core/src/libraries/worklets-idle-frames.js +70 -0
- package/native/core/src/library-glue.cpp +35 -0
- package/native/core/src/live-regions.cpp +122 -0
- package/native/core/src/module-registry.cpp +50 -0
- package/native/core/src/modules/accessibility-manager.cpp +98 -0
- package/native/core/src/modules/address-search.cpp +219 -0
- package/native/core/src/modules/alert-manager.cpp +140 -0
- package/native/core/src/modules/app-info.cpp +74 -0
- package/native/core/src/modules/app-modules.cpp +405 -0
- package/native/core/src/modules/apple-authentication.cpp +188 -0
- package/native/core/src/modules/asset.cpp +111 -0
- package/native/core/src/modules/async-storage.cpp +192 -0
- package/native/core/src/modules/audio.cpp +541 -0
- package/native/core/src/modules/battery.cpp +88 -0
- package/native/core/src/modules/calendar.cpp +457 -0
- package/native/core/src/modules/camera.cpp +221 -0
- package/native/core/src/modules/core-modules.cpp +62 -0
- package/native/core/src/modules/crypto.cpp +117 -0
- package/native/core/src/modules/desktop-expo.cpp +527 -0
- package/native/core/src/modules/file-system.cpp +941 -0
- package/native/core/src/modules/fonts.cpp +163 -0
- package/native/core/src/modules/image-loader.cpp +105 -0
- package/native/core/src/modules/image-manipulator.cpp +305 -0
- package/native/core/src/modules/language-model.cpp +195 -0
- package/native/core/src/modules/local-authentication.cpp +140 -0
- package/native/core/src/modules/location.cpp +186 -0
- package/native/core/src/modules/maps.cpp +500 -0
- package/native/core/src/modules/network-state.cpp +72 -0
- package/native/core/src/modules/networking.cpp +387 -0
- package/native/core/src/modules/notifications.cpp +549 -0
- package/native/core/src/modules/photo-library.cpp +371 -0
- package/native/core/src/modules/printing.cpp +201 -0
- package/native/core/src/modules/screen-capture.cpp +71 -0
- package/native/core/src/modules/sharing.cpp +59 -0
- package/native/core/src/modules/splash-screen.cpp +70 -0
- package/native/core/src/modules/sqlite.cpp +421 -0
- package/native/core/src/modules/store-review.cpp +23 -0
- package/native/core/src/modules/text-recognition.cpp +107 -0
- package/native/core/src/modules/tracking.cpp +46 -0
- package/native/core/src/modules/undo.cpp +59 -0
- package/native/core/src/modules/video.cpp +366 -0
- package/native/core/src/modules/web-browser.cpp +100 -0
- package/native/core/src/modules/web-socket.cpp +266 -0
- package/native/core/src/mounting-manager.cpp +380 -0
- package/native/core/src/native-controls.cpp +129 -0
- package/native/core/src/native-module.cpp +250 -0
- package/native/core/src/network.cpp +219 -0
- package/native/core/src/posix-launcher.cpp +366 -0
- package/native/core/src/processes.cpp +324 -0
- package/native/core/src/red-box-errors.js +83 -0
- package/native/core/src/red-box.cpp +306 -0
- package/native/core/src/runtime.cpp +422 -0
- package/native/core/src/safe-areas.cpp +130 -0
- package/native/core/src/screen-presentation.cpp +61 -0
- package/native/core/src/screen-stacks.cpp +254 -0
- package/native/core/src/scrollers.cpp +181 -0
- package/native/core/src/spawn-rules.cpp +292 -0
- package/native/core/src/split-view.cpp +343 -0
- package/native/core/src/svg.cpp +1883 -0
- package/native/core/src/symbol.cpp +94 -0
- package/native/core/src/tabs-host.cpp +234 -0
- package/native/core/src/text-input.cpp +126 -0
- package/native/core/src/text-layout-manager.cpp +63 -0
- package/native/core/src/tray-and-dock.cpp +154 -0
- package/native/core/src/updates.cpp +965 -0
- package/native/core/src/view-box.cpp +788 -0
- package/native/core/src/view-transform.cpp +45 -0
- package/native/core/src/view-tree.cpp +234 -0
- package/native/core/src/web-view.cpp +143 -0
- package/native/core/src/worklet-host.cpp +10 -0
- package/native/scripts/build-hermes.sh +44 -0
- package/native/scripts/build.sh +171 -0
- package/native/scripts/cache-dir.sh +18 -0
- package/native/scripts/codegen-library.sh +193 -0
- package/native/scripts/compiler-launcher.sh +17 -0
- package/native/scripts/fetch.sh +174 -0
- package/native/scripts/hermes-built.sh +10 -0
- package/native/scripts/host-dir.sh +16 -0
- package/native/scripts/macos-target.sh +8 -0
- package/native/scripts/make-prebuilt.mjs +360 -0
- package/native/scripts/prebuilt-host-cache.mjs +158 -0
- package/native/shim/openssl/sha.h +22 -0
- package/native/shim/view-platform/react/renderer/components/view/HostPlatformViewTraitsInitializer.h +37 -0
- package/package.json +70 -3
- package/prebuilt.json +33 -0
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
#include "ngn/native-controls.h"
|
|
2
|
+
|
|
3
|
+
#include "ngn/desktop-view-props.h"
|
|
4
|
+
|
|
5
|
+
#include <react/renderer/components/view/conversions.h>
|
|
6
|
+
#include <react/renderer/core/propsConversions.h>
|
|
7
|
+
|
|
8
|
+
#include <cstdlib>
|
|
9
|
+
|
|
10
|
+
using namespace facebook::react;
|
|
11
|
+
|
|
12
|
+
namespace ngn {
|
|
13
|
+
|
|
14
|
+
const char NativeButtonComponentName[] = "NgNativeButton";
|
|
15
|
+
const char SwitchControlComponentName[] = "Switch";
|
|
16
|
+
const char ActivityIndicatorControlComponentName[] = "ActivityIndicatorView";
|
|
17
|
+
const char SegmentedControlComponentName[] = "RNCSegmentedControl";
|
|
18
|
+
const char DatePickerComponentName[] = "RNDateTimePicker";
|
|
19
|
+
const char SearchFieldComponentName[] = "ViewManagerAdapter_NgNativeSearchField";
|
|
20
|
+
|
|
21
|
+
void fromRawValue(const PropsParserContext &, const RawValue &value, ControlSize &result) {
|
|
22
|
+
auto keyword = value.hasType<std::string>() ? static_cast<std::string>(value) : std::string{};
|
|
23
|
+
result = keyword == "mini" ? ControlSize::Mini
|
|
24
|
+
: keyword == "small" ? ControlSize::Small
|
|
25
|
+
: keyword == "large" ? ControlSize::Large
|
|
26
|
+
: ControlSize::Regular;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
namespace {
|
|
30
|
+
|
|
31
|
+
// `enabled` (the button's, the segmented control's) or `disabled` (React Native's switch and
|
|
32
|
+
// activity indicator), whichever the update sets.
|
|
33
|
+
bool enabledFrom(const PropsParserContext &context, const RawProps &rawProps, bool source) {
|
|
34
|
+
if (const auto *enabled = rawProps.at("enabled", nullptr, nullptr)) {
|
|
35
|
+
return enabled->hasType<bool>() ? static_cast<bool>(*enabled) : true;
|
|
36
|
+
}
|
|
37
|
+
if (const auto *disabled = rawProps.at("disabled", nullptr, nullptr)) {
|
|
38
|
+
return disabled->hasType<bool>() ? !static_cast<bool>(*disabled) : true;
|
|
39
|
+
}
|
|
40
|
+
return source;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
// A CSS font weight, as a number or a keyword, when the update sets one.
|
|
44
|
+
std::optional<int> fontWeightFrom(const RawProps &rawProps, std::optional<int> source) {
|
|
45
|
+
const auto *raw = rawProps.at("fontWeight", nullptr, nullptr);
|
|
46
|
+
if (raw == nullptr) {
|
|
47
|
+
return source;
|
|
48
|
+
}
|
|
49
|
+
if (raw->hasType<double>()) {
|
|
50
|
+
return static_cast<int>(static_cast<double>(*raw));
|
|
51
|
+
}
|
|
52
|
+
if (raw->hasType<std::string>()) {
|
|
53
|
+
auto keyword = static_cast<std::string>(*raw);
|
|
54
|
+
if (keyword == "bold") {
|
|
55
|
+
return 700;
|
|
56
|
+
}
|
|
57
|
+
if (keyword == "normal") {
|
|
58
|
+
return 400;
|
|
59
|
+
}
|
|
60
|
+
char *end = nullptr;
|
|
61
|
+
long weight = std::strtol(keyword.c_str(), &end, 10);
|
|
62
|
+
if (end != keyword.c_str() && *end == '\0') {
|
|
63
|
+
return static_cast<int>(weight);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
return std::nullopt;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
} // namespace
|
|
70
|
+
|
|
71
|
+
NativeControlProps::NativeControlProps(
|
|
72
|
+
const PropsParserContext &context,
|
|
73
|
+
const NativeControlProps &sourceProps,
|
|
74
|
+
const RawProps &rawProps)
|
|
75
|
+
: ViewProps(context, sourceProps, rawProps),
|
|
76
|
+
HasDesktopProps{parseDesktopProps(context, sourceProps.desktop, rawProps)},
|
|
77
|
+
controlSize(convertRawProp(context, rawProps, "controlSize", sourceProps.controlSize, ControlSize::Regular)),
|
|
78
|
+
enabled(enabledFrom(context, rawProps, sourceProps.enabled)),
|
|
79
|
+
title(convertRawProp(context, rawProps, "title", sourceProps.title, {})),
|
|
80
|
+
kind(convertRawProp(context, rawProps, "kind", sourceProps.kind, std::string{"default"})),
|
|
81
|
+
sfSymbol(convertRawProp(context, rawProps, "sfSymbol", sourceProps.sfSymbol, {})),
|
|
82
|
+
value(convertRawProp(context, rawProps, "value", sourceProps.value, false)),
|
|
83
|
+
animating(convertRawProp(context, rawProps, "animating", sourceProps.animating, true)),
|
|
84
|
+
hidesWhenStopped(convertRawProp(context, rawProps, "hidesWhenStopped", sourceProps.hidesWhenStopped, true)),
|
|
85
|
+
size(convertRawProp(context, rawProps, "size", sourceProps.size, std::string{"small"})),
|
|
86
|
+
color(convertRawProp(context, rawProps, "color", sourceProps.color, {})),
|
|
87
|
+
values(convertRawProp(context, rawProps, "values", sourceProps.values, {})),
|
|
88
|
+
selectedIndex(convertRawProp(context, rawProps, "selectedIndex", sourceProps.selectedIndex, -1)),
|
|
89
|
+
momentary(convertRawProp(context, rawProps, "momentary", sourceProps.momentary, false)),
|
|
90
|
+
date(convertRawProp(context, rawProps, "date", sourceProps.date, {})),
|
|
91
|
+
minimumDate(convertRawProp(context, rawProps, "minimumDate", sourceProps.minimumDate, {})),
|
|
92
|
+
maximumDate(convertRawProp(context, rawProps, "maximumDate", sourceProps.maximumDate, {})),
|
|
93
|
+
mode(convertRawProp(context, rawProps, "mode", sourceProps.mode, std::string{"date"})),
|
|
94
|
+
display(convertRawProp(context, rawProps, "displayIOS", sourceProps.display, std::string{"default"})),
|
|
95
|
+
text(convertRawProp(context, rawProps, "text", sourceProps.text, {})),
|
|
96
|
+
mostRecentEventCount(
|
|
97
|
+
convertRawProp(context, rawProps, "mostRecentEventCount", sourceProps.mostRecentEventCount, 0)),
|
|
98
|
+
placeholder(convertRawProp(context, rawProps, "placeholder", sourceProps.placeholder, {})),
|
|
99
|
+
autoCapitalize(convertRawProp(context, rawProps, "autoCapitalize", sourceProps.autoCapitalize, {})),
|
|
100
|
+
tintColor(convertRawProp(context, rawProps, "tintColor", sourceProps.tintColor, {})),
|
|
101
|
+
fontFamily(convertRawProp(context, rawProps, "fontFamily", sourceProps.fontFamily, {})),
|
|
102
|
+
fontSize(convertRawProp(context, rawProps, "fontSize", sourceProps.fontSize, {})),
|
|
103
|
+
fontWeight(fontWeightFrom(rawProps, sourceProps.fontWeight)),
|
|
104
|
+
italic(convertRawProp(context, rawProps, "fontStyle", sourceProps.italic ? std::string{"italic"} : std::string{}, {}) ==
|
|
105
|
+
"italic") {}
|
|
106
|
+
|
|
107
|
+
std::optional<ControlKind> controlKind(std::string_view componentName) {
|
|
108
|
+
if (componentName == NativeButtonComponentName) {
|
|
109
|
+
return ControlKind::Button;
|
|
110
|
+
}
|
|
111
|
+
if (componentName == SwitchControlComponentName) {
|
|
112
|
+
return ControlKind::Switch;
|
|
113
|
+
}
|
|
114
|
+
if (componentName == ActivityIndicatorControlComponentName) {
|
|
115
|
+
return ControlKind::ActivityIndicator;
|
|
116
|
+
}
|
|
117
|
+
if (componentName == SegmentedControlComponentName) {
|
|
118
|
+
return ControlKind::SegmentedControl;
|
|
119
|
+
}
|
|
120
|
+
if (componentName == DatePickerComponentName) {
|
|
121
|
+
return ControlKind::DatePicker;
|
|
122
|
+
}
|
|
123
|
+
if (componentName == SearchFieldComponentName) {
|
|
124
|
+
return ControlKind::SearchField;
|
|
125
|
+
}
|
|
126
|
+
return std::nullopt;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
} // namespace ngn
|
|
@@ -0,0 +1,250 @@
|
|
|
1
|
+
#include "ngn/native-module.h"
|
|
2
|
+
|
|
3
|
+
#include <atomic>
|
|
4
|
+
|
|
5
|
+
#include <jsi/JSIDynamic.h>
|
|
6
|
+
#include <react/bridging/Dynamic.h>
|
|
7
|
+
#include <react/bridging/Function.h>
|
|
8
|
+
#include <react/bridging/Promise.h>
|
|
9
|
+
|
|
10
|
+
#include <stdexcept>
|
|
11
|
+
|
|
12
|
+
using namespace facebook;
|
|
13
|
+
using namespace facebook::react;
|
|
14
|
+
|
|
15
|
+
namespace ngn {
|
|
16
|
+
|
|
17
|
+
void NativeModule::setEmitter(Emitter emitter) {
|
|
18
|
+
std::lock_guard lock(emitterMutex_);
|
|
19
|
+
emitter_ = std::move(emitter);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
void NativeModule::emit(const std::string &event, folly::dynamic body) const {
|
|
23
|
+
Emitter emitter;
|
|
24
|
+
{
|
|
25
|
+
std::lock_guard lock(emitterMutex_);
|
|
26
|
+
emitter = emitter_;
|
|
27
|
+
}
|
|
28
|
+
if (emitter) {
|
|
29
|
+
emitter(event, std::move(body));
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
void NativeModule::sync(const std::string &method, Sync fn) {
|
|
34
|
+
methods_[method] = Method{.kind = Method::Kind::Sync, .sync = std::move(fn)};
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
void NativeModule::async(const std::string &method, Async fn) {
|
|
38
|
+
methods_[method] = Method{.kind = Method::Kind::Async, .async = std::move(fn)};
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
void NativeModule::promise(const std::string &method, Promise fn) {
|
|
42
|
+
methods_[method] = Method{.kind = Method::Kind::Promise, .promise = std::move(fn)};
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
void NativeModule::subscription(const std::string &method, Subscribe fn) {
|
|
46
|
+
methods_[method] = Method{.kind = Method::Kind::Subscription, .subscribe = std::move(fn)};
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
namespace {
|
|
50
|
+
|
|
51
|
+
// `stop` that runs once, however often it is called.
|
|
52
|
+
std::function<void()> once(std::function<void()> stop) {
|
|
53
|
+
auto pending = std::make_shared<std::atomic<bool>>(true);
|
|
54
|
+
return [pending, stop = std::move(stop)] {
|
|
55
|
+
if (pending->exchange(false) && stop) {
|
|
56
|
+
stop();
|
|
57
|
+
}
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
} // namespace
|
|
62
|
+
|
|
63
|
+
std::function<void()> NativeModule::subscribe(const std::string &method, const folly::dynamic &args, JsCallback callback) const {
|
|
64
|
+
return once(methods_.at(method).subscribe(args, std::move(callback)));
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
void NativeModule::listeners() {
|
|
68
|
+
async("addListener", [](const folly::dynamic &, std::vector<JsCallback>) {});
|
|
69
|
+
async("removeListeners", [](const folly::dynamic &, std::vector<JsCallback>) {});
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
void NativeModule::property(const std::string &name, std::function<folly::dynamic()> fn) {
|
|
73
|
+
properties_[name] = std::move(fn);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
folly::dynamic NativeModule::property(const std::string &name) const {
|
|
77
|
+
auto it = properties_.find(name);
|
|
78
|
+
return it == properties_.end() ? folly::dynamic(nullptr) : it->second();
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
void NativeModule::constants(std::function<folly::dynamic()> fn) {
|
|
82
|
+
sync("getConstants", [fn = std::move(fn)](const folly::dynamic &) { return fn(); });
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
folly::dynamic NativeModule::call(const std::string &method, const folly::dynamic &args) const {
|
|
86
|
+
const auto &m = methods_.at(method);
|
|
87
|
+
if (m.kind == Method::Kind::Sync) {
|
|
88
|
+
return m.sync(args);
|
|
89
|
+
}
|
|
90
|
+
if (m.kind == Method::Kind::Async) {
|
|
91
|
+
m.async(args, {});
|
|
92
|
+
return nullptr;
|
|
93
|
+
}
|
|
94
|
+
throw std::logic_error(name_ + "." + method + " returns a promise");
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
void NativeModule::callWithCallbacks(
|
|
98
|
+
const std::string &method,
|
|
99
|
+
const folly::dynamic &args,
|
|
100
|
+
std::vector<JsCallback> callbacks) const {
|
|
101
|
+
methods_.at(method).async(args, std::move(callbacks));
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
void NativeModule::callWithPromise(const std::string &method, const folly::dynamic &args, JsPromise promise) const {
|
|
105
|
+
methods_.at(method).promise(args, std::move(promise));
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
namespace {
|
|
109
|
+
|
|
110
|
+
jsi::Value toJs(jsi::Runtime &rt, const folly::dynamic &value) {
|
|
111
|
+
return jsi::valueFromDynamic(rt, value);
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
class Adapter final : public TurboModule {
|
|
115
|
+
public:
|
|
116
|
+
Adapter(std::shared_ptr<NativeModule> module, std::shared_ptr<CallInvoker> jsInvoker)
|
|
117
|
+
: TurboModule(module->name(), jsInvoker), module_(std::move(module)) {}
|
|
118
|
+
|
|
119
|
+
void attach(const std::shared_ptr<Adapter> &self) {
|
|
120
|
+
// Events are queued onto the JavaScript thread by emitDeviceEvent. The emitter holds the
|
|
121
|
+
// adapter weakly, so an event from a module whose runtime is gone goes nowhere.
|
|
122
|
+
std::weak_ptr<Adapter> weak = self;
|
|
123
|
+
module_->setEmitter([weak](const std::string &event, folly::dynamic body) {
|
|
124
|
+
if (auto adapter = weak.lock()) {
|
|
125
|
+
adapter->emitDeviceEvent(event, [body = std::move(body)](jsi::Runtime &rt, std::vector<jsi::Value> &args) {
|
|
126
|
+
args.emplace_back(toJs(rt, body));
|
|
127
|
+
});
|
|
128
|
+
}
|
|
129
|
+
});
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
jsi::Value create(jsi::Runtime &rt, const jsi::PropNameID &propName) override {
|
|
133
|
+
std::string name = propName.utf8(rt);
|
|
134
|
+
if (auto property = module_->properties().find(name); property != module_->properties().end()) {
|
|
135
|
+
return toJs(rt, property->second());
|
|
136
|
+
}
|
|
137
|
+
auto it = module_->methods().find(name);
|
|
138
|
+
if (it == module_->methods().end()) {
|
|
139
|
+
return jsi::Value::undefined();
|
|
140
|
+
}
|
|
141
|
+
auto kind = it->second.kind;
|
|
142
|
+
return jsi::Function::createFromHostFunction(
|
|
143
|
+
rt, propName, 0, [this, name, kind](jsi::Runtime &rt, const jsi::Value &, const jsi::Value *args, size_t count) {
|
|
144
|
+
return invoke(rt, name, kind, args, count);
|
|
145
|
+
});
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
~Adapter() override {
|
|
149
|
+
module_->setEmitter(nullptr);
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
private:
|
|
153
|
+
jsi::Value invoke(
|
|
154
|
+
jsi::Runtime &rt,
|
|
155
|
+
const std::string &name,
|
|
156
|
+
NativeModule::Method::Kind kind,
|
|
157
|
+
const jsi::Value *args,
|
|
158
|
+
size_t count) {
|
|
159
|
+
folly::dynamic values = folly::dynamic::array();
|
|
160
|
+
std::vector<JsCallback> callbacks;
|
|
161
|
+
for (size_t i = 0; i < count; i++) {
|
|
162
|
+
if (args[i].isObject() && args[i].asObject(rt).isFunction(rt)) {
|
|
163
|
+
AsyncCallback<> callback(rt, args[i].asObject(rt).asFunction(rt), jsInvoker_);
|
|
164
|
+
callbacks.push_back([callback](folly::dynamic arguments) {
|
|
165
|
+
std::function<void(jsi::Runtime &, jsi::Function &)> impl =
|
|
166
|
+
[arguments = std::move(arguments)](jsi::Runtime &rt, jsi::Function &fn) {
|
|
167
|
+
std::vector<jsi::Value> jsArgs;
|
|
168
|
+
if (arguments.isArray()) {
|
|
169
|
+
for (const auto &a : arguments) {
|
|
170
|
+
jsArgs.push_back(toJs(rt, a));
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
fn.call(rt, static_cast<const jsi::Value *>(jsArgs.data()), jsArgs.size());
|
|
174
|
+
};
|
|
175
|
+
// The same queue, at the same priority, as emitDeviceEvent: a callback JavaScript is
|
|
176
|
+
// waiting on (Networking's request id) must not land after the events that follow it.
|
|
177
|
+
callback.call(std::move(impl));
|
|
178
|
+
});
|
|
179
|
+
values.push_back(nullptr);
|
|
180
|
+
} else {
|
|
181
|
+
values.push_back(jsi::dynamicFromValue(rt, args[i]));
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
const auto &method = module_->methods().at(name);
|
|
185
|
+
switch (kind) {
|
|
186
|
+
case NativeModule::Method::Kind::Sync:
|
|
187
|
+
// Thrown as a JavaScript Error with the module's own message, rather than Hermes's
|
|
188
|
+
// "Exception in HostFunction: " in front of it.
|
|
189
|
+
try {
|
|
190
|
+
return toJs(rt, method.sync(values));
|
|
191
|
+
} catch (const jsi::JSIException &) {
|
|
192
|
+
throw;
|
|
193
|
+
} catch (const std::exception &error) {
|
|
194
|
+
throw jsi::JSError(rt, error.what());
|
|
195
|
+
}
|
|
196
|
+
case NativeModule::Method::Kind::Async:
|
|
197
|
+
method.async(values, std::move(callbacks));
|
|
198
|
+
return jsi::Value::undefined();
|
|
199
|
+
case NativeModule::Method::Kind::Promise: {
|
|
200
|
+
AsyncPromise<folly::dynamic> promise(rt, jsInvoker_);
|
|
201
|
+
method.promise(
|
|
202
|
+
values,
|
|
203
|
+
JsPromise{
|
|
204
|
+
.resolve = [promise](folly::dynamic value) mutable { promise.resolve(std::move(value)); },
|
|
205
|
+
.reject = [promise](const std::string &message) mutable { promise.reject(Error(message)); },
|
|
206
|
+
});
|
|
207
|
+
return bridging::toJs(rt, promise, jsInvoker_);
|
|
208
|
+
}
|
|
209
|
+
case NativeModule::Method::Kind::Subscription: {
|
|
210
|
+
auto promiseClass = rt.global().getPropertyAsObject(rt, "Promise");
|
|
211
|
+
std::function<void()> stop;
|
|
212
|
+
try {
|
|
213
|
+
stop = module_->subscribe(name, values, callbacks.empty() ? JsCallback{} : callbacks[0]);
|
|
214
|
+
} catch (const std::exception &error) {
|
|
215
|
+
auto rejected = promiseClass.getPropertyAsFunction(rt, "reject");
|
|
216
|
+
return rejected.callWithThis(rt, promiseClass, jsi::JSError(rt, error.what()).value());
|
|
217
|
+
}
|
|
218
|
+
auto subscription = jsi::Object(rt);
|
|
219
|
+
subscription.setProperty(
|
|
220
|
+
rt, "remove",
|
|
221
|
+
jsi::Function::createFromHostFunction(
|
|
222
|
+
rt, jsi::PropNameID::forAscii(rt, "remove"), 0,
|
|
223
|
+
[stop](jsi::Runtime &, const jsi::Value &, const jsi::Value *, size_t) {
|
|
224
|
+
stop();
|
|
225
|
+
return jsi::Value::undefined();
|
|
226
|
+
}));
|
|
227
|
+
return promiseClass.getPropertyAsFunction(rt, "resolve").callWithThis(rt, promiseClass, subscription);
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
return jsi::Value::undefined();
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
std::shared_ptr<NativeModule> module_;
|
|
234
|
+
};
|
|
235
|
+
|
|
236
|
+
} // namespace
|
|
237
|
+
|
|
238
|
+
std::shared_ptr<TurboModule> turboModule(std::shared_ptr<NativeModule> module, const std::shared_ptr<CallInvoker> &jsInvoker) {
|
|
239
|
+
auto adapter = std::make_shared<Adapter>(std::move(module), jsInvoker);
|
|
240
|
+
adapter->attach(adapter);
|
|
241
|
+
return adapter;
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
void addModule(ModuleRegistry ®istry, const std::string &name, std::function<std::shared_ptr<NativeModule>()> make) {
|
|
245
|
+
registry.add(name, [make = std::move(make)](const std::shared_ptr<CallInvoker> &jsInvoker) {
|
|
246
|
+
return turboModule(make(), jsInvoker);
|
|
247
|
+
});
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
} // namespace ngn
|
|
@@ -0,0 +1,219 @@
|
|
|
1
|
+
#include "ngn/network.h"
|
|
2
|
+
|
|
3
|
+
#include <folly/io/IOBuf.h>
|
|
4
|
+
|
|
5
|
+
#include <mutex>
|
|
6
|
+
|
|
7
|
+
using namespace facebook::react;
|
|
8
|
+
|
|
9
|
+
namespace ngn {
|
|
10
|
+
|
|
11
|
+
namespace {
|
|
12
|
+
|
|
13
|
+
struct TaskToken final : http::IRequestToken {
|
|
14
|
+
explicit TaskToken(std::unique_ptr<HttpTask> task) : task(std::move(task)) {}
|
|
15
|
+
void cancel() noexcept override {
|
|
16
|
+
if (task) {
|
|
17
|
+
task->cancel();
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
std::unique_ptr<HttpTask> task;
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
class HttpClientAdapter final : public IHttpClient {
|
|
24
|
+
public:
|
|
25
|
+
explicit HttpClientAdapter(std::shared_ptr<Http> http) : http_(std::move(http)) {}
|
|
26
|
+
|
|
27
|
+
std::unique_ptr<http::IRequestToken> sendRequest(
|
|
28
|
+
http::NetworkCallbacks &&callbacks,
|
|
29
|
+
const std::string &method,
|
|
30
|
+
const std::string &url,
|
|
31
|
+
const http::Headers &headers,
|
|
32
|
+
const http::Body &body,
|
|
33
|
+
uint32_t timeout,
|
|
34
|
+
std::optional<std::string>) override {
|
|
35
|
+
HttpRequest request{.method = method, .url = url, .headers = headers, .timeoutMs = timeout};
|
|
36
|
+
if (body.string) {
|
|
37
|
+
request.body = *body.string;
|
|
38
|
+
}
|
|
39
|
+
// One body for the whole response unless the caller asked for it incrementally.
|
|
40
|
+
struct Response {
|
|
41
|
+
http::NetworkCallbacks callbacks;
|
|
42
|
+
std::string body;
|
|
43
|
+
int64_t received{0};
|
|
44
|
+
};
|
|
45
|
+
auto response = std::make_shared<Response>(Response{.callbacks = std::move(callbacks)});
|
|
46
|
+
HttpCallbacks out{
|
|
47
|
+
.onUploadProgress =
|
|
48
|
+
[response](int64_t sent, int64_t total) {
|
|
49
|
+
if (response->callbacks.onUploadProgress) {
|
|
50
|
+
response->callbacks.onUploadProgress(sent, total);
|
|
51
|
+
}
|
|
52
|
+
},
|
|
53
|
+
.onResponse =
|
|
54
|
+
[response](const HttpResponse &r) {
|
|
55
|
+
if (response->callbacks.onResponse) {
|
|
56
|
+
response->callbacks.onResponse(r.status, r.headers);
|
|
57
|
+
}
|
|
58
|
+
},
|
|
59
|
+
.onData =
|
|
60
|
+
[response](std::string chunk, int64_t expected) {
|
|
61
|
+
auto &cb = response->callbacks;
|
|
62
|
+
response->received += static_cast<int64_t>(chunk.size());
|
|
63
|
+
if (cb.sendIncrementalUpdates && cb.onBodyIncremental) {
|
|
64
|
+
cb.onBodyIncremental(response->received, expected, folly::IOBuf::copyBuffer(chunk));
|
|
65
|
+
return;
|
|
66
|
+
}
|
|
67
|
+
response->body += chunk;
|
|
68
|
+
if (cb.sendProgressUpdates && cb.onBodyProgress) {
|
|
69
|
+
cb.onBodyProgress(response->received, expected);
|
|
70
|
+
}
|
|
71
|
+
},
|
|
72
|
+
.onComplete =
|
|
73
|
+
[response](const std::string &error, bool timedOut) {
|
|
74
|
+
auto &cb = response->callbacks;
|
|
75
|
+
if (error.empty() && !timedOut && !cb.sendIncrementalUpdates && cb.onBody) {
|
|
76
|
+
cb.onBody(folly::IOBuf::copyBuffer(response->body));
|
|
77
|
+
}
|
|
78
|
+
if (cb.onResponseComplete) {
|
|
79
|
+
cb.onResponseComplete(error, timedOut);
|
|
80
|
+
}
|
|
81
|
+
},
|
|
82
|
+
};
|
|
83
|
+
return std::make_unique<TaskToken>(http_->send(std::move(request), std::move(out)));
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
private:
|
|
87
|
+
std::shared_ptr<Http> http_;
|
|
88
|
+
};
|
|
89
|
+
|
|
90
|
+
class WebSocketClientAdapter final : public IWebSocketClient {
|
|
91
|
+
public:
|
|
92
|
+
explicit WebSocketClientAdapter(std::shared_ptr<WebSockets> webSockets)
|
|
93
|
+
: webSockets_(std::move(webSockets)), callbacks_(std::make_shared<Callbacks>()) {}
|
|
94
|
+
|
|
95
|
+
~WebSocketClientAdapter() override {
|
|
96
|
+
callbacks_->clear();
|
|
97
|
+
if (connection_) {
|
|
98
|
+
connection_->close(1000, "");
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
void setOnClosedCallback(OnClosedCallback &&callback) noexcept override {
|
|
103
|
+
std::lock_guard lock(callbacks_->mutex);
|
|
104
|
+
callbacks_->closed = std::move(callback);
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
void setOnMessageCallback(OnMessageCallback &&callback) noexcept override {
|
|
108
|
+
std::lock_guard lock(callbacks_->mutex);
|
|
109
|
+
callbacks_->message = std::move(callback);
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
void connect(const std::string &url, OnConnectCallback &&callback) override {
|
|
113
|
+
{
|
|
114
|
+
std::lock_guard lock(callbacks_->mutex);
|
|
115
|
+
callbacks_->connect = std::move(callback);
|
|
116
|
+
}
|
|
117
|
+
auto callbacks = callbacks_;
|
|
118
|
+
connection_ = webSockets_->connect(
|
|
119
|
+
url,
|
|
120
|
+
{},
|
|
121
|
+
{},
|
|
122
|
+
WebSocketCallbacks{
|
|
123
|
+
.onOpen = [callbacks](const std::string &) { callbacks->connected(true, ""); },
|
|
124
|
+
.onText = [callbacks](const std::string &text) { callbacks->received(text); },
|
|
125
|
+
.onBinary = [callbacks](const std::string &bytes) { callbacks->received(bytes); },
|
|
126
|
+
.onClose = [callbacks](int, const std::string &reason) { callbacks->closedWith(reason); },
|
|
127
|
+
.onError =
|
|
128
|
+
[callbacks](const std::string &message) {
|
|
129
|
+
// A failure before the connection opened answers connect; after, it closes it.
|
|
130
|
+
if (!callbacks->connected(false, message)) {
|
|
131
|
+
callbacks->closedWith(message);
|
|
132
|
+
}
|
|
133
|
+
},
|
|
134
|
+
});
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
void close(const std::string &reason) override {
|
|
138
|
+
if (connection_) {
|
|
139
|
+
connection_->close(1000, reason);
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
void send(const std::string &message) override {
|
|
144
|
+
if (connection_) {
|
|
145
|
+
connection_->sendText(message);
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
void ping() override {
|
|
150
|
+
if (connection_) {
|
|
151
|
+
connection_->ping();
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
private:
|
|
156
|
+
struct Callbacks {
|
|
157
|
+
std::mutex mutex;
|
|
158
|
+
OnConnectCallback connect;
|
|
159
|
+
OnClosedCallback closed;
|
|
160
|
+
OnMessageCallback message;
|
|
161
|
+
|
|
162
|
+
// Answers connect once. False when it was already answered.
|
|
163
|
+
bool connected(bool success, const std::string &error) {
|
|
164
|
+
OnConnectCallback callback;
|
|
165
|
+
{
|
|
166
|
+
std::lock_guard lock(mutex);
|
|
167
|
+
callback = std::move(connect);
|
|
168
|
+
connect = nullptr;
|
|
169
|
+
}
|
|
170
|
+
if (!callback) {
|
|
171
|
+
return false;
|
|
172
|
+
}
|
|
173
|
+
callback(success, error);
|
|
174
|
+
return true;
|
|
175
|
+
}
|
|
176
|
+
void received(const std::string &text) {
|
|
177
|
+
OnMessageCallback callback;
|
|
178
|
+
{
|
|
179
|
+
std::lock_guard lock(mutex);
|
|
180
|
+
callback = message;
|
|
181
|
+
}
|
|
182
|
+
if (callback) {
|
|
183
|
+
callback(text);
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
void closedWith(const std::string &reason) {
|
|
187
|
+
OnClosedCallback callback;
|
|
188
|
+
{
|
|
189
|
+
std::lock_guard lock(mutex);
|
|
190
|
+
callback = closed;
|
|
191
|
+
}
|
|
192
|
+
if (callback) {
|
|
193
|
+
callback(reason);
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
void clear() {
|
|
197
|
+
std::lock_guard lock(mutex);
|
|
198
|
+
connect = nullptr;
|
|
199
|
+
closed = nullptr;
|
|
200
|
+
message = nullptr;
|
|
201
|
+
}
|
|
202
|
+
};
|
|
203
|
+
|
|
204
|
+
std::shared_ptr<WebSockets> webSockets_;
|
|
205
|
+
std::shared_ptr<Callbacks> callbacks_;
|
|
206
|
+
std::unique_ptr<WebSocketConnection> connection_;
|
|
207
|
+
};
|
|
208
|
+
|
|
209
|
+
} // namespace
|
|
210
|
+
|
|
211
|
+
HttpClientFactory httpClientFactory(std::shared_ptr<Http> http) {
|
|
212
|
+
return [http] { return std::make_unique<HttpClientAdapter>(http); };
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
WebSocketClientFactory webSocketClientFactory(std::shared_ptr<WebSockets> webSockets) {
|
|
216
|
+
return [webSockets] { return std::make_unique<WebSocketClientAdapter>(webSockets); };
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
} // namespace ngn
|