12#include <emscripten.h>
13#include <emscripten/val.h>
19#include "platforms/wasm/js_fetch.h"
23#if defined(FASTLED_HAS_NETWORKING) && !defined(FL_IS_WASM)
28 #include "platforms/win/socket_win.h"
30 #include "platforms/posix/socket_posix.h"
50 ::fl::wasm_fetch.get(url).response(callback);
62 fl::string fetch_url = request.url().empty() ? url : request.url();
65 auto wasm_request = ::fl::WasmFetchRequest(fetch_url);
69 wasm_request.response([promise](
const Response& resp)
mutable {
71 if (promise.valid()) {
72 promise.complete_with_value(resp);
81#elif defined(FASTLED_HAS_NETWORKING)
85 #define SOCKET_ERROR_WOULD_BLOCK WSAEWOULDBLOCK
86 #define SOCKET_ERROR_IN_PROGRESS WSAEINPROGRESS
88 #define SOCKET_ERROR_WOULD_BLOCK EWOULDBLOCK
89 #define SOCKET_ERROR_IN_PROGRESS EINPROGRESS
352 .then([callback](
const Response& resp) {
355 .catch_([callback](
const fl::task::Error& err) {
357 Response resp(500,
"Internal Server Error");
378 .
then([fetch_req, task_ptr]() {
383 if (fetch_req->is_done()) {
387 .catch_([promise](
const fl::task::Error& e)
mutable {
389 if (promise.valid() && !promise.is_completed()) {
390 promise.complete_with_error(e);
405 Response resp(501,
"Not Implemented");
406 resp.
set_text(
"HTTP fetch not supported on this platform");
412 FL_WARN(
"HTTP fetch is not supported on this platform. URL: " <<
url);
413 Response error_response(501,
"Not Implemented");
414 error_response.
set_body(
"HTTP fetch is not available on this platform.");
463 if (promise.valid()) {
498 if (promise.valid() && !promise.is_completed()) {
515 const auto& opts = request.
options();
516 get_request.
timeout(opts.timeout_ms);
517 for (
const auto& header : opts.headers) {
518 get_request.
header(header.first, header.second);
520 if (!opts.body.empty()) {
521 get_request.
body(opts.body);
532 const auto& opts = request.
options();
533 post_request.
timeout(opts.timeout_ms);
534 for (
const auto& header : opts.headers) {
535 post_request.
header(header.first, header.second);
537 if (!opts.body.empty()) {
538 post_request.
body(opts.body);
549 const auto& opts = request.
options();
550 put_request.
timeout(opts.timeout_ms);
551 for (
const auto& header : opts.headers) {
552 put_request.
header(header.first, header.second);
554 if (!opts.body.empty()) {
555 put_request.
body(opts.body);
566 const auto& opts = request.
options();
567 delete_request.
timeout(opts.timeout_ms);
568 for (
const auto& header : opts.headers) {
569 delete_request.
header(header.first, header.second);
571 if (!opts.body.empty()) {
572 delete_request.
body(opts.body);
583 const auto& opts = request.
options();
584 head_request.
timeout(opts.timeout_ms);
585 for (
const auto& header : opts.headers) {
586 head_request.
header(header.first, header.second);
588 if (!opts.body.empty()) {
589 head_request.
body(opts.body);
600 const auto& opts = request.
options();
601 options_request.
timeout(opts.timeout_ms);
602 for (
const auto& header : opts.headers) {
603 options_request.
header(header.first, header.second);
605 if (!opts.body.empty()) {
606 options_request.
body(opts.body);
617 const auto& opts = request.
options();
618 patch_request.
timeout(opts.timeout_ms);
619 for (
const auto& header : opts.headers) {
620 patch_request.
header(header.first, header.second);
622 if (!opts.body.empty()) {
623 patch_request.
body(opts.body);
static void removeListener(Listener *listener) FL_NOEXCEPT
static void addListener(Listener *listener, int priority=0) FL_NOEXCEPT
static T & instance() FL_NOEXCEPT
void onEndFrame() override
~FetchEngineListener() FL_NOEXCEPT override
FetchEngineListener() FL_NOEXCEPT
bool has_active_tasks() const override
Check if this runner has active tasks.
void cleanup_completed_promises()
fl::unique_ptr< FetchEngineListener > mEngineListener
size_t active_task_count() const override
Get number of active tasks (for debugging/monitoring)
fl::vector< fl::task::Promise< Response > > mActivePromises
static FetchManager & instance()
void register_promise(const fl::task::Promise< Response > &promise)
fl::size active_requests() const
void update() override
Update this runner (called during task pumping)
Internal fetch manager for promise tracking.
FetchOptions & header(const fl::string &name, const fl::string &value)
Add header.
FetchOptions & body(const fl::string &data)
Set request body.
FetchOptions & timeout(int timeout_ms)
Set timeout in milliseconds.
const RequestOptions & options() const
Get the options for this request.
Fetch options builder (fluent interface)
bool is_json() const
Check if response appears to contain JSON content.
void set_text(const fl::string &body)
fl::json parse_json_body() const
Parse JSON from response body with error handling.
fl::json json() const
Response body parsed as JSON (JavaScript-like API)
fl::optional< fl::json > mCachedJson
void set_body(const fl::string &body)
HTTP response class (unified interface)
static constexpr fl::size npos
void register_runner(Runner *r)
Register a runner.
static Executor & instance()
void unregister_runner(Runner *r)
Unregister a runner.
Handle & then(function< void()> on_then) FL_NOEXCEPT
static Promise< T > resolve(const T &value) FL_NOEXCEPT
Create a resolved Promise with value.
static Promise< T > create() FL_NOEXCEPT
Create a pending Promise.
Promise class that provides fluent .then() and .catch_() semantics This is a lightweight wrapper arou...
static Scheduler & instance()
void push_back(const T &value) FL_NOEXCEPT
Task executor — runs registered task runners and manages the run loop.
Unified HTTP fetch API for FastLED (cross-platform)
constexpr remove_reference< T >::type && move(T &&t) FL_NOEXCEPT
void fetch_update()
Legacy manual update for fetch promises (use fl::task::run() for new code)
fl::task::Promise< Response > fetch_delete(const fl::string &url, const FetchOptions &request)
HTTP DELETE request.
fl::function< void(const Response &)> FetchCallback
Callback type for simple fetch responses (backward compatible)
fl::task::Promise< Response > fetch_http_options(const fl::string &url, const FetchOptions &request)
HTTP OPTIONS request.
fl::task::Promise< Response > fetch_patch(const fl::string &url, const FetchOptions &request)
HTTP PATCH request.
fl::task::Promise< Response > fetch_post(const fl::string &url, const FetchOptions &request)
HTTP POST request.
fl::task::Promise< Response > fetch_head(const fl::string &url, const FetchOptions &request)
HTTP HEAD request.
fl::task::Promise< Response > execute_fetch_request(const fl::string &url, const FetchOptions &request)
Internal helper to execute a fetch request and return a promise.
fl::task::Promise< Response > fetch_get(const fl::string &url, const FetchOptions &request)
HTTP GET request.
fl::task::Promise< Response > fetch_request(const fl::string &url, const RequestOptions &options)
Generic request with options (like fetch(url, options))
void fetch(const fl::string &url, const FetchCallback &callback)
Make an HTTP GET request (cross-platform, backward compatible)
fl::task::Promise< Response > fetch_put(const fl::string &url, const FetchOptions &request)
HTTP PUT request.
fl::size fetch_active_requests()
Get number of active requests.
Handle every_ms(int interval_ms)
void run(fl::u32 microseconds, ExecFlags flags)
Run selected task subsystems.
fl::enable_if<!fl::is_array< T >::value, unique_ptr< T > >::type make_unique(Args &&... args) FL_NOEXCEPT
shared_ptr< T > make_shared(Args &&... args) FL_NOEXCEPT
Base definition for an LED controller.
Request options (matches JavaScript fetch RequestInit)