Files
Eric FELIXINE e30ae8ed09 feat(smart-app): implement complete mobile app MVP
- App.tsx: full navigation (Auth stack + Main tabs with 5 screens)
- Auth: LoginScreen, RegisterScreen, ForgotPasswordScreen
- HomeScreen: dashboard with IoT metrics, weather widget, alerts, quick actions, sensors
- MapScreen: interactive map with layer toggles (6 layers)
- MarketplaceScreen: categories (6), products (5), search
- ChatScreen: AI chat with quick prompts (4), bot responses
- ProfileScreen: user info, stats, menu (9 items), logout
- AlertsScreen: alert list with severity, acknowledge
- SensorsScreen: sensor list with type filters (6 types), search
- ZonesScreen: zone cards with stats
- SettingsScreen: language picker (FR/EN/ES/DE), privacy, about
- Stores: iotStore (sensors, zones, alerts), notificationStore, uiStore + i18n
- Hooks: useSensors, useAlerts, useNotifications, useLocation
- Components: Card, Button, LoadingSpinner, ErrorBoundary, Header
- Services: iotService, notificationService (with axios API client)
- Utils: formatters (temp, AQI, noise, dates), validators (email, password, IBAN)
- Theme: colors.ts with full design system (Blue Ocean palette)
- Ditto: fixed MongoDB connection, new JWT secrets, official gateway image
2026-06-01 18:00:35 -04:00

125 lines
3.9 KiB
Plaintext

/**
* Copyright (c) 2013-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @providesModule fetchWithRetries
* @typechecks
* @flow
*/
'use strict';
const ExecutionEnvironment = require("./ExecutionEnvironment");
const sprintf = require("./sprintf");
const fetch = require("./fetch");
const warning = require("./warning");
export type InitWithRetries = {
body?: mixed,
cache?: ?string,
credentials?: ?string,
fetchTimeout?: ?number,
headers?: mixed,
method?: ?string,
mode?: ?string,
retryDelays?: ?Array<number>,
};
const DEFAULT_TIMEOUT = 15000;
const DEFAULT_RETRIES = [1000, 3000];
/**
* Makes a POST request to the server with the given data as the payload.
* Automatic retries are done based on the values in `retryDelays`.
*/
function fetchWithRetries(uri: string, initWithRetries?: ?InitWithRetries): Promise<any> {
const {
fetchTimeout,
retryDelays,
...init
} = initWithRetries || {};
const _fetchTimeout = fetchTimeout != null ? fetchTimeout : DEFAULT_TIMEOUT;
const _retryDelays = retryDelays != null ? retryDelays : DEFAULT_RETRIES;
let requestsAttempted = 0;
let requestStartTime = 0;
return new Promise((resolve, reject) => {
/**
* Sends a request to the server that will timeout after `fetchTimeout`.
* If the request fails or times out a new request might be scheduled.
*/
function sendTimedRequest(): void {
requestsAttempted++;
requestStartTime = Date.now();
let isRequestAlive = true;
const request = fetch(uri, init);
const requestTimeout = setTimeout(() => {
isRequestAlive = false;
if (shouldRetry(requestsAttempted)) {
warning(false, 'fetchWithRetries: HTTP timeout, retrying.');
retryRequest();
} else {
reject(new Error(sprintf('fetchWithRetries(): Failed to get response from server, ' + 'tried %s times.', requestsAttempted)));
}
}, _fetchTimeout);
request.then(response => {
clearTimeout(requestTimeout);
if (isRequestAlive) {
// We got a response, we can clear the timeout.
if (response.status >= 200 && response.status < 300) {
// Got a response code that indicates success, resolve the promise.
resolve(response);
} else if (shouldRetry(requestsAttempted)) {
// Fetch was not successful, retrying.
// TODO(#7595849): Only retry on transient HTTP errors.
warning(false, 'fetchWithRetries: HTTP error, retrying.'), retryRequest();
} else {
// Request was not successful, giving up.
const error: any = new Error(sprintf('fetchWithRetries(): Still no successful response after ' + '%s retries, giving up.', requestsAttempted));
error.response = response;
reject(error);
}
}
}).catch(error => {
clearTimeout(requestTimeout);
if (shouldRetry(requestsAttempted)) {
retryRequest();
} else {
reject(error);
}
});
}
/**
* Schedules another run of sendTimedRequest based on how much time has
* passed between the time the last request was sent and now.
*/
function retryRequest(): void {
const retryDelay = _retryDelays[requestsAttempted - 1];
const retryStartTime = requestStartTime + retryDelay; // Schedule retry for a configured duration after last request started.
setTimeout(sendTimedRequest, retryStartTime - Date.now());
}
/**
* Checks if another attempt should be done to send a request to the server.
*/
function shouldRetry(attempt: number): boolean {
return ExecutionEnvironment.canUseDOM && attempt <= _retryDelays.length;
}
sendTimedRequest();
});
}
module.exports = fetchWithRetries;