12#include <emscripten.h>
13#include <emscripten/val.h>
19#include "platforms/wasm/js_fetch.h"
24#if defined(FASTLED_HAS_NETWORKING) && !defined(FL_IS_WASM)
29 #include "platforms/win/socket_win.h"
31 #include "platforms/posix/socket_posix.h"
51 ::fl::wasm_fetch.get(url).response(callback);
63 fl::string fetch_url = request.url().empty() ? url : request.url();
66 auto wasm_request = ::fl::WasmFetchRequest(fetch_url);
70 wasm_request.response([promise](
const Response& resp)
mutable {
72 if (promise.valid()) {
73 promise.complete_with_value(resp);
82#elif defined(FASTLED_HAS_NETWORKING)
86 #define SOCKET_ERROR_WOULD_BLOCK WSAEWOULDBLOCK
87 #define SOCKET_ERROR_IN_PROGRESS WSAEINPROGRESS
89 #define SOCKET_ERROR_WOULD_BLOCK EWOULDBLOCK
90 #define SOCKET_ERROR_IN_PROGRESS EINPROGRESS
353 .then([callback](
const Response& resp) {
356 .catch_([callback](
const fl::task::Error& err) {
358 Response resp(500,
"Internal Server Error");
379 .
then([fetch_req, task_ptr]() {
384 if (fetch_req->is_done()) {
388 .catch_([promise](
const fl::task::Error& e)
mutable {
390 if (promise.valid() && !promise.is_completed()) {
391 promise.complete_with_error(e);
406 Response resp(501,
"Not Implemented");
407 resp.
set_text(
"HTTP fetch not supported on this platform");
414 FL_WARN(
"HTTP fetch is not supported on this platform. URL: %s",
url);
415 Response error_response(501,
"Not Implemented");
416 error_response.
set_body(
"HTTP fetch is not available on this platform.");
465 if (promise.valid()) {
500 if (promise.valid() && !promise.is_completed()) {
517 const auto& opts = request.
options();
518 get_request.
timeout(opts.timeout_ms);
519 for (
const auto& header : opts.headers) {
520 get_request.
header(header.first, header.second);
522 if (!opts.body.empty()) {
523 get_request.
body(opts.body);
534 const auto& opts = request.
options();
535 post_request.
timeout(opts.timeout_ms);
536 for (
const auto& header : opts.headers) {
537 post_request.
header(header.first, header.second);
539 if (!opts.body.empty()) {
540 post_request.
body(opts.body);
551 const auto& opts = request.
options();
552 put_request.
timeout(opts.timeout_ms);
553 for (
const auto& header : opts.headers) {
554 put_request.
header(header.first, header.second);
556 if (!opts.body.empty()) {
557 put_request.
body(opts.body);
568 const auto& opts = request.
options();
569 delete_request.
timeout(opts.timeout_ms);
570 for (
const auto& header : opts.headers) {
571 delete_request.
header(header.first, header.second);
573 if (!opts.body.empty()) {
574 delete_request.
body(opts.body);
585 const auto& opts = request.
options();
586 head_request.
timeout(opts.timeout_ms);
587 for (
const auto& header : opts.headers) {
588 head_request.
header(header.first, header.second);
590 if (!opts.body.empty()) {
591 head_request.
body(opts.body);
602 const auto& opts = request.
options();
603 options_request.
timeout(opts.timeout_ms);
604 for (
const auto& header : opts.headers) {
605 options_request.
header(header.first, header.second);
607 if (!opts.body.empty()) {
608 options_request.
body(opts.body);
619 const auto& opts = request.
options();
620 patch_request.
timeout(opts.timeout_ms);
621 for (
const auto& header : opts.headers) {
622 patch_request.
header(header.first, header.second);
624 if (!opts.body.empty()) {
625 patch_request.
body(opts.body);
static void removeListener(Listener *listener) FL_NO_EXCEPT
static void addListener(Listener *listener, int priority=0) FL_NO_EXCEPT
static T & instance() FL_NO_EXCEPT
void onEndFrame() override
FetchEngineListener() FL_NO_EXCEPT
~FetchEngineListener() FL_NO_EXCEPT override
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_NO_EXCEPT
Sets the callback invoked when the task runs successfully.
static Promise< T > resolve(const T &value) FL_NO_EXCEPT
Create a resolved Promise with value.
static Promise< T > create() FL_NO_EXCEPT
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_NO_EXCEPT
Task executor — runs registered task runners and manages the run loop.
Unified HTTP fetch API for FastLED (cross-platform)
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.
void run(fl::u32 microseconds, ExecFlags flags)
Run selected task subsystems.
Handle every_ms(int interval_ms)
Creates a task that runs at a fixed millisecond interval.
constexpr remove_reference< T >::type && move(T &&t) FL_NO_EXCEPT
fl::enable_if<!fl::is_array< T >::value, unique_ptr< T > >::type make_unique(Args &&... args) FL_NO_EXCEPT
shared_ptr< T > make_shared(Args &&... args) FL_NO_EXCEPT
InputGamut g FL_NO_EXCEPT
Base definition for an LED controller.
Request options (matches JavaScript fetch RequestInit)